Skip to content

Instantly share code, notes, and snippets.

@yuyoyuppe
Created December 23, 2021 07:58
Show Gist options
  • Save yuyoyuppe/8c49b7e73b2f93106352caa81246e0a9 to your computer and use it in GitHub Desktop.
Save yuyoyuppe/8c49b7e73b2f93106352caa81246e0a9 to your computer and use it in GitHub Desktop.
class OnExc(object):
def __init__(self, default_val):
self.default_val = default_val
def __call__(self, func):
decorator_self = self
def decorated_func(*args, **kwargs):
try:
return func(*args, **kwargs)
except:
error_str = f"Error in {func.__name__}: {sys.exc_info()[1]}"
if decorator_self.default_val == exit:
sys.exit(error_str)
else:
print(error_str)
return decorator_self.default_val
return decorated_func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment