50 lines
1.3 KiB
Python
50 lines
1.3 KiB
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.config import settings
|
|
|
|
|
|
app = FastAPI(
|
|
title=settings.app_name,
|
|
version="1.0.0",
|
|
)
|
|
|
|
|
|
@app.exception_handler(RequestValidationError)
|
|
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
|
errors: list[dict[str, str]] = []
|
|
|
|
for item in exc.errors():
|
|
loc = [str(part) for part in item.get("loc", []) if part not in ("body", "query", "path")]
|
|
field = ".".join(loc) if loc else "request"
|
|
message = item.get("msg", "Ошибка валидации")
|
|
message = message.replace("Value error, ", "")
|
|
message = message.replace("Value error,", "")
|
|
errors.append({"field": field, "message": message})
|
|
|
|
detail = errors[0]["message"] if errors else "Ошибка валидации запроса"
|
|
|
|
return JSONResponse(
|
|
status_code=422,
|
|
content={
|
|
"detail": detail,
|
|
"errors": errors,
|
|
},
|
|
)
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {
|
|
"service": settings.app_name,
|
|
"env": settings.app_env,
|
|
"status": "ok",
|
|
"port": settings.app_port,
|
|
"api_prefix": settings.api_v1_prefix,
|
|
}
|
|
|
|
|
|
app.include_router(router)
|