
- Set up project structure and dependencies - Create database models and schemas for tasks - Implement CRUD operations for tasks - Add API endpoints for task management - Create Alembic migrations for the database - Add health check endpoint - Implement error handling - Add documentation in README.md
34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
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"},
|
|
)
|