Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle TransportError Exceptions thrown from gapic_publish #1318

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion google/cloud/pubsub_v1/publisher/_batch/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from opentelemetry import trace
import google.api_core.exceptions
from google.api_core import gapic_v1
from google.auth import exceptions as auth_exceptions

from google.cloud.pubsub_v1.publisher import exceptions
from google.cloud.pubsub_v1.publisher import futures
Expand Down Expand Up @@ -342,7 +343,10 @@ def _commit(self) -> None:
)
span.set_attribute(key="messaging.message.id", value=message_id)
wrapper.end_create_span()
except google.api_core.exceptions.GoogleAPIError as exc:
except (
google.api_core.exceptions.GoogleAPIError,
auth_exceptions.TransportError,
) as exc:
# We failed to publish, even after retries, so set the exception on
# all futures and exit.
self._status = base.BatchStatus.ERROR
Expand Down
13 changes: 10 additions & 3 deletions tests/unit/pubsub_v1/publisher/batch/test_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import google.api_core.exceptions
from google.api_core import gapic_v1
from google.auth import credentials
from google.auth import exceptions as auth_exceptions
from google.cloud.pubsub_v1 import publisher
from google.cloud.pubsub_v1 import types
from google.cloud.pubsub_v1.publisher import exceptions
Expand Down Expand Up @@ -329,7 +330,14 @@ def test_blocking__commit_wrong_messageid_length():
assert isinstance(future.exception(), exceptions.PublishError)


def test_block__commmit_api_error():
@pytest.mark.parametrize(
"error",
[
(google.api_core.exceptions.InternalServerError("Internal server error"),),
(auth_exceptions.TransportError("some transport error"),),
],
)
def test_block__commmit_api_error(error):
batch = create_batch()
futures = (
batch.publish(
Expand All @@ -345,15 +353,14 @@ def test_block__commmit_api_error():
)

# Make the API throw an error when publishing.
error = google.api_core.exceptions.InternalServerError("uh oh")
patch = mock.patch.object(type(batch.client), "_gapic_publish", side_effect=error)

with patch:
batch._commit()

for future in futures:
assert future.done()
assert future.exception() == error
assert future.exception() == error[0]


def test_block__commmit_retry_error():
Expand Down
Loading