Skip to content

Instantly share code, notes, and snippets.

@jezeniel
Created September 13, 2017 10:14
Show Gist options
  • Save jezeniel/15e02f518a85d4ba66e64f1bc22feeb3 to your computer and use it in GitHub Desktop.
Save jezeniel/15e02f518a85d4ba66e64f1bc22feeb3 to your computer and use it in GitHub Desktop.
Error Registry
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