1

I'm having the following error handler (bypasser actually) decorator -

import functools

def exception_safe(*args):
        ErrList = tuple(args)
        def decorator(f):
                @functools.wraps(f)
                def wrapper(*args, **kwargs):
                        if ErrList:
                                try:
                                        result = f(*args, **kwargs)
                                        print(f'No error!')
                                        return result 
                                except ErrList as err:
                                        print(f'got error!')
                        else:
                                try:
                                        result = f(*args, **kwargs)
                                        print(f'No error!')
                                        return result 
                                except Exception as err:
                                        print(f'got error!')
                return wrapper
        return decorator

Even though I still get the next assertion error from my pytests, telling me that the function name is 'decorator':

pytest stack

What could be a reason for that? I literally tried everything..

alexisdevarennes
  • 5,437
  • 4
  • 24
  • 38
Yariv Levy
  • 162
  • 1
  • 9
  • There's an easier way for the record, using `suppress` you can tell it which errors to ignore - https://docs.python.org/3/library/contextlib.html#contextlib.suppress. It'd make your wrapper a lot cleaner. – Peter Nov 14 '19 at 12:39

1 Answers1

1

exception_safe needs to be given arguments, even if there aren't any:

@exception_safe()
def function():
Alex Hall
  • 34,833
  • 5
  • 57
  • 89