Created
September 13, 2017 10:14
-
-
Save jezeniel/15e02f518a85d4ba66e64f1bc22feeb3 to your computer and use it in GitHub Desktop.
Error Registry
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 Jug: | |
def __init__(self): | |
self.exception_handlers = {} | |
def errorhandler(self, exception): | |
def decorator(f): | |
self.exception_handlers[exception] = f | |
return f | |
return decorator | |
def call_function(self, func): | |
try: | |
func() | |
except Exception as e: | |
self.exception_handlers[type(e)](e) | |
jug = Jug() | |
@jug.errorhandler(ValueError) | |
def foo(e): | |
print("THIS IS FOO") | |
@jug.errorhandler(KeyError) | |
def bar(e): | |
print("THIS IS BAR") | |
def func1(): | |
print("Func1!") | |
raise ValueError() | |
def func2(): | |
print("Func2!") | |
raise KeyError() | |
jug.call_function(func1) | |
jug.call_function(func2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment