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

This commit is contained in:
insistence
2024-07-11 16:47:23 +08:00
parent 0dfc201990
commit 47d5697653
2 changed files with 17 additions and 1 deletions

View File

@@ -38,6 +38,16 @@ class ServiceException(Exception):
self.message = message 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): class ModelValidatorException(Exception):
""" """
自定义模型校验异常ModelValidatorException 自定义模型校验异常ModelValidatorException

View File

@@ -1,7 +1,7 @@
from fastapi import FastAPI, Request from fastapi import FastAPI, Request
from fastapi.exceptions import HTTPException from fastapi.exceptions import HTTPException
from pydantic_validation_decorator import FieldValidationError 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.log_util import logger
from utils.response_util import ResponseUtil, JSONResponse, jsonable_encoder from utils.response_util import ResponseUtil, JSONResponse, jsonable_encoder
@@ -28,6 +28,12 @@ def handle_exception(app: FastAPI):
# 自定义服务异常 # 自定义服务异常
@app.exception_handler(ServiceException) @app.exception_handler(ServiceException)
async def service_exception_handler(request: Request, exc: 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) logger.warning(exc.message)
return ResponseUtil.failure(data=exc.data, msg=exc.message) return ResponseUtil.failure(data=exc.data, msg=exc.message)