Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /stateset_stateset-agents /stateset_agents /api /errors.py
| """ | |
| API Error Handling Module | |
| Unified error handling with consistent response format across all API services. | |
| """ | |
| import logging | |
| from datetime import datetime | |
| from enum import Enum | |
| from typing import Any | |
| from fastapi import FastAPI, HTTPException, Request | |
| from fastapi.exceptions import RequestValidationError | |
| from fastapi.responses import JSONResponse | |
| from pydantic import BaseModel, ConfigDict, Field | |
| from starlette.exceptions import HTTPException as StarletteHTTPException | |
| logger = logging.getLogger(__name__) | |
| # ============================================================================ | |
| # Error Codes | |
| # ============================================================================ | |
| class ErrorCode(str, Enum): | |
| """Standardized error codes.""" | |
| # Client errors (4xx) | |
| BAD_REQUEST = "BAD_REQUEST" | |
| UNAUTHORIZED = "UNAUTHORIZED" | |
| FORBIDDEN = "FORBIDDEN" | |
| NOT_FOUND = "NOT_FOUND" | |
| METHOD_NOT_ALLOWED = "METHOD_NOT_ALLOWED" | |
| CONFLICT = "CONFLICT" | |
| VALIDATION_ERROR = "VALIDATION_ERROR" | |
| RATE_LIMIT_EXCEEDED = "RATE_LIMIT_EXCEEDED" | |
| PAYLOAD_TOO_LARGE = "PAYLOAD_TOO_LARGE" | |
| # Server errors (5xx) | |
| INTERNAL_ERROR = "INTERNAL_ERROR" | |
| SERVICE_UNAVAILABLE = "SERVICE_UNAVAILABLE" | |
| TIMEOUT = "TIMEOUT" | |
| DEPENDENCY_ERROR = "DEPENDENCY_ERROR" | |
| # Business logic errors | |
| RESOURCE_EXHAUSTED = "RESOURCE_EXHAUSTED" | |
| INVALID_STATE = "INVALID_STATE" | |
| OPERATION_FAILED = "OPERATION_FAILED" | |
| TRAINING_FAILED = "TRAINING_FAILED" | |
| AGENT_ERROR = "AGENT_ERROR" | |
| # ============================================================================ | |
| # Error Response Models | |
| # ============================================================================ | |
| class ErrorDetail(BaseModel): | |
| """Individual error detail.""" | |
| field: str | None = Field(None, description="Field that caused the error") | |
| message: str = Field(..., description="Human-readable error message") | |
| code: str | None = Field(None, description="Machine-readable error code") | |
| class ErrorResponse(BaseModel): | |
| """Standardized error response envelope.""" | |
| model_config = ConfigDict( | |
| json_schema_extra={ | |
| "example": { | |
| "error": { | |
| "code": "VALIDATION_ERROR", | |
| "message": "Request validation failed", | |
| "details": [ | |
| { | |
| "field": "prompts", | |
| "message": "At least one prompt is required", | |
| "code": "required", | |
| } | |
| ], | |
| }, | |
| "request_id": "550e8400-e29b-41d4-a716-446655440000", | |
| "timestamp": "2024-01-15T10:30:00.000Z", | |
| "path": "/api/v1/train", | |
| } | |
| } | |
| ) | |
| error: dict[str, Any] = Field(..., description="Error information") | |
| request_id: str = Field(..., description="Unique request identifier for tracing") | |
| timestamp: str = Field(..., description="ISO 8601 timestamp") | |
| path: str = Field(..., description="Request path that caused the error") | |
| documentation_url: str | None = Field( | |
| None, description="Link to relevant documentation" | |
| ) | |
| # ============================================================================ | |
| # API Exceptions | |
| # ============================================================================ | |
| class APIError(Exception): | |
| """Base API exception with consistent error information.""" | |
| def __init__( | |
| self, | |
| code: ErrorCode, | |
| message: str, | |
| status_code: int = 500, | |
| details: list[ErrorDetail] | None = None, | |
| documentation_url: str | None = None, | |
| internal_error: Exception | None = None, | |
| ): | |
| super().__init__(message) | |
| self.code = code | |
| self.message = message | |
| self.status_code = status_code | |
| self.details = details or [] | |
| self.documentation_url = documentation_url | |
| self.internal_error = internal_error | |
| def to_response(self, request_id: str, path: str) -> dict[str, Any]: | |
| """Convert to response dictionary.""" | |
| response: dict[str, Any] = { | |
| "error": { | |
| "code": self.code.value, | |
| "message": self.message, | |
| }, | |
| "request_id": request_id, | |
| "timestamp": datetime.utcnow().isoformat() + "Z", | |
| "path": path, | |
| } | |
| if self.details: | |
| response["error"]["details"] = [ | |
| d.model_dump(exclude_none=True) for d in self.details | |
| ] | |
| if self.documentation_url: | |
| response["documentation_url"] = self.documentation_url | |
| return response | |
| class BadRequestError(APIError): | |
| """400 Bad Request.""" | |
| def __init__(self, message: str, details: list[ErrorDetail] | None = None): | |
| super().__init__(ErrorCode.BAD_REQUEST, message, 400, details) | |
| class UnauthorizedError(APIError): | |
| """401 Unauthorized.""" | |
| def __init__(self, message: str = "Authentication required"): | |
| super().__init__(ErrorCode.UNAUTHORIZED, message, 401) | |
| class ForbiddenError(APIError): | |
| """403 Forbidden.""" | |
| def __init__(self, message: str = "Access denied"): | |
| super().__init__(ErrorCode.FORBIDDEN, message, 403) | |
| class NotFoundError(APIError): | |
| """404 Not Found.""" | |
| def __init__(self, resource: str, identifier: str | None = None): | |
| message = f"{resource} not found" | |
| if identifier: | |
| message = f"{resource} '{identifier}' not found" | |
| super().__init__(ErrorCode.NOT_FOUND, message, 404) | |
| class ConflictError(APIError): | |
| """409 Conflict.""" | |
| def __init__(self, message: str): | |
| super().__init__(ErrorCode.CONFLICT, message, 409) | |
| class ValidationError(APIError): | |
| """422 Validation Error.""" | |
| def __init__(self, message: str, details: list[ErrorDetail] | None = None): | |
| super().__init__(ErrorCode.VALIDATION_ERROR, message, 422, details) | |
| class RateLimitError(APIError): | |
| """429 Rate Limit Exceeded.""" | |
| def __init__(self, retry_after: int = 60): | |
| super().__init__( | |
| ErrorCode.RATE_LIMIT_EXCEEDED, | |
| f"Rate limit exceeded. Retry after {retry_after} seconds.", | |
| 429, | |
| ) | |
| self.retry_after = retry_after | |
| class InternalError(APIError): | |
| """500 Internal Server Error.""" | |
| def __init__( | |
| self, | |
| message: str = "An internal error occurred", | |
| internal_error: Exception | None = None, | |
| ): | |
| super().__init__( | |
| ErrorCode.INTERNAL_ERROR, message, 500, internal_error=internal_error | |
| ) | |
| class ServiceUnavailableError(APIError): | |
| """503 Service Unavailable.""" | |
| def __init__(self, message: str = "Service temporarily unavailable"): | |
| super().__init__(ErrorCode.SERVICE_UNAVAILABLE, message, 503) | |
| class TrainingError(APIError): | |
| """Training-specific error.""" | |
| def __init__(self, message: str, details: list[ErrorDetail] | None = None): | |
| super().__init__(ErrorCode.TRAINING_FAILED, message, 500, details) | |
| class AgentError(APIError): | |
| """Agent-specific error.""" | |
| def __init__(self, message: str, details: list[ErrorDetail] | None = None): | |
| super().__init__(ErrorCode.AGENT_ERROR, message, 500, details) | |
| # ============================================================================ | |
| # Domain-Specific Exceptions | |
| # ============================================================================ | |
| class AgentNotFoundError(NotFoundError): | |
| """Agent with given ID not found.""" | |
| def __init__(self, agent_id: str): | |
| super().__init__("Agent", agent_id) | |
| self.agent_id = agent_id | |
| class InvalidAgentConfigError(BadRequestError): | |
| """Agent configuration is invalid.""" | |
| def __init__( | |
| self, details: str, validation_errors: list[ErrorDetail] | None = None | |
| ): | |
| super().__init__(f"Invalid agent configuration: {details}", validation_errors) | |
| class ConversationNotFoundError(NotFoundError): | |
| """Conversation not found.""" | |
| def __init__(self, conversation_id: str): | |
| super().__init__("Conversation", conversation_id) | |
| self.conversation_id = conversation_id | |
| class TrainingJobNotFoundError(NotFoundError): | |
| """Training job not found.""" | |
| def __init__(self, job_id: str): | |
| super().__init__("Training job", job_id) | |
| self.job_id = job_id | |
| class TrainingConfigError(BadRequestError): | |
| """Training configuration is invalid.""" | |
| def __init__( | |
| self, details: str, validation_errors: list[ErrorDetail] | None = None | |
| ): | |
| super().__init__( | |
| f"Invalid training configuration: {details}", validation_errors | |
| ) | |
| class TokenizationError(InternalError): | |
| """Failed to tokenize text.""" | |
| def __init__(self, message: str = "Failed to calculate token count"): | |
| super().__init__(message) | |
| class ModelLoadError(InternalError): | |
| """Failed to load model.""" | |
| def __init__(self, model_name: str, reason: str | None = None): | |
| message = f"Failed to load model '{model_name}'" | |
| if reason: | |
| message += f": {reason}" | |
| super().__init__(message) | |
| self.model_name = model_name | |
| class PromptInjectionError(BadRequestError): | |
| """Potential prompt injection detected.""" | |
| def __init__(self, field: str = "input"): | |
| super().__init__( | |
| f"Request rejected: potentially harmful content detected in {field}", | |
| details=[ | |
| ErrorDetail( | |
| field=field, | |
| message="Content appears to contain prompt injection attempt", | |
| code="prompt_injection", | |
| ) | |
| ], | |
| ) | |
| class ResourceExhaustedError(APIError): | |
| """Resource limit exceeded.""" | |
| def __init__(self, resource: str, limit: int): | |
| super().__init__( | |
| ErrorCode.RESOURCE_EXHAUSTED, | |
| f"Resource limit exceeded for {resource}. Maximum allowed: {limit}", | |
| 429, | |
| ) | |
| self.resource = resource | |
| self.limit = limit | |
| class AuthLockoutError(APIError): | |
| """Account temporarily locked due to too many failed attempts.""" | |
| def __init__(self, retry_after_seconds: int): | |
| super().__init__( | |
| ErrorCode.FORBIDDEN, | |
| f"Account temporarily locked due to too many failed authentication attempts. " | |
| f"Please try again in {retry_after_seconds} seconds.", | |
| 403, | |
| ) | |
| self.retry_after_seconds = retry_after_seconds | |
| # ============================================================================ | |
| # Error Response Builder | |
| # ============================================================================ | |
| def build_error_response( | |
| request: Request, | |
| status_code: int, | |
| code: ErrorCode, | |
| message: str, | |
| details: list[dict[str, Any]] | None = None, | |
| documentation_url: str | None = None, | |
| ) -> JSONResponse: | |
| """Build a standardized error response.""" | |
| request_id = getattr(request.state, "request_id", "unknown") | |
| response_body: dict[str, Any] = { | |
| "error": { | |
| "code": code.value, | |
| "message": message, | |
| }, | |
| "request_id": request_id, | |
| "timestamp": datetime.utcnow().isoformat() + "Z", | |
| "path": str(request.url.path), | |
| } | |
| if details: | |
| response_body["error"]["details"] = details | |
| if documentation_url: | |
| response_body["documentation_url"] = documentation_url | |
| return JSONResponse(status_code=status_code, content=response_body) | |
| # ============================================================================ | |
| # Exception Handlers | |
| # ============================================================================ | |
| async def api_error_handler(request: Request, exc: APIError) -> JSONResponse: | |
| """Handle APIError exceptions.""" | |
| request_id = getattr(request.state, "request_id", "unknown") | |
| # Log the error | |
| if exc.status_code >= 500: | |
| logger.error( | |
| f"API error: {exc.code.value} - {exc.message}", | |
| extra={ | |
| "request_id": request_id, | |
| "path": request.url.path, | |
| "status_code": exc.status_code, | |
| "error_code": exc.code.value, | |
| }, | |
| exc_info=exc.internal_error, | |
| ) | |
| else: | |
| logger.warning( | |
| f"API error: {exc.code.value} - {exc.message}", | |
| extra={ | |
| "request_id": request_id, | |
| "path": request.url.path, | |
| "status_code": exc.status_code, | |
| "error_code": exc.code.value, | |
| }, | |
| ) | |
| response = exc.to_response(request_id, str(request.url.path)) | |
| headers = {} | |
| if isinstance(exc, RateLimitError): | |
| headers["Retry-After"] = str(exc.retry_after) | |
| return JSONResponse( | |
| status_code=exc.status_code, | |
| content=response, | |
| headers=headers, | |
| ) | |
| async def http_exception_handler(request: Request, exc: HTTPException) -> JSONResponse: | |
| """Handle FastAPI HTTPException.""" | |
| # Map status codes to error codes | |
| status_to_code = { | |
| 400: ErrorCode.BAD_REQUEST, | |
| 401: ErrorCode.UNAUTHORIZED, | |
| 403: ErrorCode.FORBIDDEN, | |
| 404: ErrorCode.NOT_FOUND, | |
| 405: ErrorCode.METHOD_NOT_ALLOWED, | |
| 409: ErrorCode.CONFLICT, | |
| 422: ErrorCode.VALIDATION_ERROR, | |
| 429: ErrorCode.RATE_LIMIT_EXCEEDED, | |
| 500: ErrorCode.INTERNAL_ERROR, | |
| 503: ErrorCode.SERVICE_UNAVAILABLE, | |
| } | |
| code = status_to_code.get(exc.status_code, ErrorCode.INTERNAL_ERROR) | |
| message = str(exc.detail) if exc.detail else "An error occurred" | |
| response = build_error_response( | |
| request=request, | |
| status_code=exc.status_code, | |
| code=code, | |
| message=message, | |
| ) | |
| if exc.headers: | |
| response.headers.update(exc.headers) | |
| return response | |
| async def starlette_http_exception_handler( | |
| request: Request, exc: StarletteHTTPException | |
| ) -> JSONResponse: | |
| """Handle Starlette HTTPException (e.g. 404/405 for missing routes).""" | |
| status_to_code = { | |
| 400: ErrorCode.BAD_REQUEST, | |
| 401: ErrorCode.UNAUTHORIZED, | |
| 403: ErrorCode.FORBIDDEN, | |
| 404: ErrorCode.NOT_FOUND, | |
| 405: ErrorCode.METHOD_NOT_ALLOWED, | |
| 409: ErrorCode.CONFLICT, | |
| 422: ErrorCode.VALIDATION_ERROR, | |
| 429: ErrorCode.RATE_LIMIT_EXCEEDED, | |
| 500: ErrorCode.INTERNAL_ERROR, | |
| 503: ErrorCode.SERVICE_UNAVAILABLE, | |
| } | |
| code = status_to_code.get(exc.status_code, ErrorCode.INTERNAL_ERROR) | |
| message = str(exc.detail) if exc.detail else "An error occurred" | |
| response = build_error_response( | |
| request=request, | |
| status_code=exc.status_code, | |
| code=code, | |
| message=message, | |
| ) | |
| if exc.headers: | |
| response.headers.update(exc.headers) | |
| return response | |
| async def validation_exception_handler( | |
| request: Request, exc: RequestValidationError | |
| ) -> JSONResponse: | |
| """Handle Pydantic validation errors.""" | |
| request_id = getattr(request.state, "request_id", "unknown") | |
| # Convert Pydantic errors to our format | |
| details = [] | |
| for error in exc.errors(): | |
| field_path = ".".join(str(loc) for loc in error["loc"]) | |
| details.append( | |
| { | |
| "field": field_path, | |
| "message": error["msg"], | |
| "code": error["type"], | |
| } | |
| ) | |
| logger.warning( | |
| "Validation error", | |
| extra={ | |
| "request_id": request_id, | |
| "path": request.url.path, | |
| "errors": details, | |
| }, | |
| ) | |
| return build_error_response( | |
| request=request, | |
| status_code=422, | |
| code=ErrorCode.VALIDATION_ERROR, | |
| message="Request validation failed", | |
| details=details, | |
| ) | |
| async def unhandled_exception_handler(request: Request, exc: Exception) -> JSONResponse: | |
| """Handle unexpected exceptions with specific mapping for known types.""" | |
| import asyncio | |
| request_id = getattr(request.state, "request_id", "unknown") | |
| exc_type = type(exc).__name__ | |
| # Map specific exception types to appropriate error responses | |
| # These are common Python exceptions that should have meaningful HTTP mappings | |
| # Timeout errors -> 408 Request Timeout / 504 Gateway Timeout | |
| if isinstance(exc, (asyncio.TimeoutError, TimeoutError)): | |
| logger.warning( | |
| f"Timeout error: {exc}", | |
| extra={"request_id": request_id, "path": request.url.path}, | |
| ) | |
| return build_error_response( | |
| request=request, | |
| status_code=408, | |
| code=ErrorCode.TIMEOUT, | |
| message="Request timed out. Please try again.", | |
| ) | |
| # Value errors -> 400 Bad Request | |
| if isinstance(exc, ValueError): | |
| logger.warning( | |
| f"ValueError: {exc}", | |
| extra={"request_id": request_id, "path": request.url.path}, | |
| ) | |
| return build_error_response( | |
| request=request, | |
| status_code=400, | |
| code=ErrorCode.BAD_REQUEST, | |
| message=f"Invalid value: {str(exc)[:200]}", | |
| ) | |
| # Type errors -> 400 Bad Request | |
| if isinstance(exc, TypeError): | |
| logger.warning( | |
| f"TypeError: {exc}", | |
| extra={"request_id": request_id, "path": request.url.path}, | |
| ) | |
| return build_error_response( | |
| request=request, | |
| status_code=400, | |
| code=ErrorCode.BAD_REQUEST, | |
| message="Invalid request data type", | |
| ) | |
| # Key errors (missing data) -> 400 Bad Request or 404 Not Found | |
| if isinstance(exc, KeyError): | |
| logger.warning( | |
| f"KeyError: {exc}", | |
| extra={"request_id": request_id, "path": request.url.path}, | |
| ) | |
| return build_error_response( | |
| request=request, | |
| status_code=400, | |
| code=ErrorCode.BAD_REQUEST, | |
| message=f"Missing required field: {str(exc)[:100]}", | |
| ) | |
| # Permission/OS errors -> 500 with specific message | |
| if isinstance(exc, PermissionError): | |
| logger.error( | |
| f"PermissionError: {exc}", | |
| extra={"request_id": request_id, "path": request.url.path}, | |
| exc_info=True, | |
| ) | |
| return build_error_response( | |
| request=request, | |
| status_code=500, | |
| code=ErrorCode.INTERNAL_ERROR, | |
| message="Server configuration error. Please contact support.", | |
| ) | |
| # Connection errors -> 503 Service Unavailable | |
| if isinstance(exc, (ConnectionError, ConnectionRefusedError, ConnectionResetError)): | |
| logger.error( | |
| f"Connection error: {exc}", | |
| extra={"request_id": request_id, "path": request.url.path}, | |
| exc_info=True, | |
| ) | |
| return build_error_response( | |
| request=request, | |
| status_code=503, | |
| code=ErrorCode.SERVICE_UNAVAILABLE, | |
| message="Unable to connect to required service. Please try again later.", | |
| ) | |
| # Memory errors -> 503 Service Unavailable | |
| if isinstance(exc, MemoryError): | |
| logger.critical( | |
| f"MemoryError: {exc}", | |
| extra={"request_id": request_id, "path": request.url.path}, | |
| exc_info=True, | |
| ) | |
| return build_error_response( | |
| request=request, | |
| status_code=503, | |
| code=ErrorCode.SERVICE_UNAVAILABLE, | |
| message="Server is experiencing high load. Please try again later.", | |
| ) | |
| # FileNotFoundError -> 404 or 500 depending on context | |
| if isinstance(exc, FileNotFoundError): | |
| logger.error( | |
| f"FileNotFoundError: {exc}", | |
| extra={"request_id": request_id, "path": request.url.path}, | |
| ) | |
| return build_error_response( | |
| request=request, | |
| status_code=500, | |
| code=ErrorCode.INTERNAL_ERROR, | |
| message="Required resource not found on server.", | |
| ) | |
| # RuntimeError -> 500 with generic message | |
| if isinstance(exc, RuntimeError): | |
| logger.error( | |
| f"RuntimeError: {exc}", | |
| extra={"request_id": request_id, "path": request.url.path}, | |
| exc_info=True, | |
| ) | |
| return build_error_response( | |
| request=request, | |
| status_code=500, | |
| code=ErrorCode.INTERNAL_ERROR, | |
| message="An unexpected runtime error occurred. Please try again.", | |
| ) | |
| # Log the full traceback for truly unexpected exceptions | |
| logger.error( | |
| f"Unhandled exception: {exc_type}: {exc}", | |
| extra={ | |
| "request_id": request_id, | |
| "path": request.url.path, | |
| "exception_type": exc_type, | |
| }, | |
| exc_info=True, | |
| ) | |
| # Don't expose internal details in production | |
| return build_error_response( | |
| request=request, | |
| status_code=500, | |
| code=ErrorCode.INTERNAL_ERROR, | |
| message="An internal error occurred. Please try again later.", | |
| ) | |
| # ============================================================================ | |
| # Setup Function | |
| # ============================================================================ | |
| def setup_exception_handlers(app: FastAPI) -> None: | |
| """Register all exception handlers.""" | |
| app.add_exception_handler(APIError, api_error_handler) | |
| app.add_exception_handler(HTTPException, http_exception_handler) | |
| app.add_exception_handler(StarletteHTTPException, starlette_http_exception_handler) | |
| app.add_exception_handler(RequestValidationError, validation_exception_handler) | |
| app.add_exception_handler(Exception, unhandled_exception_handler) | |
| logger.info("Exception handlers configured successfully") | |
Xet Storage Details
- Size:
- 21.7 kB
- Xet hash:
- 8ae9514cfdbc964e415867fa6dc12daef91caafa8b0034cd2f87625026eaef7a
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.