Created
December 23, 2021 07:58
-
-
Save yuyoyuppe/8c49b7e73b2f93106352caa81246e0a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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