Browse Source

feat: 新增全局处理自定义服务警告

master
insistence 7 months ago
parent
commit
47d5697653
  1. 10
      ruoyi-fastapi-backend/exceptions/exception.py
  2. 8
      ruoyi-fastapi-backend/exceptions/handle.py

10
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

8
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)

Loading…
Cancel
Save