Skip to content

Commit 3b0a803

Browse files
committed
Fixing broken exception handling after GAX 0.13.0 upgrade.
1 parent 11ca5bd commit 3b0a803

8 files changed

Lines changed: 165 additions & 215 deletions

File tree

google/cloud/_helpers.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,9 @@
3131
except ImportError:
3232
app_identity = None
3333
try:
34-
from google.gax.grpc import exc_to_code as beta_exc_to_code
3534
import grpc
36-
from grpc._channel import _Rendezvous
3735
except ImportError: # pragma: NO COVER
38-
beta_exc_to_code = None
3936
grpc = None
40-
_Rendezvous = Exception
4137
import six
4238
from six.moves import http_client
4339
from six.moves import configparser
@@ -685,21 +681,6 @@ def make_insecure_stub(stub_class, host, port=None):
685681
return stub_class(channel)
686682

687683

688-
def exc_to_code(exc):
689-
"""Retrieves the status code from a gRPC exception.
690-
691-
:type exc: :class:`Exception`
692-
:param exc: An exception from gRPC beta or stable.
693-
694-
:rtype: :class:`grpc.StatusCode`
695-
:returns: The status code attached to the exception.
696-
"""
697-
if isinstance(exc, _Rendezvous):
698-
return exc.code()
699-
else:
700-
return beta_exc_to_code(exc)
701-
702-
703684
try:
704685
from pytz import UTC # pylint: disable=unused-import,wrong-import-order
705686
except ImportError:

google/cloud/logging/_gax.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@
1818

1919
from google.gax import CallOptions
2020
from google.gax import INITIAL_PAGE
21-
from google.gax.errors import GaxError
2221
from google.logging.type.log_severity_pb2 import LogSeverity
2322
from google.logging.v2.logging_config_pb2 import LogSink
2423
from google.logging.v2.logging_metrics_pb2 import LogMetric
2524
from google.logging.v2.log_entry_pb2 import LogEntry
2625
from google.protobuf.json_format import Parse
2726
from grpc import StatusCode
27+
from grpc._channel import _Rendezvous
2828

2929
# pylint: disable=ungrouped-imports
3030
from google.cloud._helpers import _datetime_to_pb_timestamp
3131
from google.cloud._helpers import _pb_timestamp_to_rfc3339
32-
from google.cloud._helpers import exc_to_code
3332
from google.cloud.exceptions import Conflict
3433
from google.cloud.exceptions import NotFound
3534
# pylint: enable=ungrouped-imports
@@ -123,8 +122,8 @@ def logger_delete(self, project, logger_name):
123122
path = 'projects/%s/logs/%s' % (project, logger_name)
124123
try:
125124
self._gax_api.delete_log(path, options)
126-
except GaxError as exc:
127-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
125+
except _Rendezvous as exc:
126+
if exc.code() == StatusCode.NOT_FOUND:
128127
raise NotFound(path)
129128
raise
130129

@@ -195,8 +194,8 @@ def sink_create(self, project, sink_name, filter_, destination):
195194
destination=destination)
196195
try:
197196
self._gax_api.create_sink(parent, sink_pb, options)
198-
except GaxError as exc:
199-
if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION:
197+
except _Rendezvous as exc:
198+
if exc.code() == StatusCode.FAILED_PRECONDITION:
200199
path = 'projects/%s/sinks/%s' % (project, sink_name)
201200
raise Conflict(path)
202201
raise
@@ -218,8 +217,8 @@ def sink_get(self, project, sink_name):
218217
path = 'projects/%s/sinks/%s' % (project, sink_name)
219218
try:
220219
sink_pb = self._gax_api.get_sink(path, options)
221-
except GaxError as exc:
222-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
220+
except _Rendezvous as exc:
221+
if exc.code() == StatusCode.NOT_FOUND:
223222
raise NotFound(path)
224223
raise
225224
return _log_sink_pb_to_mapping(sink_pb)
@@ -250,8 +249,8 @@ def sink_update(self, project, sink_name, filter_, destination):
250249
sink_pb = LogSink(name=path, filter=filter_, destination=destination)
251250
try:
252251
self._gax_api.update_sink(path, sink_pb, options)
253-
except GaxError as exc:
254-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
252+
except _Rendezvous as exc:
253+
if exc.code() == StatusCode.NOT_FOUND:
255254
raise NotFound(path)
256255
raise
257256
return _log_sink_pb_to_mapping(sink_pb)
@@ -269,8 +268,8 @@ def sink_delete(self, project, sink_name):
269268
path = 'projects/%s/sinks/%s' % (project, sink_name)
270269
try:
271270
self._gax_api.delete_sink(path, options)
272-
except GaxError as exc:
273-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
271+
except _Rendezvous as exc:
272+
if exc.code() == StatusCode.NOT_FOUND:
274273
raise NotFound(path)
275274
raise
276275

