
- Setup project structure with FastAPI, SQLAlchemy, and SQLite - Create Todo model and database migrations - Implement CRUD API endpoints - Add error handling and validation - Update README with documentation and examples generated with BackendIM... (backend.im)
28 lines
1.0 KiB
Python
28 lines
1.0 KiB
Python
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"},
|
|
) |