Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: retry auth.TransportError errors (#418)
* fix: retry auth.TransportError's

* unwrap exceptions

* lint

* revert exceptions alias

* scope unwrapping and add unit test

* lint

* fix unit test
  • Loading branch information
frankyn committed Apr 26, 2021
1 parent 2adfb59 commit 23a8db8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
17 changes: 10 additions & 7 deletions google/cloud/storage/retry.py
Expand Up @@ -14,18 +14,19 @@

import requests

from google.api_core import exceptions
from google.api_core import exceptions as api_exceptions
from google.api_core import retry
from google.auth import exceptions as auth_exceptions

import json


_RETRYABLE_TYPES = (
exceptions.TooManyRequests, # 429
exceptions.InternalServerError, # 500
exceptions.BadGateway, # 502
exceptions.ServiceUnavailable, # 503
exceptions.GatewayTimeout, # 504
api_exceptions.TooManyRequests, # 429
api_exceptions.InternalServerError, # 500
api_exceptions.BadGateway, # 502
api_exceptions.ServiceUnavailable, # 503
api_exceptions.GatewayTimeout, # 504
requests.ConnectionError,
)

Expand All @@ -37,8 +38,10 @@ def _should_retry(exc):
"""Predicate for determining when to retry."""
if isinstance(exc, _RETRYABLE_TYPES):
return True
elif isinstance(exc, exceptions.GoogleAPICallError):
elif isinstance(exc, api_exceptions.GoogleAPICallError):
return exc.code in _ADDITIONAL_RETRYABLE_STATUS_CODES
elif isinstance(exc, auth_exceptions.TransportError):
return _should_retry(exc.args[0])
else:
return False

Expand Down
11 changes: 10 additions & 1 deletion tests/unit/test_retry.py
Expand Up @@ -25,7 +25,16 @@ def _call_fut(self, exc):

return retry._should_retry(exc)

def test_w_retryable_types(self):
def test_w_retryable_transport_error(self):
from google.cloud.storage import retry
from google.auth.exceptions import TransportError as eTransportError
from requests import ConnectionError as rConnectionError

caught_exc = rConnectionError("Remote end closed connection unexpected")
exc = eTransportError(caught_exc)
self.assertTrue(retry._should_retry(exc))

def test_w_wrapped_type(self):
from google.cloud.storage import retry

for exc_type in retry._RETRYABLE_TYPES:
Expand Down

0 comments on commit 23a8db8

Please sign in to comment.