I have a decorator that I use to suppress and log exceptions within a function. The code is something like this:
def log_exceptions(func):
def wrapper(*args, **kwargs):
try:
ret = func(*args, **kwargs)
except Exception as e:
print(e)
print(args, kwargs) # also log what arguments caused the exception
return ret
return wrapper
A problem here is that, it is hard to manually match the printed argument values to the argument names of the function, because positional arguments could also go inside kwargs
, and there may be args
and kwargs
arguments in the inner function as well. So, it would be useful to match the args
and kwargs
values in the wrapper with argument names in the inner function.
My question is then, is there a built-in way to do this matching? If not, what would be an elegant way to implement it?