{ "detail":[ { "loc":[ "body", "age" ], "msg":"ensure this value is greater than 18", "type":"value_error.number.not_gt", "ctx":{ "limit_value":18 } }, { "loc":[ "body", "likes" ], "msg":"ensure this value has at least 2 items", "type":"value_error.list.min_items", "ctx":{ "limit_value":2 } } ] }
from fastapi import Request, status from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse from app.types import response from fastapi.encoders import jsonable_encoder
from fastapi import status from fastapi.encoders import jsonable_encoder from fastapi.responses import JSONResponse from starlette.exceptions import HTTPException from app.types import response
from fastapi import FastAPI from .validation_error import validationExceptionHandler from .http_error import httpExceptionHandler from fastapi.exceptions import RequestValidationError from starlette.exceptions import HTTPException as StarletteHTTPException
from .validate_template_config import validateChineseDict, keyErrorChineseDict
3.3 改造自定义处理器
修改文件app/errors/validation_error.py,修改后内容如下:
from fastapi import Request, status from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse from app.types import response from app import config from fastapi.encoders import jsonable_encoder
deftranslate(fieldName: str, errType: str, limitDict: dict) -> str: """ 翻译错误信息""" # 先判断是否满足关键词错误 for k, v in config.keyErrorChineseDict.items(): if fieldName.find(k) != -1: return v
from fastapi import status from fastapi.encoders import jsonable_encoder from fastapi.requests import Request from fastapi.responses import JSONResponse from app.types import response