from fastapi import HTTPException from typing import Any, Dict, Optional class CustomException(HTTPException): """Base custom exception class""" def __init__( self, status_code: int, detail: str, error_code: str = "ERROR", headers: Optional[Dict[str, Any]] = None ): self.status_code = status_code self.detail = detail self.error_code = error_code self.headers = headers class ModelLoadException(CustomException): """Raised when model loading fails""" def __init__(self, detail: str = "Failed to load model"): super().__init__( status_code=500, detail=detail, error_code="MODEL_LOAD_ERROR" ) class PredictionException(CustomException): """Raised when prediction fails""" def __init__(self, detail: str = "Failed to make prediction"): super().__init__( status_code=500, detail=detail, error_code="PREDICTION_ERROR" ) class RateLimitException(CustomException): """Raised when rate limit is exceeded""" def __init__(self, detail: str = "Rate limit exceeded"): super().__init__( status_code=429, detail=detail, error_code="RATE_LIMIT_EXCEEDED", headers={"Retry-After": "3600"} ) class ValidationException(CustomException): """Raised when input validation fails""" def __init__(self, detail: str = "Validation error"): super().__init__( status_code=422, detail=detail, error_code="VALIDATION_ERROR" )