from fastapi import Request, status from fastapi.responses import JSONResponse from fastapi.exceptions import RequestValidationError from sqlalchemy.exc import SQLAlchemyError from app.core.exceptions import TodoNotFoundException, ValidationError, DatabaseError async def exception_handler(request: Request, exc: Exception): if isinstance(exc, TodoNotFoundException): return JSONResponse( status_code=exc.status_code, content={"detail": exc.detail}, ) elif isinstance(exc, RequestValidationError): return JSONResponse( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, content={"detail": str(exc)}, ) elif isinstance(exc, SQLAlchemyError): return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={"detail": "Database error occurred"}, ) else: return JSONResponse( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, content={"detail": "An unexpected error occurred"}, )