@@ -340,8 +339,8 @@ def metric_create(self, project, metric_name, filter_, description):
340339
description=description)
341340
try:
342341
self._gax_api.create_log_metric(parent, metric_pb, options)
343-
except GaxError as exc:
344-
if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION:
342+
except _Rendezvous as exc:
343+
if exc.code() == StatusCode.FAILED_PRECONDITION:
345344
path = 'projects/%s/metrics/%s' % (project, metric_name)
346345
raise Conflict(path)
347346
raise
@@ -363,8 +362,8 @@ def metric_get(self, project, metric_name):
363362
path = 'projects/%s/metrics/%s' % (project, metric_name)
364363
try:
365364
metric_pb = self._gax_api.get_log_metric(path, options)
366-
except GaxError as exc:
367-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
365+
except _Rendezvous as exc:
366+
if exc.code() == StatusCode.NOT_FOUND:
368367
raise NotFound(path)
369368
raise
370369
return _log_metric_pb_to_mapping(metric_pb)
@@ -395,8 +394,8 @@ def metric_update(self, project, metric_name, filter_, description):
395394
description=description)
396395
try:
397396
self._gax_api.update_log_metric(path, metric_pb, options)
398-
except GaxError as exc:
399-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
397+
except _Rendezvous as exc:
398+
if exc.code() == StatusCode.NOT_FOUND:
400399
raise NotFound(path)
401400
raise
402401
return _log_metric_pb_to_mapping(metric_pb)
@@ -414,8 +413,8 @@ def metric_delete(self, project, metric_name):
414413
path = 'projects/%s/metrics/%s' % (project, metric_name)
415414
try:
416415
self._gax_api.delete_log_metric(path, options)
417-
except GaxError as exc:
418-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
416+
except _Rendezvous as exc:
417+
if exc.code() == StatusCode.NOT_FOUND:
419418
raise NotFound(path)
420419
raise
421420

google/cloud/pubsub/_gax.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
from google.cloud.gapic.pubsub.v1.subscriber_api import SubscriberApi
1919
from google.gax import CallOptions
2020
from google.gax import INITIAL_PAGE
21-
from google.gax.errors import GaxError
2221
from google.pubsub.v1.pubsub_pb2 import PubsubMessage
2322
from google.pubsub.v1.pubsub_pb2 import PushConfig
24-
from grpc.beta.implementations import insecure_channel
23+
from grpc import insecure_channel
2524
from grpc import StatusCode
25+
from grpc._channel import _Rendezvous
2626

2727
# pylint: disable=ungrouped-imports
2828
from google.cloud._helpers import _to_bytes
29-
from google.cloud._helpers import exc_to_code
3029
from google.cloud._helpers import _pb_timestamp_to_rfc3339
3130
from google.cloud.exceptions import Conflict
3231
from google.cloud.exceptions import NotFound
@@ -93,8 +92,8 @@ def topic_create(self, topic_path):
9392
"""
9493
try:
9594
topic_pb = self._gax_api.create_topic(topic_path)
96-
except GaxError as exc:
97-
if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION:
95+
except _Rendezvous as exc:
96+
if exc.code() == StatusCode.FAILED_PRECONDITION:
9897
raise Conflict(topic_path)
9998
raise
10099
return {'name': topic_pb.name}
@@ -116,8 +115,8 @@ def topic_get(self, topic_path):
116115
"""
117116
try:
118117
topic_pb = self._gax_api.get_topic(topic_path)
119-
except GaxError as exc:
120-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
118+
except _Rendezvous as exc:
119+
if exc.code() == StatusCode.NOT_FOUND:
121120
raise NotFound(topic_path)
122121
raise
123122
return {'name': topic_pb.name}
@@ -134,8 +133,8 @@ def topic_delete(self, topic_path):
134133
"""
135134
try:
136135
self._gax_api.delete_topic(topic_path)
137-
except GaxError as exc:
138-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
136+
except _Rendezvous as exc:
137+
if exc.code() == StatusCode.NOT_FOUND:
139138
raise NotFound(topic_path)
140139
raise
141140

@@ -163,8 +162,8 @@ def topic_publish(self, topic_path, messages):
163162
try:
164163
result = self._gax_api.publish(topic_path, message_pbs,
165164
options=options)
166-
except GaxError as exc:
167-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
165+
except _Rendezvous as exc:
166+
if exc.code() == StatusCode.NOT_FOUND:
168167
raise NotFound(topic_path)
169168
raise
170169
return result.message_ids
@@ -201,8 +200,8 @@ def topic_list_subscriptions(self, topic_path, page_size=0,
201200
try:
202201
page_iter = self._gax_api.list_topic_subscriptions(
203202
topic_path, page_size=page_size, options=options)
204-
except GaxError as exc:
205-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
203+
except _Rendezvous as exc:
204+
if exc.code() == StatusCode.NOT_FOUND:
206205
raise NotFound(topic_path)
207206
raise
208207
subs = page_iter.next()
@@ -294,8 +293,8 @@ def subscription_create(self, subscription_path, topic_path,
294293
try:
295294
sub_pb = self._gax_api.create_subscription(
296295
subscription_path, topic_path, push_config, ack_deadline)
297-
except GaxError as exc:
298-
if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION:
296+
except _Rendezvous as exc:
297+
if exc.code() == StatusCode.FAILED_PRECONDITION:
299298
raise Conflict(topic_path)
300299
raise
301300
return _subscription_pb_to_mapping(sub_pb)
@@ -316,8 +315,8 @@ def subscription_get(self, subscription_path):
316315
"""
317316
try:
318317
sub_pb = self._gax_api.get_subscription(subscription_path)
319-
except GaxError as exc:
320-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
318+
except _Rendezvous as exc:
319+
if exc.code() == StatusCode.NOT_FOUND:
321320
raise NotFound(subscription_path)
322321
raise
323322
return _subscription_pb_to_mapping(sub_pb)
@@ -335,8 +334,8 @@ def subscription_delete(self, subscription_path):
335334
"""
336335
try:
337336
self._gax_api.delete_subscription(subscription_path)
338-
except GaxError as exc:
339-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
337+
except _Rendezvous as exc:
338+
if exc.code() == StatusCode.NOT_FOUND:
340339
raise NotFound(subscription_path)
341340
raise
342341

@@ -360,8 +359,8 @@ def subscription_modify_push_config(self, subscription_path,
360359
push_config = PushConfig(push_endpoint=push_endpoint)
361360
try:
362361
self._gax_api.modify_push_config(subscription_path, push_config)
363-
except GaxError as exc:
364-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
362+
except _Rendezvous as exc:
363+
if exc.code() == StatusCode.NOT_FOUND:
365364
raise NotFound(subscription_path)
366365
raise
367366

@@ -392,8 +391,8 @@ def subscription_pull(self, subscription_path, return_immediately=False,
392391
try:
393392
response_pb = self._gax_api.pull(
394393
subscription_path, max_messages, return_immediately)
395-
except GaxError as exc:
396-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
394+
except _Rendezvous as exc:
395+
if exc.code() == StatusCode.NOT_FOUND:
397396
raise NotFound(subscription_path)
398397
raise
399398
return [_received_message_pb_to_mapping(rmpb)
@@ -415,8 +414,8 @@ def subscription_acknowledge(self, subscription_path, ack_ids):
415414
"""
416415
try:
417416
self._gax_api.acknowledge(subscription_path, ack_ids)
418-
except GaxError as exc:
419-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
417+
except _Rendezvous as exc:
418+
if exc.code() == StatusCode.NOT_FOUND:
420419
raise NotFound(subscription_path)
421420
raise
422421

