From 47d5697653f351ebfe1d7153b0c9e4aff8c50806 Mon Sep 17 00:00:00 2001 From: insistence <3055204202@qq.com> Date: Thu, 11 Jul 2024 16:47:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=A4=84=E7=90=86=E8=87=AA=E5=AE=9A=E4=B9=89=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-fastapi-backend/exceptions/exception.py | 10 ++++++++++ ruoyi-fastapi-backend/exceptions/handle.py | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ruoyi-fastapi-backend/exceptions/exception.py b/ruoyi-fastapi-backend/exceptions/exception.py index 0a94948..b86f50d 100644 --- a/ruoyi-fastapi-backend/exceptions/exception.py +++ b/ruoyi-fastapi-backend/exceptions/exception.py @@ -38,6 +38,16 @@ class ServiceException(Exception): self.message = message +class ServiceWarning(Exception): + """ + 自定义服务警告ServiceWarning + """ + + def __init__(self, data: str = None, message: str = None): + self.data = data + self.message = message + + class ModelValidatorException(Exception): """ 自定义模型校验异常ModelValidatorException diff --git a/ruoyi-fastapi-backend/exceptions/handle.py b/ruoyi-fastapi-backend/exceptions/handle.py index 2b18687..5895586 100644 --- a/ruoyi-fastapi-backend/exceptions/handle.py +++ b/ruoyi-fastapi-backend/exceptions/handle.py @@ -1,7 +1,7 @@ from fastapi import FastAPI, Request from fastapi.exceptions import HTTPException from pydantic_validation_decorator import FieldValidationError -from exceptions.exception import AuthException, LoginException, PermissionException, ServiceException, ModelValidatorException +from exceptions.exception import AuthException, LoginException, PermissionException, ServiceException, ServiceWarning, ModelValidatorException from utils.log_util import logger from utils.response_util import ResponseUtil, JSONResponse, jsonable_encoder @@ -28,6 +28,12 @@ def handle_exception(app: FastAPI): # 自定义服务异常 @app.exception_handler(ServiceException) async def service_exception_handler(request: Request, exc: ServiceException): + logger.warning(exc.message) + return ResponseUtil.error(data=exc.data, msg=exc.message) + + # 自定义服务警告 + @app.exception_handler(ServiceWarning) + async def service_warning_handler(request: Request, exc: ServiceWarning): logger.warning(exc.message) return ResponseUtil.failure(data=exc.data, msg=exc.message)