Skip to content

Commit 66e61cc

Browse files
authored
fix: Show a non-None error for core_exception.Unknown errors. (#968)
* fix: Show a non-None error for core_exception.Unknown errors. * Add test for Unknown api errors.
1 parent 98fb176 commit 66e61cc

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

google/cloud/ndb/_retry.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ def retry_wrapper(*args, **kwargs):
8282
result = yield result
8383
except exceptions.NestedRetryException as e:
8484
error = e
85-
except Exception as e:
85+
except BaseException as e:
8686
# `e` is removed from locals at end of block
8787
error = e # See: https://goo.gl/5J8BMK
88+
8889
if not is_transient_error(error):
8990
# If we are in an inner retry block, use special nested
9091
# retry exception to bubble up to outer retry. Else, raise
@@ -104,6 +105,10 @@ def retry_wrapper(*args, **kwargs):
104105

105106
yield tasklets.sleep(sleep_time)
106107

108+
# Unknown errors really want to show up as None, so manually set the error.
109+
if isinstance(error, core_exceptions.Unknown):
110+
error = "google.api_core.exceptions.Unknown"
111+
107112
raise core_exceptions.RetryError(
108113
"Maximum number of {} retries exceeded while calling {}".format(
109114
retries, callback

tests/unit/test__retry.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ def callback():
9898
retry = _retry.retry_async(callback)
9999
assert retry().exception() is error
100100

101+
@staticmethod
102+
@pytest.mark.usefixtures("in_context")
103+
def test_api_core_unknown():
104+
def callback():
105+
raise core_exceptions.Unknown("Unknown")
106+
107+
with pytest.raises(core_exceptions.RetryError) as e:
108+
retry = _retry.retry_async(callback, retries=1)
109+
retry().result()
110+
111+
assert e.value.cause == "google.api_core.exceptions.Unknown"
112+
101113
@staticmethod
102114
@pytest.mark.usefixtures("in_context")
103115
@mock.patch("google.cloud.ndb.tasklets.sleep")

0 commit comments

Comments
 (0)