@@ -442,8 +441,8 @@ def subscription_modify_ack_deadline(self, subscription_path, ack_ids,
442441
try:
443442
self._gax_api.modify_ack_deadline(
444443
subscription_path, ack_ids, ack_deadline)
445-
except GaxError as exc:
446-
if exc_to_code(exc.cause) == StatusCode.NOT_FOUND:
444+
except _Rendezvous as exc:
445+
if exc.code() == StatusCode.NOT_FOUND:
447446
raise NotFound(subscription_path)
448447
raise
449448

@@ -520,7 +519,7 @@ def make_gax_publisher_api(connection):
520519
"""
521520
channel = None
522521
if connection.in_emulator:
523-
channel = insecure_channel(connection.host, None)
522+
channel = insecure_channel(connection.host)
524523
return PublisherApi(channel=channel)
525524

526525

@@ -540,5 +539,5 @@ def make_gax_subscriber_api(connection):
540539
"""
541540
channel = None
542541
if connection.in_emulator:
543-
channel = insecure_channel(connection.host, None)
542+
channel = insecure_channel(connection.host)
544543
return SubscriberApi(channel=channel)

system_tests/pubsub.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
import os
1616
import unittest
1717

18-
from google.gax.errors import GaxError
1918
from grpc import StatusCode
2019
from grpc._channel import _Rendezvous
2120
import httplib2
2221

2322
# pylint: disable=ungrouped-imports
24-
from google.cloud import _helpers
2523
from google.cloud.environment_vars import PUBSUB_EMULATOR
2624
from google.cloud.pubsub import client
2725
# pylint: enable=ungrouped-imports
@@ -34,10 +32,10 @@
3432

3533

3634
def _unavailable(exc):
37-
return _helpers.exc_to_code(exc) == StatusCode.UNAVAILABLE
35+
return exc.code() == StatusCode.UNAVAILABLE
3836

3937

40-
retry_unavailable = RetryErrors((GaxError, _Rendezvous), _unavailable)
38+
retry_unavailable = RetryErrors(_Rendezvous, _unavailable)
4139

4240

4341
class Config(object):

unit_tests/_testing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ class _GAXBaseAPI(object):
5757
def __init__(self, **kw):
5858
self.__dict__.update(kw)
5959

60-
def _make_grpc_error(self, status_code):
60+
def _make_grpc_error(self, status_code=None):
6161
from grpc._channel import _Rendezvous
6262
from grpc._channel import _RPCState
63+
from grpc import StatusCode
64+
65+
if status_code is None:
66+
status_code = StatusCode.UNKNOWN
6367

6468
details = 'Some error details.'
6569
exc_state = _RPCState((), None, None, status_code, details)

0 commit comments

Comments
 (0)