afp-backend / app /api /errors.py
cdupland
Add FastAPI application structure with Prisma integration, including API endpoints, services, repositories, and database setup. Introduce strict layering rules, documentation, and testing strategy.
0ddf928
from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from app.exceptions.domain import BusinessRuleError, ConflictError, NotFoundError
def register_exception_handlers(app: FastAPI) -> None:
@app.exception_handler(NotFoundError)
async def handle_not_found(_: Request, exc: NotFoundError) -> JSONResponse:
return JSONResponse(status_code=404, content={"detail": str(exc)})
@app.exception_handler(ConflictError)
async def handle_conflict(_: Request, exc: ConflictError) -> JSONResponse:
return JSONResponse(status_code=409, content={"detail": str(exc)})
@app.exception_handler(BusinessRuleError)
async def handle_business_rule(_: Request, exc: BusinessRuleError) -> JSONResponse:
return JSONResponse(status_code=422, content={"detail": str(exc)})
@app.exception_handler(RequestValidationError)
async def handle_validation(_: Request, exc: RequestValidationError) -> JSONResponse:
return JSONResponse(status_code=400, content={"detail": exc.errors()})