groodt/retrying

using retry_on_exception with class function is not working

TarikIbrahim opened this issue · 2 comments

Hello,
trying the following code is not working

def catch_general_exception(self,ex):
       return isinstance(ex,Exception)

@retry(retry_on_exception=catch_general_exception)
def do_some_code:
       # do some code
       try:
              # some ode
       except Exception as ex:

passing catch_general_exception is not accepted even i passed self.catch_general_exception

I suspect that you meant something like this:

class SomeClass:
    def catch_general_exception(self, ex):
        return isinstance(ex,Exception)

    @retry(retry_on_exception=self.catch_general_exception)
    def do_some_code(self):
        # do some code
        try:
            # some ode
        except Exception as ex:
            raise

This won't work because self isn't defined when the @retry decorator is being created.

This should work, though:

def catch_general_exception(ex):
    return isinstance(ex,Exception)

class SomeClass:

    @retry(retry_on_exception=catch_general_exception)
    def do_some_code(self):
        # do some code
        try:
            # some ode
        except Exception as ex:
            raise

This might even work: @retry(retry_on_exception=lamba ex: isinstance(ex, Exception)

Actually, after doing more reading, your use case is already supported:

@retry(
    retry_on_exception=(Exception,)
)