我正在使用该retry
模块来处理异常。当我调用 f.write 时,这可能会返回带有错误代码 400-500 的 RateLimitException。我只需要重试代码 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)