wrapping class method in try / except using decorator

decorator, python, python-2.7, python-decorators

Solution

Such a decorator would simply be:

def handle_exceptions(f):
    def wrapper(*args, **kw):
        try:
            return f(*args, **kw)
        except Exception:
            self = args[0]
            exception_handler(self.log, True)
    return wrapper

This decorator simply calls the wrapped function inside a `try` suite.

This can be applied to methods only, as it assumes the first argument is `self`.

Problem

I have a general purpose function that sends info about exceptions to an application log. I use the `exception_handler` function from within methods in classes. The app log handler that is passed into and called by the `exception_handler` creates a JSON string that is what actually gets sent to the logfile. This all works fine. ``` def exception_handler(log, terminate=False): exc_type, exc_value, exc_tb = sys.exc_info() filename, line_num, func_name, text = traceback.extract_tb(exc_tb)[-1] log.error('{0} Thrown from module: {1} in {2} at line: {3} ({4})'.format(exc_value, filename, func_name, line_num, text)) del (filename, line_num, func_name, text) if terminate: sys.exit() ``` I use it as follows: (a hyper-simplified example) ``` from utils import exception_handler class Demo1(object): def __init__(self): self.log = {a class that implements the application log} def demo(self, name): try: print(name) except Exception: exception_handler(self.log, True) ``` I would like to alter `exception_handler` for use as a decorator for a large number of methods, i.e.: ``` @handle_exceptions def func1(self, name) {some code that gets wrapped in a try / except by the decorator} ``` I've looked at a number of articles about decorators, but I haven't yet figured out how to implement what I want to do. I need to pass a reference to the active log object and also pass 0 or more arguments to the wrapped function. I'd be happy to convert `exception_handler` to a method in a class if that makes things easier.

Original source