
- 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
23 lines
908 B
Python
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)}"},
|
|
) |