0

I'm using the retry module to handle exceptions. When I call f.write, this can return RateLimitException with error codes 400-500. I just need to retry on code 401, how can I achieve this?
(I currently don't have access to that library) I can read the self.code property of the Exception

import retry

@retry.retry(RateLimitException, tries=3, delay=1, backoff=2)
def write(self, buf, path):
   with self._get_client().open(path, 'w') as f:
     return f.write(buf)


class RateLimitException(Exception):
    """Holds the message and code from cloud errors."""
    def __init__(self, error_response=None):
        if error_response:
            self.message = error_response.get('message', '')
            self.code = error_response.get('code', None)
        else:
            self.message = ''
            self.code = None
        # Call the base class constructor with the parameters it needs
        super(RateLimitException, self).__init__(self.message)
gogasca
  • 9,283
  • 6
  • 80
  • 125
  • 1
    Why are you using `super(HtmlError, ...` inside a class that isn't `HtmlError`? – user2357112 Feb 15 '19 at 06:17
  • Just corrected it, was showing a more comprehensive example. – gogasca Feb 15 '19 at 06:36
  • You might prefer to use the [`retrying`](https://github.com/rholder/retrying) module over `retry`. It can take a function that checks the exception and return true if it wants to retry. – Dunes Feb 15 '19 at 09:51

0 Answers0