src.decorators.handlers
Defines custom exception handlers
1"""Defines custom exception handlers""" 2import inspect 3from functools import wraps 4import src.constants as cst 5from src.cmd_types.output import CommandOutput 6from src.extra.formatter import formatter 7 8def get_cls_caller(func): 9 """ 10 Gets initial caller of the decorated function. Caller is identified by logger attribute availability 11 :param func: decorated function 12 :return: 13 """ 14 @wraps(func) 15 def wrapper(self = None, *args, **kwargs): 16 requires_self = True 17 if not getattr(self, 'logger', None): 18 requires_self = False 19 caller_frame = inspect.currentframe().f_back 20 while not ( 21 getattr(caller_frame.f_locals.get('self', {}), 22 "logger", 23 None) 24 ): 25 caller_frame = caller_frame.f_back 26 args = list(args) 27 args.insert(0, self) 28 self = caller_frame.f_locals["self"] 29 30 return func(self, *args, requires_self = requires_self, **kwargs) 31 return wrapper 32 33def handle_all_default(func): 34 """Converts default exceptions to custom ones. Will raise an exception, if initial caller is class, not a function""" 35 @wraps(func) 36 @get_cls_caller 37 def wrapper(self, *args, requires_self: bool = True, **kwargs): 38 try: 39 if requires_self: 40 return func(self, *args, **kwargs) 41 else: 42 return func(*args, **kwargs) 43 except tuple(cst.ERROR_HANDLERS_MESSAGES_FORMATS.keys()) as e: 44 e_type = type(e) 45 err_msg_format = cst.ERROR_HANDLERS_MESSAGES_FORMATS[e_type] 46 err_msg = formatter(e, err_msg_format) 47 return CommandOutput( 48 stderr = err_msg+"\n", 49 errcode = err_msg_format.errcode 50 ) 51 52 return wrapper
def
get_cls_caller(func):
9def get_cls_caller(func): 10 """ 11 Gets initial caller of the decorated function. Caller is identified by logger attribute availability 12 :param func: decorated function 13 :return: 14 """ 15 @wraps(func) 16 def wrapper(self = None, *args, **kwargs): 17 requires_self = True 18 if not getattr(self, 'logger', None): 19 requires_self = False 20 caller_frame = inspect.currentframe().f_back 21 while not ( 22 getattr(caller_frame.f_locals.get('self', {}), 23 "logger", 24 None) 25 ): 26 caller_frame = caller_frame.f_back 27 args = list(args) 28 args.insert(0, self) 29 self = caller_frame.f_locals["self"] 30 31 return func(self, *args, requires_self = requires_self, **kwargs) 32 return wrapper
Gets initial caller of the decorated function. Caller is identified by logger attribute availability
Parameters
- func: decorated function
Returns
def
handle_all_default(func):
34def handle_all_default(func): 35 """Converts default exceptions to custom ones. Will raise an exception, if initial caller is class, not a function""" 36 @wraps(func) 37 @get_cls_caller 38 def wrapper(self, *args, requires_self: bool = True, **kwargs): 39 try: 40 if requires_self: 41 return func(self, *args, **kwargs) 42 else: 43 return func(*args, **kwargs) 44 except tuple(cst.ERROR_HANDLERS_MESSAGES_FORMATS.keys()) as e: 45 e_type = type(e) 46 err_msg_format = cst.ERROR_HANDLERS_MESSAGES_FORMATS[e_type] 47 err_msg = formatter(e, err_msg_format) 48 return CommandOutput( 49 stderr = err_msg+"\n", 50 errcode = err_msg_format.errcode 51 ) 52 53 return wrapper
Converts default exceptions to custom ones. Will raise an exception, if initial caller is class, not a function