Skip to content

Commit 6fb2d73

Browse files
authored
Merge pull request #2336 from dhermes/fix-0.13.0-gax
Fixing broken exception handling after GAX 0.13.0 upgrade.
2 parents 5f1cda9 + b185ee8 commit 6fb2d73

File tree

15 files changed

+193
-232
lines changed

15 files changed

+193
-232
lines changed

google/cloud/_helpers.py

-19
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/bigtable/row_data.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class InvalidChunk(RuntimeError):
183183
class PartialRowsData(object):
184184
"""Convenience wrapper for consuming a ``ReadRows`` streaming response.
185185
186-
:type response_iterator: :class:`grpc._channel._Rendezvous`
186+
:type response_iterator: :class:`~google.cloud.exceptions.GrpcRendezvous`
187187
:param response_iterator: A streaming iterator returned from a
188188
``ReadRows`` request.
189189
"""

google/cloud/bigtable/table.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def sample_row_keys(self):
302302
samples would require space roughly equal to the difference in their
303303
``offset_bytes`` fields.
304304
305-
:rtype: :class:`grpc._channel._Rendezvous`
305+
:rtype: :class:`~google.cloud.exceptions.GrpcRendezvous`
306306
:returns: A cancel-able iterator. Can be consumed by calling ``next()``
307307
or by casting to a :class:`list` and can be cancelled by
308308
calling ``cancel()``.

google/cloud/datastore/connection.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,17 @@
2424
from google.cloud.environment_vars import DISABLE_GRPC
2525
from google.cloud.environment_vars import GCD_HOST
2626
from google.cloud.exceptions import Conflict
27+
from google.cloud.exceptions import GrpcRendezvous
2728
from google.cloud.exceptions import make_exception
2829
from google.cloud.datastore._generated import datastore_pb2 as _datastore_pb2
2930
# pylint: disable=ungrouped-imports
3031
try:
3132
from grpc import StatusCode
32-
from grpc._channel import _Rendezvous
3333
from google.cloud.datastore._generated import datastore_grpc_pb2
3434
except ImportError: # pragma: NO COVER
3535
_HAVE_GRPC = False
3636
datastore_grpc_pb2 = None
3737
StatusCode = None
38-
_Rendezvous = Exception
3938
else:
4039
_HAVE_GRPC = True
4140
# pylint: enable=ungrouped-imports
@@ -313,7 +312,7 @@ def commit(self, project, request_pb):
313312
request_pb.project_id = project
314313
try:
315314
return self._stub.Commit(request_pb)
316-
except _Rendezvous as exc:
315+
except GrpcRendezvous as exc:
317316
if exc.code() == StatusCode.ABORTED:
318317
raise Conflict(exc.details())
319318
raise

google/cloud/exceptions.py

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@
2323

2424
_HTTP_CODE_TO_EXCEPTION = {} # populated at end of module
2525

26+
try:
27+
from grpc._channel import _Rendezvous
28+
except ImportError: # pragma: NO COVER
29+
_Rendezvous = None
30+
31+
32+
# pylint: disable=invalid-name
33+
GrpcRendezvous = _Rendezvous
34+
"""Exception class raised by gRPC stable."""
35+
# pylint: enable=invalid-name
36+
2637

2738
class GoogleCloudError(Exception):
2839
"""Base error class for Google Cloud errors (abstract).

google/cloud/logging/_gax.py

+19-20
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
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
@@ -29,8 +28,8 @@
2928
# pylint: disable=ungrouped-imports
3029
from google.cloud._helpers import _datetime_to_pb_timestamp
3130
from google.cloud._helpers import _pb_timestamp_to_rfc3339
32-
from google.cloud._helpers import exc_to_code
3331
from google.cloud.exceptions import Conflict
32+
from google.cloud.exceptions import GrpcRendezvous
3433
from google.cloud.exceptions import NotFound
3534
# pylint: enable=ungrouped-imports
3635

@@ -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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous as exc:
417+
if exc.code() == StatusCode.NOT_FOUND:
419418
raise NotFound(path)
420419
raise
421420

google/cloud/pubsub/_gax.py

+28-29
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
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
2625

2726
# pylint: disable=ungrouped-imports
2827
from google.cloud._helpers import _to_bytes
29-
from google.cloud._helpers import exc_to_code
3028
from google.cloud._helpers import _pb_timestamp_to_rfc3339
3129
from google.cloud.exceptions import Conflict
30+
from google.cloud.exceptions import GrpcRendezvous
3231
from google.cloud.exceptions import NotFound
3332
# pylint: enable=ungrouped-imports
3433

@@ -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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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 GrpcRendezvous 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/bigtable.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ def _retry_on_unavailable(exc):
8989

9090

9191
def setUpModule():
92-
from grpc._channel import _Rendezvous
92+
from google.cloud.exceptions import GrpcRendezvous
93+
9394
Config.CLIENT = Client(admin=True)
9495
Config.INSTANCE = Config.CLIENT.instance(INSTANCE_ID, LOCATION_ID)
95-
retry = RetryErrors(_Rendezvous, error_predicate=_retry_on_unavailable)
96+
retry = RetryErrors(GrpcRendezvous, error_predicate=_retry_on_unavailable)
9697
instances, failed_locations = retry(Config.CLIENT.list_instances)()
9798

9899
if len(failed_locations) != 0:

0 commit comments

Comments
 (0)