优化:异常处理
This commit is contained in:
parent
4a9bf1fdc3
commit
21980a6e34
@ -11,7 +11,7 @@ from fastapi.security import OAuth2PasswordBearer
|
|||||||
"""
|
"""
|
||||||
系统版本
|
系统版本
|
||||||
"""
|
"""
|
||||||
VERSION = "3.2.2"
|
VERSION = "3.3.0"
|
||||||
|
|
||||||
"""安全警告: 不要在生产中打开调试运行!"""
|
"""安全警告: 不要在生产中打开调试运行!"""
|
||||||
DEBUG = False
|
DEBUG = False
|
||||||
|
@ -13,6 +13,7 @@ from fastapi import Request
|
|||||||
from fastapi.encoders import jsonable_encoder
|
from fastapi.encoders import jsonable_encoder
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from core.logger import logger
|
from core.logger import logger
|
||||||
|
from application.settings import DEBUG
|
||||||
|
|
||||||
|
|
||||||
class CustomException(Exception):
|
class CustomException(Exception):
|
||||||
@ -40,10 +41,11 @@ def register_exception(app: FastAPI):
|
|||||||
"""
|
"""
|
||||||
自定义异常
|
自定义异常
|
||||||
"""
|
"""
|
||||||
print("请求地址", request.url.__str__())
|
if DEBUG:
|
||||||
print("捕捉到重写CustomException异常异常:custom_exception_handler")
|
print("请求地址", request.url.__str__())
|
||||||
# logger.error(exc.desc)
|
print("捕捉到重写CustomException异常异常:custom_exception_handler")
|
||||||
# logger.error(exc.msg)
|
print(exc.desc)
|
||||||
|
print(exc.msg)
|
||||||
# 打印栈信息,方便追踪排查异常
|
# 打印栈信息,方便追踪排查异常
|
||||||
logger.exception(exc)
|
logger.exception(exc)
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
@ -56,9 +58,10 @@ def register_exception(app: FastAPI):
|
|||||||
"""
|
"""
|
||||||
重写HTTPException异常处理器
|
重写HTTPException异常处理器
|
||||||
"""
|
"""
|
||||||
print("请求地址", request.url.__str__())
|
if DEBUG:
|
||||||
print("捕捉到重写HTTPException异常异常:unicorn_exception_handler")
|
print("请求地址", request.url.__str__())
|
||||||
# logger.error(exc.detail)
|
print("捕捉到重写HTTPException异常异常:unicorn_exception_handler")
|
||||||
|
print(exc.detail)
|
||||||
# 打印栈信息,方便追踪排查异常
|
# 打印栈信息,方便追踪排查异常
|
||||||
logger.exception(exc)
|
logger.exception(exc)
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
@ -74,9 +77,10 @@ def register_exception(app: FastAPI):
|
|||||||
"""
|
"""
|
||||||
重写请求验证异常处理器
|
重写请求验证异常处理器
|
||||||
"""
|
"""
|
||||||
print("请求地址", request.url.__str__())
|
if DEBUG:
|
||||||
print("捕捉到重写请求验证异常异常:validation_exception_handler")
|
print("请求地址", request.url.__str__())
|
||||||
# logger.error(exc.errors())
|
print("捕捉到重写请求验证异常异常:validation_exception_handler")
|
||||||
|
print(exc.errors())
|
||||||
# 打印栈信息,方便追踪排查异常
|
# 打印栈信息,方便追踪排查异常
|
||||||
logger.exception(exc)
|
logger.exception(exc)
|
||||||
msg = exc.errors()[0].get("msg")
|
msg = exc.errors()[0].get("msg")
|
||||||
@ -107,9 +111,10 @@ def register_exception(app: FastAPI):
|
|||||||
"""
|
"""
|
||||||
捕获值异常
|
捕获值异常
|
||||||
"""
|
"""
|
||||||
print("请求地址", request.url.__str__())
|
if DEBUG:
|
||||||
print("捕捉到值异常:value_exception_handler")
|
print("请求地址", request.url.__str__())
|
||||||
# logger.error(exc.__str__())
|
print("捕捉到值异常:value_exception_handler")
|
||||||
|
print(exc.__str__())
|
||||||
# 打印栈信息,方便追踪排查异常
|
# 打印栈信息,方便追踪排查异常
|
||||||
logger.exception(exc)
|
logger.exception(exc)
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
@ -127,9 +132,10 @@ def register_exception(app: FastAPI):
|
|||||||
"""
|
"""
|
||||||
捕获全部异常
|
捕获全部异常
|
||||||
"""
|
"""
|
||||||
print("请求地址", request.url.__str__())
|
if DEBUG:
|
||||||
print("捕捉到全局异常:all_exception_handler")
|
print("请求地址", request.url.__str__())
|
||||||
# logger.error(exc.__str__())
|
print("捕捉到全局异常:all_exception_handler")
|
||||||
|
print(exc.__str__())
|
||||||
# 打印栈信息,方便追踪排查异常
|
# 打印栈信息,方便追踪排查异常
|
||||||
logger.exception(exc)
|
logger.exception(exc)
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user