feat: 新增FieldValidatorException全局异常处理

This commit is contained in:
insistence
2024-07-04 09:54:49 +08:00
parent 2f3e5171b0
commit 549948ba37

View File

@@ -1,6 +1,6 @@
from fastapi import FastAPI, Request from fastapi import FastAPI, Request
from fastapi.exceptions import HTTPException from fastapi.exceptions import HTTPException
from exceptions.exception import AuthException, PermissionException, ModelValidatorException from exceptions.exception import AuthException, PermissionException, ModelValidatorException, FieldValidatorException
from utils.response_util import ResponseUtil, JSONResponse, jsonable_encoder from utils.response_util import ResponseUtil, JSONResponse, jsonable_encoder
@@ -23,6 +23,11 @@ def handle_exception(app: FastAPI):
async def model_validator_exception_handler(request: Request, exc: ModelValidatorException): async def model_validator_exception_handler(request: Request, exc: ModelValidatorException):
return ResponseUtil.failure(data=exc.data, msg=exc.message) return ResponseUtil.failure(data=exc.data, msg=exc.message)
# 自定义模型检验异常
@app.exception_handler(FieldValidatorException)
async def field_validator_exception_handler(request: Request, exc: FieldValidatorException):
return ResponseUtil.failure(data=exc.data, msg=exc.message)
# 处理其他http请求异常 # 处理其他http请求异常
@app.exception_handler(HTTPException) @app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException): async def http_exception_handler(request: Request, exc: HTTPException):