6
This commit is contained in:
@@ -114,12 +114,24 @@ async def app_error_handler(_: Request, exc: AppError) -> JSONResponse:
|
||||
|
||||
|
||||
async def validation_error_handler(_: Request, exc: RequestValidationError) -> JSONResponse:
|
||||
# 序列化错误详情,过滤不可 JSON 序列化的对象
|
||||
errors = []
|
||||
for err in exc.errors():
|
||||
clean_err = {
|
||||
"type": err.get("type", ""),
|
||||
"loc": err.get("loc", []),
|
||||
"msg": err.get("msg", ""),
|
||||
}
|
||||
if "input" in err:
|
||||
clean_err["input"] = str(err["input"])
|
||||
errors.append(clean_err)
|
||||
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
content={
|
||||
"code": "VALIDATION_ERROR",
|
||||
"message": "请求参数不正确。",
|
||||
"detail": exc.errors(),
|
||||
"detail": errors,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user