修复一些问题
This commit is contained in:
		@@ -1,19 +1,28 @@
 | 
			
		||||
from functools import wraps
 | 
			
		||||
from codes.common import db_operation
 | 
			
		||||
from inspect import getfile
 | 
			
		||||
from pathlib import Path
 | 
			
		||||
from inspect import currentframe
 | 
			
		||||
import traceback
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def handle_exception(stop: bool = False):
 | 
			
		||||
    def exceptions(func):
 | 
			
		||||
        module = Path(getfile(func)).stem
 | 
			
		||||
        func_name = func.__name__
 | 
			
		||||
        @wraps(func)
 | 
			
		||||
        def wrapper(*args, **kwargs):
 | 
			
		||||
            real_frame = currentframe().f_back
 | 
			
		||||
            try:
 | 
			
		||||
                return func(*args, **kwargs)
 | 
			
		||||
            except Exception as e:
 | 
			
		||||
                db_operation.db_write_logs(str(e), "@".join([func_name, module]), "exception")
 | 
			
		||||
                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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user