Spaces:
Running
Running
File size: 2,475 Bytes
09801ca | 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | """
Custom Exception Classes — Consistent error handling across the application.
"""
from fastapi import HTTPException, status
class DataVisionException(Exception):
"""Base exception for all DataVision errors."""
def __init__(self, message: str, error_code: str = "INTERNAL_ERROR"):
self.message = message
self.error_code = error_code
super().__init__(message)
class AuthenticationError(DataVisionException):
"""Raised when authentication fails."""
def __init__(self, message: str = "Authentication failed"):
super().__init__(message, error_code="AUTH_FAILED")
class AuthorizationError(DataVisionException):
"""Raised when user lacks required permissions."""
def __init__(self, message: str = "Insufficient permissions"):
super().__init__(message, error_code="FORBIDDEN")
class NotFoundError(DataVisionException):
"""Raised when a resource is not found."""
def __init__(self, resource: str, identifier: str = ""):
msg = f"{resource} not found"
if identifier:
msg = f"{resource} '{identifier}' not found"
super().__init__(msg, error_code="NOT_FOUND")
class ConflictError(DataVisionException):
"""Raised when a resource already exists."""
def __init__(self, message: str = "Resource already exists"):
super().__init__(message, error_code="CONFLICT")
class ValidationError(DataVisionException):
"""Raised when input validation fails."""
def __init__(self, message: str = "Validation failed"):
super().__init__(message, error_code="VALIDATION_ERROR")
class RateLimitError(DataVisionException):
"""Raised when rate limit is exceeded."""
def __init__(self, message: str = "Rate limit exceeded"):
super().__init__(message, error_code="RATE_LIMIT")
def to_http_exception(exc: DataVisionException) -> HTTPException:
"""Convert a DataVisionException to an HTTPException."""
status_map = {
"AUTH_FAILED": status.HTTP_401_UNAUTHORIZED,
"FORBIDDEN": status.HTTP_403_FORBIDDEN,
"NOT_FOUND": status.HTTP_404_NOT_FOUND,
"CONFLICT": status.HTTP_409_CONFLICT,
"VALIDATION_ERROR": status.HTTP_422_UNPROCESSABLE_ENTITY,
"RATE_LIMIT": status.HTTP_429_TOO_MANY_REQUESTS,
"INTERNAL_ERROR": status.HTTP_500_INTERNAL_SERVER_ERROR,
}
return HTTPException(
status_code=status_map.get(exc.error_code, 500),
detail=exc.message,
)
|