From 13cfc607824d7d90a55e2df9a82f0a7d9d42d2db Mon Sep 17 00:00:00 2001 From: insistence <3055204202@qq.com> Date: Fri, 12 Jul 2024 11:21:37 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E4=BD=BF=E7=94=A8ruff=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96exceptions=E6=A8=A1=E5=9D=97=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-fastapi-backend/exceptions/handle.py | 39 +++++++++++++--------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/ruoyi-fastapi-backend/exceptions/handle.py b/ruoyi-fastapi-backend/exceptions/handle.py index 5895586..879019c 100644 --- a/ruoyi-fastapi-backend/exceptions/handle.py +++ b/ruoyi-fastapi-backend/exceptions/handle.py @@ -1,15 +1,23 @@ from fastapi import FastAPI, Request from fastapi.exceptions import HTTPException from pydantic_validation_decorator import FieldValidationError -from exceptions.exception import AuthException, LoginException, PermissionException, ServiceException, ServiceWarning, ModelValidatorException +from exceptions.exception import ( + AuthException, + LoginException, + ModelValidatorException, + PermissionException, + ServiceException, + ServiceWarning, +) from utils.log_util import logger -from utils.response_util import ResponseUtil, JSONResponse, jsonable_encoder +from utils.response_util import jsonable_encoder, JSONResponse, ResponseUtil def handle_exception(app: FastAPI): """ 全局异常处理 """ + # 自定义token检验异常 @app.exception_handler(AuthException) async def auth_exception_handler(request: Request, exc: AuthException): @@ -20,6 +28,18 @@ def handle_exception(app: FastAPI): async def login_exception_handler(request: Request, exc: LoginException): return ResponseUtil.failure(data=exc.data, msg=exc.message) + # 自定义模型检验异常 + @app.exception_handler(ModelValidatorException) + async def model_validator_exception_handler(request: Request, exc: ModelValidatorException): + logger.warning(exc.message) + return ResponseUtil.failure(data=exc.data, msg=exc.message) + + # 自定义字段检验异常 + @app.exception_handler(FieldValidationError) + async def field_validation_error_handler(request: Request, exc: FieldValidationError): + logger.warning(exc.message) + return ResponseUtil.failure(msg=exc.message) + # 自定义权限检验异常 @app.exception_handler(PermissionException) async def permission_exception_handler(request: Request, exc: PermissionException): @@ -37,24 +57,11 @@ def handle_exception(app: FastAPI): logger.warning(exc.message) return ResponseUtil.failure(data=exc.data, msg=exc.message) - # 自定义模型检验异常 - @app.exception_handler(ModelValidatorException) - async def model_validator_exception_handler(request: Request, exc: ModelValidatorException): - logger.warning(exc.message) - return ResponseUtil.failure(data=exc.data, msg=exc.message) - - # 自定义字段检验异常 - @app.exception_handler(FieldValidationError) - async def field_validation_error_handler(request: Request, exc: FieldValidationError): - logger.warning(exc.message) - return ResponseUtil.failure(msg=exc.message) - # 处理其他http请求异常 @app.exception_handler(HTTPException) async def http_exception_handler(request: Request, exc: HTTPException): return JSONResponse( - content=jsonable_encoder({"code": exc.status_code, "msg": exc.detail}), - status_code=exc.status_code + content=jsonable_encoder({'code': exc.status_code, 'msg': exc.detail}), status_code=exc.status_code ) # 处理其他异常