from fastapi import FastAPI, Request, status from fastapi.responses import JSONResponse from sqlalchemy.exc import SQLAlchemyError from app.core.exceptions import TaskException def add_exception_handlers(app: FastAPI) -> None: """Add global exception handlers to the FastAPI app.""" @app.exception_handler(TaskException) async def task_exception_handler(request: Request, exc: TaskException): """Handle Task API exceptions.""" return JSONResponse( status_code=exc.status_code, content={"detail": exc.detail}, ) @app.exception_handler(SQLAlchemyError) async def sqlalchemy_exception_handler(request: Request, exc: SQLAlchemyError): """Handle SQLAlchemy exceptions.""" return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={"detail": "Database error occurred"}, ) @app.exception_handler(Exception) async def general_exception_handler(request: Request, exc: Exception): """Handle general exceptions.""" return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={"detail": "An unexpected error occurred"}, )