feat: 新增全局处理自定义服务异常及其他异常
This commit is contained in:
@@ -28,6 +28,16 @@ class PermissionException(Exception):
|
|||||||
self.message = message
|
self.message = message
|
||||||
|
|
||||||
|
|
||||||
|
class ServiceException(Exception):
|
||||||
|
"""
|
||||||
|
自定义服务异常ServiceException
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, data: str = None, message: str = None):
|
||||||
|
self.data = data
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
|
||||||
class ModelValidatorException(Exception):
|
class ModelValidatorException(Exception):
|
||||||
"""
|
"""
|
||||||
自定义模型校验异常ModelValidatorException
|
自定义模型校验异常ModelValidatorException
|
||||||
|
@@ -1,7 +1,8 @@
|
|||||||
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, PermissionException, ModelValidatorException
|
from exceptions.exception import AuthException, PermissionException, ServiceException, ModelValidatorException
|
||||||
|
from utils.log_util import logger
|
||||||
from utils.response_util import ResponseUtil, JSONResponse, jsonable_encoder
|
from utils.response_util import ResponseUtil, JSONResponse, jsonable_encoder
|
||||||
|
|
||||||
|
|
||||||
@@ -19,14 +20,22 @@ def handle_exception(app: FastAPI):
|
|||||||
async def permission_exception_handler(request: Request, exc: PermissionException):
|
async def permission_exception_handler(request: Request, exc: PermissionException):
|
||||||
return ResponseUtil.forbidden(data=exc.data, msg=exc.message)
|
return ResponseUtil.forbidden(data=exc.data, msg=exc.message)
|
||||||
|
|
||||||
|
# 自定义服务异常
|
||||||
|
@app.exception_handler(ServiceException)
|
||||||
|
async def service_exception_handler(request: Request, exc: ServiceException):
|
||||||
|
logger.warning(exc.message)
|
||||||
|
return ResponseUtil.failure(data=exc.data, msg=exc.message)
|
||||||
|
|
||||||
# 自定义模型检验异常
|
# 自定义模型检验异常
|
||||||
@app.exception_handler(ModelValidatorException)
|
@app.exception_handler(ModelValidatorException)
|
||||||
async def model_validator_exception_handler(request: Request, exc: ModelValidatorException):
|
async def model_validator_exception_handler(request: Request, exc: ModelValidatorException):
|
||||||
|
logger.warning(exc.message)
|
||||||
return ResponseUtil.failure(data=exc.data, msg=exc.message)
|
return ResponseUtil.failure(data=exc.data, msg=exc.message)
|
||||||
|
|
||||||
# 自定义字段检验异常
|
# 自定义字段检验异常
|
||||||
@app.exception_handler(FieldValidationError)
|
@app.exception_handler(FieldValidationError)
|
||||||
async def field_validation_error_handler(request: Request, exc: FieldValidationError):
|
async def field_validation_error_handler(request: Request, exc: FieldValidationError):
|
||||||
|
logger.warning(exc.message)
|
||||||
return ResponseUtil.failure(msg=exc.message)
|
return ResponseUtil.failure(msg=exc.message)
|
||||||
|
|
||||||
# 处理其他http请求异常
|
# 处理其他http请求异常
|
||||||
@@ -36,3 +45,9 @@ def handle_exception(app: FastAPI):
|
|||||||
content=jsonable_encoder({"code": exc.status_code, "msg": exc.detail}),
|
content=jsonable_encoder({"code": exc.status_code, "msg": exc.detail}),
|
||||||
status_code=exc.status_code
|
status_code=exc.status_code
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 处理其他异常
|
||||||
|
@app.exception_handler(Exception)
|
||||||
|
async def exception_handler(request: Request, exc: Exception):
|
||||||
|
logger.exception(exc)
|
||||||
|
return ResponseUtil.error(msg=str(exc))
|
||||||
|
Reference in New Issue
Block a user