1616from backend .common .enums import OperaLogCipherType , StatusType
1717from backend .common .log import log
1818from backend .common .queue import batch_dequeue
19+ from backend .common .response .response_code import StandardResponseCode
1920from backend .core .conf import settings
2021from backend .utils .encrypt import AESCipher , ItsDCipher , Md5Cipher
2122from backend .utils .trace_id import get_request_trace_id
@@ -44,7 +45,6 @@ async def dispatch(self, request: Request, call_next: Any) -> Response:
4445 args = await self .get_request_args (request )
4546
4647 # 执行请求
47- elapsed = 0.0
4848 code = 200
4949 msg = 'Success'
5050 status = StatusType .enable
@@ -57,8 +57,6 @@ async def dispatch(self, request: Request, call_next: Any) -> Response:
5757 '__request_validation_exception__' ,
5858 '__request_assertion_error__' ,
5959 '__request_custom_exception__' ,
60- '__request_all_unknown_exception__' ,
61- '__request_cors_500_exception__' ,
6260 ]:
6361 exception = getattr (request .state , state , None )
6462 if exception :
@@ -67,11 +65,12 @@ async def dispatch(self, request: Request, call_next: Any) -> Response:
6765 log .error (f'请求异常: { msg } ' )
6866 break
6967 except Exception as e :
70- log . error ( f'请求异常: { str ( e ) } ' )
71- code = getattr (e , 'code' , code ) # 兼容 SQLAlchemy 异常用法
72- msg = getattr (e , 'msg' , msg )
68+ elapsed = ( time . perf_counter () - request . state . perf_time ) * 1000
69+ code = getattr (e , 'code' , StandardResponseCode . HTTP_500 ) # 兼容 SQLAlchemy 异常用法
70+ msg = getattr (e , 'msg' , str ( e )) # 不建议使用 traceback 模块获取错误信息,会暴漏代码信息
7371 status = StatusType .disable
7472 error = e
73+ log .error (f'请求异常: { str (e )} ' )
7574
7675 # 此信息只能在请求后获取
7776 _route = request .scope .get ('route' )
0 commit comments