From 6ae04a8d134fffe13f06081e15f9723c1b2ea334 Mon Sep 17 00:00:00 2001 From: Anthonios Partheniou Date: Mon, 3 May 2021 11:26:04 -0400 Subject: [PATCH] feat: retry google.auth TransportError and requests ConnectionError (#178) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/python-api-core/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes #176 🦕 --- google/api_core/retry.py | 4 ++++ tests/unit/test_retry.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/google/api_core/retry.py b/google/api_core/retry.py index 43e4f079..f0f23bc8 100644 --- a/google/api_core/retry.py +++ b/google/api_core/retry.py @@ -62,11 +62,13 @@ def check_if_exists(): import random import time +import requests.exceptions import six from google.api_core import datetime_helpers from google.api_core import exceptions from google.api_core import general_helpers +from google.auth import exceptions as auth_exceptions _LOGGER = logging.getLogger(__name__) _DEFAULT_INITIAL_DELAY = 1.0 # seconds @@ -101,6 +103,8 @@ def if_exception_type_predicate(exception): exceptions.InternalServerError, exceptions.TooManyRequests, exceptions.ServiceUnavailable, + requests.exceptions.ConnectionError, + auth_exceptions.TransportError, ) """A predicate that checks if an exception is a transient API error. diff --git a/tests/unit/test_retry.py b/tests/unit/test_retry.py index 4c2e457a..ce8f417d 100644 --- a/tests/unit/test_retry.py +++ b/tests/unit/test_retry.py @@ -18,9 +18,11 @@ import mock import pytest +import requests.exceptions from google.api_core import exceptions from google.api_core import retry +from google.auth import exceptions as auth_exceptions def test_if_exception_type(): @@ -42,6 +44,8 @@ def test_if_transient_error(): assert retry.if_transient_error(exceptions.InternalServerError("")) assert retry.if_transient_error(exceptions.TooManyRequests("")) assert retry.if_transient_error(exceptions.ServiceUnavailable("")) + assert retry.if_transient_error(requests.exceptions.ConnectionError("")) + assert retry.if_transient_error(auth_exceptions.TransportError("")) assert not retry.if_transient_error(exceptions.InvalidArgument(""))