added vegetation
This commit is contained in:
+27
-1
@@ -1,4 +1,7 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
|
||||
from app.config import get_settings
|
||||
from app.routers import api_router
|
||||
@@ -16,6 +19,29 @@ def create_app() -> FastAPI:
|
||||
def health() -> dict[str, str]:
|
||||
return {"status": "ok"}
|
||||
|
||||
@app.exception_handler(StarletteHTTPException)
|
||||
def http_exception_handler(_request: Request, exc: StarletteHTTPException) -> JSONResponse:
|
||||
detail = exc.detail
|
||||
if not (isinstance(detail, dict) and "code" in detail and "detail" in detail):
|
||||
detail = {"code": "HTTP_ERROR", "detail": detail}
|
||||
return JSONResponse(status_code=exc.status_code, content={"detail": detail})
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
def validation_exception_handler(
|
||||
_request: Request,
|
||||
exc: RequestValidationError,
|
||||
) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
status_code=422,
|
||||
content={
|
||||
"detail": {
|
||||
"code": "VALIDATION_ERROR",
|
||||
"detail": "Request validation failed",
|
||||
"errors": exc.errors(),
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
app.include_router(api_router, prefix=settings.api_v1_prefix)
|
||||
return app
|
||||
|
||||
|
||||
Reference in New Issue
Block a user