[go: up one dir, main page]

Skip to content

Commit

Permalink
fix: Show a non-None error for core_exception.Unknown errors. (#968)
Browse files Browse the repository at this point in the history
* fix: Show a non-None error for core_exception.Unknown errors.

* Add test for Unknown api errors.
  • Loading branch information
sorced-jim committed Mar 9, 2024
1 parent 98fb176 commit 66e61cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion google/cloud/ndb/_retry.py
Expand Up @@ -82,9 +82,10 @@ def retry_wrapper(*args, **kwargs):
result = yield result
except exceptions.NestedRetryException as e:
error = e
except Exception as e:
except BaseException as e:
# `e` is removed from locals at end of block
error = e # See: https://goo.gl/5J8BMK

if not is_transient_error(error):
# If we are in an inner retry block, use special nested
# retry exception to bubble up to outer retry. Else, raise
Expand All @@ -104,6 +105,10 @@ def retry_wrapper(*args, **kwargs):

yield tasklets.sleep(sleep_time)

# Unknown errors really want to show up as None, so manually set the error.
if isinstance(error, core_exceptions.Unknown):
error = "google.api_core.exceptions.Unknown"

raise core_exceptions.RetryError(
"Maximum number of {} retries exceeded while calling {}".format(
retries, callback
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test__retry.py
Expand Up @@ -98,6 +98,18 @@ def callback():
retry = _retry.retry_async(callback)
assert retry().exception() is error

@staticmethod
@pytest.mark.usefixtures("in_context")
def test_api_core_unknown():
def callback():
raise core_exceptions.Unknown("Unknown")

with pytest.raises(core_exceptions.RetryError) as e:
retry = _retry.retry_async(callback, retries=1)
retry().result()

assert e.value.cause == "google.api_core.exceptions.Unknown"

@staticmethod
@pytest.mark.usefixtures("in_context")
@mock.patch("google.cloud.ndb.tasklets.sleep")
Expand Down

0 comments on commit 66e61cc

Please sign in to comment.