This commit is contained in:
2025-11-20 11:42:18 +08:00
parent 1bd91df9a1
commit e2d2b0b75b
29 changed files with 2100 additions and 48 deletions

47
back/utils/exceptions.py Normal file
View File

@@ -0,0 +1,47 @@
import os
from fastapi import Request, status
from fastapi.exceptions import HTTPException, RequestValidationError
from fastapi.responses import JSONResponse
from .logs import getLogger
logger = getLogger(os.environ.get('APP_NAME'))
def global_http_exception_handler(request: Request, exc):
"""
全局HTTP请求处理异常
:param request: HTTP请求对象
:param exc: 本次发生的异常对象
:return:
"""
# 使用日志记录异常
logger.error(f"发生异常:{exc.detail}")
# 直接返回JSONResponse避免重新抛出异常导致循环
return JSONResponse(
status_code=exc.status_code,
content={
'err_msg': exc.detail,
'status': False
},
headers=getattr(exc, 'headers', None)
)
def global_request_exception_handler(request: Request, exc):
"""
全局请求校验异常处理函数
:param request: HTTP请求对象
:param exc: 本次发生的异常对象
:return:
"""
# 直接返回JSONResponse避免重新抛出异常
return JSONResponse(
status_code=status.HTTP_400_BAD_REQUEST,
content={
'err_msg': exc.errors()[0],
'status': False
}
)