30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
from functools import wraps
|
|
from codes.common import db_operation
|
|
from inspect import currentframe
|
|
import traceback
|
|
|
|
|
|
def handle_exception(stop: bool = False):
|
|
def exceptions(func):
|
|
@wraps(func)
|
|
def wrapper(*args, **kwargs):
|
|
real_frame = currentframe().f_back
|
|
try:
|
|
return func(*args, **kwargs)
|
|
except Exception as e:
|
|
for frame, lineno in traceback.walk_tb(e.__traceback__):
|
|
if frame.f_code.co_name == func.__name__:
|
|
real_frame = frame
|
|
break
|
|
|
|
tb = traceback.format_exc() # 完整堆栈
|
|
module_name = real_frame.f_globals["__name__"].split(".")[-1]
|
|
func_name = real_frame.f_code.co_name
|
|
line_no = real_frame.f_lineno
|
|
module = f"{module_name}-{func_name}:{line_no}"
|
|
db_operation.db_write_logs(tb, module, "exception")
|
|
if stop:
|
|
raise e
|
|
return wrapper
|
|
return exceptions
|