Skip to content

Commit 07771c1

Browse files
committed
fix: 修复1.3.0版本采用新的异常处理机制后日志装饰器无法记录异常日志的问题
1 parent bea5d94 commit 07771c1

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

ruoyi-fastapi-backend/module_admin/annotation/log_annotation.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from module_admin.service.login_service import LoginService
1616
from config.enums import BusinessType
1717
from config.env import AppConfig
18+
from exceptions.exception import LoginException, ServiceException, ServiceWarning
19+
from utils.response_util import ResponseUtil
1820

1921

2022
class Log:
@@ -111,8 +113,17 @@ async def wrapper(*args, **kwargs):
111113
loginTime=oper_time.strftime('%Y-%m-%d %H:%M:%S'),
112114
)
113115
kwargs['form_data'].login_info = login_log
114-
# 调用原始函数
115-
result = await func(*args, **kwargs)
116+
try:
117+
# 调用原始函数
118+
result = await func(*args, **kwargs)
119+
except LoginException as e:
120+
result = ResponseUtil.failure(data=e.data, msg=e.message)
121+
except ServiceException as e:
122+
result = ResponseUtil.error(data=e.data, msg=e.message)
123+
except ServiceWarning as e:
124+
result = ResponseUtil.failure(data=e.data, msg=e.message)
125+
except Exception as e:
126+
result = ResponseUtil.error(msg=str(e))
116127
# 获取请求耗时
117128
cost_time = float(time.time() - start_time) * 100
118129
# 判断请求是否来自api文档
@@ -281,8 +292,17 @@ async def wrapper(*args, **kwargs):
281292
loginTime=oper_time.strftime('%Y-%m-%d %H:%M:%S'),
282293
)
283294
kwargs['form_data'].login_info = login_log
284-
# 调用原始函数
285-
result = await func(*args, **kwargs)
295+
try:
296+
# 调用原始函数
297+
result = await func(*args, **kwargs)
298+
except LoginException as e:
299+
result = ResponseUtil.failure(data=e.data, msg=e.message)
300+
except ServiceException as e:
301+
result = ResponseUtil.error(data=e.data, msg=e.message)
302+
except ServiceWarning as e:
303+
result = ResponseUtil.failure(data=e.data, msg=e.message)
304+
except Exception as e:
305+
result = ResponseUtil.error(msg=str(e))
286306
# 获取请求耗时
287307
cost_time = float(time.time() - start_time) * 100
288308
# 判断请求是否来自api文档

0 commit comments

Comments
 (0)