retrying icon indicating copy to clipboard operation
retrying copied to clipboard

using retry_on_exception with class function is not working

Open TarikIbrahim opened this issue 2 years ago • 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

TarikIbrahim avatar Jan 23 '23 14:01 TarikIbrahim

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)

PamelaM avatar Aug 28 '24 13:08 PamelaM

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

@retry(
    retry_on_exception=(Exception,)
)

PamelaM avatar Aug 28 '24 14:08 PamelaM