如何在Python发生异常错误时调用重试装饰器

时间:2019-02-15 06:14:38

标签: python python-3.x retry-logic

我正在使用retry模块来处理异常。当我调用f.write时,这会返回RateLimitException,错误代码为400-500。我只需要重试代码401,如何实现?
(我目前无法访问该库)我可以读取Exception的self.code属性

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)

0 个答案:

没有答案