Automated Action 9505ec13a1 Implement simple todo application with FastAPI and SQLite
- Set up project structure and FastAPI app
- Create database models and SQLAlchemy connection
- Implement Alembic migration scripts
- Add CRUD API endpoints for Todo items
- Add health check endpoint
- Set up validation, error handling, and middleware
- Add comprehensive documentation in README.md
2025-05-18 12:42:38 +00:00

23 lines
908 B
Python

from fastapi import FastAPI, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from sqlalchemy.exc import SQLAlchemyError
def add_error_handlers(app: FastAPI) -> None:
"""
Add global error handlers to the FastAPI application.
"""
@app.exception_handler(SQLAlchemyError)
async def sqlalchemy_exception_handler(request: Request, exc: SQLAlchemyError):
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={"detail": f"Database error: {str(exc)}"},
)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content={"detail": f"Validation error: {str(exc)}"},
)