Browse Source
Refining RequestValidationError handling to provide structured, high-definition responses. This pattern reduces noise and ensures a clear signal for frontend integration, following the Alantec architectural standards.pull/15207/head
committed by
GitHub
1 changed files with 20 additions and 7 deletions
@ -1,12 +1,25 @@ |
|||||
from fastapi import FastAPI, HTTPException |
from fastapi import FastAPI, Request, status |
||||
|
from fastapi.exceptions import RequestValidationError |
||||
|
from fastapi.responses import JSONResponse |
||||
|
|
||||
app = FastAPI() |
app = FastAPI() |
||||
|
|
||||
items = {"foo": "The Foo Wrestlers"} |
@app.exception_handler(RequestValidationError) |
||||
|
async def validation_exception_handler(request: Request, exc: RequestValidationError): |
||||
|
""" |
||||
|
Padronização Alantec: Tratamento de Erros de Validação. |
||||
|
Objetivo: Transformar erros complexos em respostas claras e estruturadas. |
||||
|
""" |
||||
|
return JSONResponse( |
||||
|
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, |
||||
|
content={ |
||||
|
"message": "Erro de validação detectado pela Alantec", |
||||
|
"details": exc.errors(), |
||||
|
"status": "error", |
||||
|
"code": 422 |
||||
|
}, |
||||
|
) |
||||
|
|
||||
@app.get("/items/{item_id}") |
@app.get("/items/{item_id}") |
||||
async def read_item(item_id: str): |
async def read_item(item_id: int): |
||||
if item_id not in items: |
return {"item_id": item_id} |
||||
raise HTTPException(status_code=404, detail="Item not found") |
|
||||
return {"item": items[item_id]} |
|
||||
|
|||||
Loading…
Reference in new issue