Spaces:
Paused
Paused
File size: 1,645 Bytes
f6ea7de | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 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"
)
|