"""Error handling utilities.""" from typing import Any from fastapi import HTTPException, status class AppException(Exception): """Base application exception.""" def __init__(self, message: str, details: dict[str, Any] | None = None) -> None: """Initialize exception.""" self.message = message self.details = details or {} super().__init__(self.message) class NotFoundError(AppException): """Resource not found error.""" pass class ValidationError(AppException): """Validation error.""" pass class AuthenticationError(AppException): """Authentication error.""" pass def raise_not_found(resource: str, identifier: str | int) -> None: """Raise HTTP 404 exception.""" raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"{resource} with id '{identifier}' not found", ) def raise_validation_error(message: str) -> None: """Raise HTTP 422 exception.""" raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=message, ) def raise_unauthorized(message: str = "Unauthorized") -> None: """Raise HTTP 401 exception.""" raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail=message, headers={"WWW-Authenticate": "Bearer"}, )