From 23a8db839909a0781343cb18edffaea06a0b7092 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Mon, 26 Apr 2021 12:06:19 -0700 Subject: [PATCH] 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 --- google/cloud/storage/retry.py | 17 ++++++++++------- tests/unit/test_retry.py | 11 ++++++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/google/cloud/storage/retry.py b/google/cloud/storage/retry.py index e9a9eeb2f..e17f3d5a0 100644 --- a/google/cloud/storage/retry.py +++ b/google/cloud/storage/retry.py @@ -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, ) @@ -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 diff --git a/tests/unit/test_retry.py b/tests/unit/test_retry.py index d9a773cf9..582fa8097 100644 --- a/tests/unit/test_retry.py +++ b/tests/unit/test_retry.py @@ -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: