The goal of WrapErr is to avoid contaminating main logic with try-catch blocks and the exception handling logic.
Without WrapErr, you have something like this:
try:
// do something
except Exception as e:
// exception handling logicWith WrapErr, you would write something more like this:
def error_handler(e: Exception):
// exception handling logic
@wrap_error(error_handler)
def foo(*args, **kwargs):
// do somethingYou can reuse the same error handling logic across multiple functions and methods:
def error_handler(e: Exception):
// exception handling logic
@wrap_error(error_handler)
def foo(*args, **kwargs):
// do something
@wrap_error(error_handler):
def bar(*args, **kwargs):
// do something elseYou can also only handle exceptions of a specific type:
def value_error_handler(e: ValueError):
// exception handling logic
@wrap_error(error_handler, ValueError)
def foo(*args, **kwargs):
// do somethingAnd you can combine different error handlers that handle different exceptions:
def value_error_handler(e: ValueError):
// exception handling logic
def attribute_error_handler(e: AttributeError):
// more exception handling logic
@wrap_error(value_error_handler, ValueError)
@wrap_error(attribute_error_handler, AttributeError)
def foo(*args, **kwargs):
// do something