Download exceptions.py from Atomio/facerecog: direct link, hf CLI and curl.
- Browser
- Download file 4.81 kB
-
https://huggingface.co/spaces/Atomio/facerecog/resolve/main/exceptions.py
- Command line
-
hf download hf://spaces/Atomio/facerecog/exceptions.py
-
curl -L -o exceptions.py https://huggingface.co/spaces/Atomio/facerecog/resolve/main/exceptions.py
4.81 kB
| """Global exception classes and handlers.""" | |
| from datetime import datetime, timezone | |
| from typing import Any | |
| from fastapi import HTTPException, Request, status | |
| from fastapi.responses import JSONResponse | |
| from models import ErrorResponse | |
| class BaseAPIException(HTTPException): | |
| """Base exception class for API errors.""" | |
| def __init__( | |
| self, | |
| status_code: int, | |
| message: str, | |
| details: dict[str, Any] | None = None, | |
| headers: dict[str, str] | None = None, | |
| ) -> None: | |
| self.message = message | |
| self.details = details | |
| super().__init__(status_code=status_code, detail=message, headers=headers) | |
| class ValidationError(BaseAPIException): | |
| """Validation error exception.""" | |
| def __init__( | |
| self, | |
| message: str = "Validation failed", | |
| details: dict[str, Any] | None = None, | |
| ) -> None: | |
| super().__init__( | |
| status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | |
| message=message, | |
| details=details, | |
| ) | |
| class NotFoundError(BaseAPIException): | |
| """Resource not found exception.""" | |
| def __init__( | |
| self, | |
| message: str = "Resource not found", | |
| details: dict[str, Any] | None = None, | |
| ) -> None: | |
| super().__init__( | |
| status_code=status.HTTP_404_NOT_FOUND, | |
| message=message, | |
| details=details, | |
| ) | |
| class ConflictError(BaseAPIException): | |
| """Resource conflict exception.""" | |
| def __init__( | |
| self, | |
| message: str = "Resource conflict", | |
| details: dict[str, Any] | None = None, | |
| ) -> None: | |
| super().__init__( | |
| status_code=status.HTTP_409_CONFLICT, | |
| message=message, | |
| details=details, | |
| ) | |
| class UnauthorizedError(BaseAPIException): | |
| """Unauthorized access exception.""" | |
| def __init__( | |
| self, | |
| message: str = "Unauthorized access", | |
| details: dict[str, Any] | None = None, | |
| ) -> None: | |
| super().__init__( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| message=message, | |
| details=details, | |
| headers={"WWW-Authenticate": "Bearer"}, | |
| ) | |
| class ForbiddenError(BaseAPIException): | |
| """Forbidden access exception.""" | |
| def __init__( | |
| self, | |
| message: str = "Forbidden access", | |
| details: dict[str, Any] | None = None, | |
| ) -> None: | |
| super().__init__( | |
| status_code=status.HTTP_403_FORBIDDEN, | |
| message=message, | |
| details=details, | |
| ) | |
| class InternalServerError(BaseAPIException): | |
| """Internal server error exception.""" | |
| def __init__( | |
| self, | |
| message: str = "Internal server error", | |
| details: dict[str, Any] | None = None, | |
| ) -> None: | |
| super().__init__( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| message=message, | |
| details=details, | |
| ) | |
| class BadRequestError(BaseAPIException): | |
| """Bad request exception.""" | |
| def __init__( | |
| self, | |
| message: str = "Bad request", | |
| details: dict[str, Any] | None = None, | |
| ) -> None: | |
| super().__init__( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| message=message, | |
| details=details, | |
| ) | |
| class ServiceUnavailableError(BaseAPIException): | |
| """Service unavailable exception.""" | |
| def __init__( | |
| self, | |
| message: str = "Service temporarily unavailable", | |
| details: dict[str, Any] | None = None, | |
| ) -> None: | |
| super().__init__( | |
| status_code=status.HTTP_503_SERVICE_UNAVAILABLE, | |
| message=message, | |
| details=details, | |
| ) | |
| async def base_api_exception_handler( | |
| request: Request, exc: BaseAPIException | |
| ) -> JSONResponse: | |
| """Handle BaseAPIException and return structured error response.""" | |
| error_response = ErrorResponse( | |
| error=exc.__class__.__name__, | |
| message=exc.message, | |
| details=exc.details, | |
| timestamp=datetime.now(timezone.utc), | |
| path=str(request.url.path), | |
| ) | |
| return JSONResponse( | |
| status_code=exc.status_code, | |
| content=error_response.model_dump(), | |
| headers=exc.headers, | |
| ) | |
| async def general_exception_handler(request: Request, exc: Exception) -> JSONResponse: | |
| """Handle general exceptions and return structured error response.""" | |
| error_response = ErrorResponse( | |
| error="InternalServerError", | |
| message="An unexpected error occurred", | |
| details={"exception_type": exc.__class__.__name__}, | |
| timestamp=datetime.now(timezone.utc), | |
| path=str(request.url.path), | |
| ) | |
| return JSONResponse( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| content=error_response.model_dump(), | |
| ) | |