优化:异常处理

This commit is contained in:
ktianc 2023-11-16 13:47:07 +08:00
parent 4a9bf1fdc3
commit 21980a6e34
2 changed files with 23 additions and 17 deletions

View File

@ -11,7 +11,7 @@ from fastapi.security import OAuth2PasswordBearer
"""
系统版本
"""
VERSION = "3.2.2"
VERSION = "3.3.0"
"""安全警告: 不要在生产中打开调试运行!"""
DEBUG = False

View File

@ -13,6 +13,7 @@ from fastapi import Request
from fastapi.encoders import jsonable_encoder
from fastapi import FastAPI
from core.logger import logger
from application.settings import DEBUG
class CustomException(Exception):
@ -40,10 +41,11 @@ def register_exception(app: FastAPI):
"""
自定义异常
"""
if DEBUG:
print("请求地址", request.url.__str__())
print("捕捉到重写CustomException异常异常custom_exception_handler")
# logger.error(exc.desc)
# logger.error(exc.msg)
print(exc.desc)
print(exc.msg)
# 打印栈信息,方便追踪排查异常
logger.exception(exc)
return JSONResponse(
@ -56,9 +58,10 @@ def register_exception(app: FastAPI):
"""
重写HTTPException异常处理器
"""
if DEBUG:
print("请求地址", request.url.__str__())
print("捕捉到重写HTTPException异常异常unicorn_exception_handler")
# logger.error(exc.detail)
print(exc.detail)
# 打印栈信息,方便追踪排查异常
logger.exception(exc)
return JSONResponse(
@ -74,9 +77,10 @@ def register_exception(app: FastAPI):
"""
重写请求验证异常处理器
"""
if DEBUG:
print("请求地址", request.url.__str__())
print("捕捉到重写请求验证异常异常validation_exception_handler")
# logger.error(exc.errors())
print(exc.errors())
# 打印栈信息,方便追踪排查异常
logger.exception(exc)
msg = exc.errors()[0].get("msg")
@ -107,9 +111,10 @@ def register_exception(app: FastAPI):
"""
捕获值异常
"""
if DEBUG:
print("请求地址", request.url.__str__())
print("捕捉到值异常value_exception_handler")
# logger.error(exc.__str__())
print(exc.__str__())
# 打印栈信息,方便追踪排查异常
logger.exception(exc)
return JSONResponse(
@ -127,9 +132,10 @@ def register_exception(app: FastAPI):
"""
捕获全部异常
"""
if DEBUG:
print("请求地址", request.url.__str__())
print("捕捉到全局异常all_exception_handler")
# logger.error(exc.__str__())
print(exc.__str__())
# 打印栈信息,方便追踪排查异常
logger.exception(exc)
return JSONResponse(