From f33c6e3434bc0133910347ddd0a32787e66efa4b Mon Sep 17 00:00:00 2001 From: alpha-nerd-nomyo Date: Wed, 4 Mar 2026 11:06:17 +0100 Subject: [PATCH] feat: added addtl. error handling (Forbidden, ServiceUnavailable) --- nomyo/SecureCompletionClient.py | 36 ++++++++++++++++++++++++++++++++- nomyo/__init__.py | 6 +++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/nomyo/SecureCompletionClient.py b/nomyo/SecureCompletionClient.py index 790d97d..0e99c69 100644 --- a/nomyo/SecureCompletionClient.py +++ b/nomyo/SecureCompletionClient.py @@ -51,11 +51,21 @@ class RateLimitError(APIError): def __init__(self, message: str, status_code: int = 429, error_details: Optional[Dict[str, Any]] = None): super().__init__(message, status_code, error_details) +class ForbiddenError(APIError): + """Raised when access is forbidden (HTTP 403), e.g. model not allowed for the requested security tier.""" + def __init__(self, message: str, status_code: int = 403, error_details: Optional[Dict[str, Any]] = None): + super().__init__(message, status_code, error_details) + class ServerError(APIError): """Raised when the server returns an error (HTTP 500).""" def __init__(self, message: str, status_code: int = 500, error_details: Optional[Dict[str, Any]] = None): super().__init__(message, status_code, error_details) +class ServiceUnavailableError(APIError): + """Raised when the inference backend is unavailable (HTTP 503).""" + def __init__(self, message: str, status_code: int = 503, error_details: Optional[Dict[str, Any]] = None): + super().__init__(message, status_code, error_details) + class SecureCompletionClient: """ Client for the /v1/chat/secure_completion endpoint. @@ -716,6 +726,18 @@ class SecureCompletionClient: except (json.JSONDecodeError, ValueError): raise AuthenticationError("Invalid API key or authentication failed") + elif response.status_code == 403: + # Forbidden - model not allowed for security tier + try: + error = response.json() + raise ForbiddenError( + f"Forbidden: {error.get('detail', 'Model not allowed for the requested security tier')}", + status_code=403, + error_details=error + ) + except (json.JSONDecodeError, ValueError): + raise ForbiddenError("Forbidden: Model not allowed for the requested security tier") + elif response.status_code == 404: # Endpoint not found try: @@ -752,6 +774,18 @@ class SecureCompletionClient: except (json.JSONDecodeError, ValueError): raise ServerError("Server error: Internal server error") + elif response.status_code == 503: + # Service unavailable - inference backend is down + try: + error = response.json() + raise ServiceUnavailableError( + f"Service unavailable: {error.get('detail', 'Inference backend is unavailable')}", + status_code=503, + error_details=error + ) + except (json.JSONDecodeError, ValueError): + raise ServiceUnavailableError("Service unavailable: Inference backend is unavailable") + else: # Unexpected status code unexp_detail = response.json() @@ -766,7 +800,7 @@ class SecureCompletionClient: except httpx.NetworkError as e: raise APIConnectionError(f"Failed to connect to router: {e}") - except (SecurityError, APIError, AuthenticationError, InvalidRequestError, RateLimitError, ServerError, APIConnectionError): + except (SecurityError, APIError, AuthenticationError, InvalidRequestError, ForbiddenError, RateLimitError, ServerError, ServiceUnavailableError, APIConnectionError): raise # Re-raise known exceptions except Exception as e: raise Exception(f"Request failed: {e}") diff --git a/nomyo/__init__.py b/nomyo/__init__.py index 427930d..0209dba 100644 --- a/nomyo/__init__.py +++ b/nomyo/__init__.py @@ -10,8 +10,10 @@ from .SecureCompletionClient import ( AuthenticationError, InvalidRequestError, APIConnectionError, + ForbiddenError, RateLimitError, - ServerError + ServerError, + ServiceUnavailableError ) # Import secure memory module if available @@ -33,8 +35,10 @@ __all__ = [ 'AuthenticationError', 'InvalidRequestError', 'APIConnectionError', + 'ForbiddenError', 'RateLimitError', 'ServerError', + 'ServiceUnavailableError', 'get_memory_protection_info', 'disable_secure_memory', 'enable_secure_memory',