Skip to content

Commit 0e058c7

Browse files
fix: Handle TransportError Exceptions thrown from gapic_publish (#1318)
1 parent f79d35a commit 0e058c7

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

google/cloud/pubsub_v1/publisher/_batch/thread.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from opentelemetry import trace
2525
import google.api_core.exceptions
2626
from google.api_core import gapic_v1
27+
from google.auth import exceptions as auth_exceptions
2728

2829
from google.cloud.pubsub_v1.publisher import exceptions
2930
from google.cloud.pubsub_v1.publisher import futures
@@ -342,7 +343,10 @@ def _commit(self) -> None:
342343
)
343344
span.set_attribute(key="messaging.message.id", value=message_id)
344345
wrapper.end_create_span()
345-
except google.api_core.exceptions.GoogleAPIError as exc:
346+
except (
347+
google.api_core.exceptions.GoogleAPIError,
348+
auth_exceptions.TransportError,
349+
) as exc:
346350
# We failed to publish, even after retries, so set the exception on
347351
# all futures and exit.
348352
self._status = base.BatchStatus.ERROR

tests/unit/pubsub_v1/publisher/batch/test_thread.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import google.api_core.exceptions
3232
from google.api_core import gapic_v1
3333
from google.auth import credentials
34+
from google.auth import exceptions as auth_exceptions
3435
from google.cloud.pubsub_v1 import publisher
3536
from google.cloud.pubsub_v1 import types
3637
from google.cloud.pubsub_v1.publisher import exceptions
@@ -329,7 +330,14 @@ def test_blocking__commit_wrong_messageid_length():
329330
assert isinstance(future.exception(), exceptions.PublishError)
330331

331332

332-
def test_block__commmit_api_error():
333+
@pytest.mark.parametrize(
334+
"error",
335+
[
336+
(google.api_core.exceptions.InternalServerError("Internal server error"),),
337+
(auth_exceptions.TransportError("some transport error"),),
338+
],
339+
)
340+
def test_block__commmit_api_error(error):
333341
batch = create_batch()
334342
futures = (
335343
batch.publish(
@@ -345,15 +353,14 @@ def test_block__commmit_api_error():
345353
)
346354

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

351358
with patch:
352359
batch._commit()
353360

354361
for future in futures:
355362
assert future.done()
356-
assert future.exception() == error
363+
assert future.exception() == error[0]
357364

358365

359366
def test_block__commmit_retry_error():

0 commit comments

Comments
 (0)