22 lines
837 B
Python

from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from app.api.routes import router
from app.core.errors import ApiError
def create_app() -> FastAPI:
app = FastAPI(title="Ubuntu User Manage API", version="1.0.0")
app.include_router(router)
@app.exception_handler(ApiError)
async def api_error_handler(_: Request, exc: ApiError) -> JSONResponse:
return JSONResponse(status_code=exc.status_code, content={"code": exc.code, "message": exc.message})
@app.exception_handler(RequestValidationError)
async def validation_error_handler(_: Request, exc: RequestValidationError) -> JSONResponse:
return JSONResponse(status_code=400, content={"code": "invalid_parameter", "message": str(exc.errors())})
return app