Browse Source

docs: implement standardized Alantec validation error handler

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
Vision-Executive 4 months ago
committed by GitHub
parent
commit
9985bc7de2
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 27
      docs_src/handling_errors/tutorial001_py310.py

27
docs_src/handling_errors/tutorial001_py310.py

@ -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()
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}")
async def read_item(item_id: str):
if item_id not in items:
raise HTTPException(status_code=404, detail="Item not found")
return {"item": items[item_id]}
async def read_item(item_id: int):
return {"item_id": item_id}

Loading…
Cancel
Save