from __future__ import annotations from typing import Any, Dict, Optional from fastapi import HTTPException, status class BaseAPIException(HTTPException): """ Base class for API exceptions. """ status_code: int detail: str headers: Optional[Dict[str, Any]] = None def __init__( self, detail: str = None, headers: Optional[Dict[str, Any]] = None, ) -> None: super().__init__( status_code=self.status_code, detail=detail or self.detail, headers=headers or self.headers, ) class NotFoundException(BaseAPIException): """ Exception raised when a resource is not found. """ status_code = status.HTTP_404_NOT_FOUND detail = "Resource not found" class ForbiddenException(BaseAPIException): """ Exception raised when a user doesn't have permission to access a resource. """ status_code = status.HTTP_403_FORBIDDEN detail = "Not enough permissions" class UnauthorizedException(BaseAPIException): """ Exception raised when authentication fails. """ status_code = status.HTTP_401_UNAUTHORIZED detail = "Authentication failed" headers = {"WWW-Authenticate": "Bearer"} class BadRequestException(BaseAPIException): """ Exception raised for invalid requests. """ status_code = status.HTTP_400_BAD_REQUEST detail = "Invalid request" class ConflictException(BaseAPIException): """ Exception raised when there's a conflict with the current state of the resource. """ status_code = status.HTTP_409_CONFLICT detail = "Conflict with current state" class ValidationException(BadRequestException): """ Exception raised for validation errors. """ detail = "Validation error"