[英]How can I overwrite the default behavior of pydantic validation errors in FastAPI?
I want to overwrite the default behavior of validation errors to go from outputting something like:我想覆盖验证错误的默认行为,以输出如下内容:
{
"detail": [
{
"loc": [
"query",
"email"
],
"msg": "value is not a valid email address",
"type": "value_error.email"
}
]
}
to至
{
"type": "/errors/unprocessable_entity",
"title": "Unprocessable Entity",
"status": 422,
"detail": "The request is invalid.",
"instance": "/api/test/email",
"issues": [
{
"loc": [
"query",
"email"
],
"msg": "value is not a valid email address",
"type": "value_error.email"
}
]
}
I tried using an API exception handler like:我尝试使用 API 异常处理程序,例如:
from fastapi import FastAPI, Query
from fastapi.responses import JSONResponse
from pydantic import EmailStr, error_wrappers
app = FastAPI()
@app.get("/api/test/email")
async def test_email(email: EmailStr = Query(...)):
return "Success"
@app.exception_handler(error_wrappers.ValidationError)
def format_validation_error_as_rfc_7807_problem_json(request: Request, exc: error_wrappers.ValidationError):
content = {
"type": f"/errors/unprocessable_entity",
"title": "Unprocessable Entity",
"status": exc.status_code,
"detail": "The request is invalid.",
"instance": request.url.path,
"issues": exc.errors()
}
return JSONResponse(**content, status_code=exc.status_code
However the function format_validation_error_as_rfc_7807_problem_json
is never called when you enter an invalid email.但是,当您输入无效的电子邮件时,永远不会调用函数format_validation_error_as_rfc_7807_problem_json
。
Was simply using the wrong exception handler, instead, do:只是使用了错误的异常处理程序,而是执行以下操作:
from fastapi.exceptions import RequestValidationError
@api.exception_handler(RequestValidationError)
async def format_validation_error_as_rfc_7807_problem_json(request: Request, exc: error_wrappers.ValidationError):
status_code = status.HTTP_422_UNPROCESSABLE_ENTITY
content = {
"type": f"/errors/unprocessable_entity",
"title": "Unprocessable Entity",
"status": status_code,
"detail": "The request is invalid.",
"instance": request.url.path,
"issues": jsonable_encoder(exc.errors()),
}
return JSONResponse(content, status_code=status_code)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.