Skip to content

Commit

Permalink
move stored error into own variable
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sanche committed Oct 9, 2024
1 parent 30ded2c commit bfea5ae
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions google/api_core/grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __init__(self, wrapped, prefetch_first_result=True):
pass
except grpc.RpcError as exc:
# If the pre-fetch fails, store exception to be raised on next() call.
self._stored_first_result = exc
self._stored_first_error = exc

def __iter__(self) -> Iterator[P]:
"""This iterator is also an iterable that returns itself."""
Expand All @@ -115,9 +115,11 @@ def __next__(self) -> P:
if hasattr(self, "_stored_first_result"):
result = self._stored_first_result
del self._stored_first_result
if isinstance(result, Exception):
raise result
return result
elif hasattr(self, "_stored_first_error"):
exc = self._stored_first_error
del self._stored_first_error
raise exc
return next(self._wrapped)
except grpc.RpcError as exc:
# If the stream has already returned data, we cannot recover here.
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_ctor_w_rpc_error_on_prefetch(self):
wrapped.__next__.side_effect = exc

obj = self._make_one(wrapped)
assert obj._stored_first_result is exc
assert obj._stored_first_error is exc
with pytest.raises(exceptions.GoogleAPICallError):
next(obj)

Expand Down Expand Up @@ -330,7 +330,7 @@ def test_wrap_stream_errors_iterator_initialization():
wrapped_callable = grpc_helpers._wrap_stream_errors(callable_)

it = wrapped_callable(1, 2, three="four")
assert it._stored_first_result is grpc_error
assert it._stored_first_error is grpc_error

callable_.assert_called_once_with(1, 2, three="four")
# should raise on first iteration
Expand Down

0 comments on commit bfea5ae

Please sign in to comment.