diff --git a/google/cloud/_helpers.py b/google/cloud/_helpers.py index a7da90880bbc..3b214db77b82 100644 --- a/google/cloud/_helpers.py +++ b/google/cloud/_helpers.py @@ -31,13 +31,9 @@ except ImportError: app_identity = None try: - from google.gax.grpc import exc_to_code as beta_exc_to_code import grpc - from grpc._channel import _Rendezvous except ImportError: # pragma: NO COVER - beta_exc_to_code = None grpc = None - _Rendezvous = Exception import six from six.moves import http_client from six.moves import configparser @@ -685,21 +681,6 @@ def make_insecure_stub(stub_class, host, port=None): return stub_class(channel) -def exc_to_code(exc): - """Retrieves the status code from a gRPC exception. - - :type exc: :class:`Exception` - :param exc: An exception from gRPC beta or stable. - - :rtype: :class:`grpc.StatusCode` - :returns: The status code attached to the exception. - """ - if isinstance(exc, _Rendezvous): - return exc.code() - else: - return beta_exc_to_code(exc) - - try: from pytz import UTC # pylint: disable=unused-import,wrong-import-order except ImportError: diff --git a/google/cloud/bigtable/row_data.py b/google/cloud/bigtable/row_data.py index add93113f6ad..91d457319973 100644 --- a/google/cloud/bigtable/row_data.py +++ b/google/cloud/bigtable/row_data.py @@ -183,7 +183,7 @@ class InvalidChunk(RuntimeError): class PartialRowsData(object): """Convenience wrapper for consuming a ``ReadRows`` streaming response. - :type response_iterator: :class:`grpc._channel._Rendezvous` + :type response_iterator: :class:`~google.cloud.exceptions.GrpcRendezvous` :param response_iterator: A streaming iterator returned from a ``ReadRows`` request. """ diff --git a/google/cloud/bigtable/table.py b/google/cloud/bigtable/table.py index 665491d7d0a7..6e3ac7eef370 100644 --- a/google/cloud/bigtable/table.py +++ b/google/cloud/bigtable/table.py @@ -302,7 +302,7 @@ def sample_row_keys(self): samples would require space roughly equal to the difference in their ``offset_bytes`` fields. - :rtype: :class:`grpc._channel._Rendezvous` + :rtype: :class:`~google.cloud.exceptions.GrpcRendezvous` :returns: A cancel-able iterator. Can be consumed by calling ``next()`` or by casting to a :class:`list` and can be cancelled by calling ``cancel()``. diff --git a/google/cloud/datastore/connection.py b/google/cloud/datastore/connection.py index 1f6416a1d39b..9382eb533176 100644 --- a/google/cloud/datastore/connection.py +++ b/google/cloud/datastore/connection.py @@ -24,18 +24,17 @@ from google.cloud.environment_vars import DISABLE_GRPC from google.cloud.environment_vars import GCD_HOST from google.cloud.exceptions import Conflict +from google.cloud.exceptions import GrpcRendezvous from google.cloud.exceptions import make_exception from google.cloud.datastore._generated import datastore_pb2 as _datastore_pb2 # pylint: disable=ungrouped-imports try: from grpc import StatusCode - from grpc._channel import _Rendezvous from google.cloud.datastore._generated import datastore_grpc_pb2 except ImportError: # pragma: NO COVER _HAVE_GRPC = False datastore_grpc_pb2 = None StatusCode = None - _Rendezvous = Exception else: _HAVE_GRPC = True # pylint: enable=ungrouped-imports @@ -313,7 +312,7 @@ def commit(self, project, request_pb): request_pb.project_id = project try: return self._stub.Commit(request_pb) - except _Rendezvous as exc: + except GrpcRendezvous as exc: if exc.code() == StatusCode.ABORTED: raise Conflict(exc.details()) raise diff --git a/google/cloud/exceptions.py b/google/cloud/exceptions.py index bc1349cb7d31..17b1e0e69860 100644 --- a/google/cloud/exceptions.py +++ b/google/cloud/exceptions.py @@ -23,6 +23,17 @@ _HTTP_CODE_TO_EXCEPTION = {} # populated at end of module +try: + from grpc._channel import _Rendezvous +except ImportError: # pragma: NO COVER + _Rendezvous = None + + +# pylint: disable=invalid-name +GrpcRendezvous = _Rendezvous +"""Exception class raised by gRPC stable.""" +# pylint: enable=invalid-name + class GoogleCloudError(Exception): """Base error class for Google Cloud errors (abstract). diff --git a/google/cloud/logging/_gax.py b/google/cloud/logging/_gax.py index 8f6c76572cf1..f84ef53b6f42 100644 --- a/google/cloud/logging/_gax.py +++ b/google/cloud/logging/_gax.py @@ -18,7 +18,6 @@ from google.gax import CallOptions from google.gax import INITIAL_PAGE -from google.gax.errors import GaxError from google.logging.type.log_severity_pb2 import LogSeverity from google.logging.v2.logging_config_pb2 import LogSink from google.logging.v2.logging_metrics_pb2 import LogMetric @@ -29,8 +28,8 @@ # pylint: disable=ungrouped-imports from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud._helpers import _pb_timestamp_to_rfc3339 -from google.cloud._helpers import exc_to_code from google.cloud.exceptions import Conflict +from google.cloud.exceptions import GrpcRendezvous from google.cloud.exceptions import NotFound # pylint: enable=ungrouped-imports @@ -123,8 +122,8 @@ def logger_delete(self, project, logger_name): path = 'projects/%s/logs/%s' % (project, logger_name) try: self._gax_api.delete_log(path, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise @@ -195,8 +194,8 @@ def sink_create(self, project, sink_name, filter_, destination): destination=destination) try: self._gax_api.create_sink(parent, sink_pb, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.FAILED_PRECONDITION: path = 'projects/%s/sinks/%s' % (project, sink_name) raise Conflict(path) raise @@ -218,8 +217,8 @@ def sink_get(self, project, sink_name): path = 'projects/%s/sinks/%s' % (project, sink_name) try: sink_pb = self._gax_api.get_sink(path, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise return _log_sink_pb_to_mapping(sink_pb) @@ -250,8 +249,8 @@ def sink_update(self, project, sink_name, filter_, destination): sink_pb = LogSink(name=path, filter=filter_, destination=destination) try: self._gax_api.update_sink(path, sink_pb, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise return _log_sink_pb_to_mapping(sink_pb) @@ -269,8 +268,8 @@ def sink_delete(self, project, sink_name): path = 'projects/%s/sinks/%s' % (project, sink_name) try: self._gax_api.delete_sink(path, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise @@ -340,8 +339,8 @@ def metric_create(self, project, metric_name, filter_, description): description=description) try: self._gax_api.create_log_metric(parent, metric_pb, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.FAILED_PRECONDITION: path = 'projects/%s/metrics/%s' % (project, metric_name) raise Conflict(path) raise @@ -363,8 +362,8 @@ def metric_get(self, project, metric_name): path = 'projects/%s/metrics/%s' % (project, metric_name) try: metric_pb = self._gax_api.get_log_metric(path, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise return _log_metric_pb_to_mapping(metric_pb) @@ -395,8 +394,8 @@ def metric_update(self, project, metric_name, filter_, description): description=description) try: self._gax_api.update_log_metric(path, metric_pb, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise return _log_metric_pb_to_mapping(metric_pb) @@ -414,8 +413,8 @@ def metric_delete(self, project, metric_name): path = 'projects/%s/metrics/%s' % (project, metric_name) try: self._gax_api.delete_log_metric(path, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise diff --git a/google/cloud/pubsub/_gax.py b/google/cloud/pubsub/_gax.py index 2a0b0393aa5b..c05d1f352518 100644 --- a/google/cloud/pubsub/_gax.py +++ b/google/cloud/pubsub/_gax.py @@ -18,17 +18,16 @@ from google.cloud.gapic.pubsub.v1.subscriber_api import SubscriberApi from google.gax import CallOptions from google.gax import INITIAL_PAGE -from google.gax.errors import GaxError from google.pubsub.v1.pubsub_pb2 import PubsubMessage from google.pubsub.v1.pubsub_pb2 import PushConfig -from grpc.beta.implementations import insecure_channel +from grpc import insecure_channel from grpc import StatusCode # pylint: disable=ungrouped-imports from google.cloud._helpers import _to_bytes -from google.cloud._helpers import exc_to_code from google.cloud._helpers import _pb_timestamp_to_rfc3339 from google.cloud.exceptions import Conflict +from google.cloud.exceptions import GrpcRendezvous from google.cloud.exceptions import NotFound # pylint: enable=ungrouped-imports @@ -93,8 +92,8 @@ def topic_create(self, topic_path): """ try: topic_pb = self._gax_api.create_topic(topic_path) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.FAILED_PRECONDITION: raise Conflict(topic_path) raise return {'name': topic_pb.name} @@ -116,8 +115,8 @@ def topic_get(self, topic_path): """ try: topic_pb = self._gax_api.get_topic(topic_path) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(topic_path) raise return {'name': topic_pb.name} @@ -134,8 +133,8 @@ def topic_delete(self, topic_path): """ try: self._gax_api.delete_topic(topic_path) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(topic_path) raise @@ -163,8 +162,8 @@ def topic_publish(self, topic_path, messages): try: result = self._gax_api.publish(topic_path, message_pbs, options=options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(topic_path) raise return result.message_ids @@ -201,8 +200,8 @@ def topic_list_subscriptions(self, topic_path, page_size=0, try: page_iter = self._gax_api.list_topic_subscriptions( topic_path, page_size=page_size, options=options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(topic_path) raise subs = page_iter.next() @@ -294,8 +293,8 @@ def subscription_create(self, subscription_path, topic_path, try: sub_pb = self._gax_api.create_subscription( subscription_path, topic_path, push_config, ack_deadline) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.FAILED_PRECONDITION: raise Conflict(topic_path) raise return _subscription_pb_to_mapping(sub_pb) @@ -316,8 +315,8 @@ def subscription_get(self, subscription_path): """ try: sub_pb = self._gax_api.get_subscription(subscription_path) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(subscription_path) raise return _subscription_pb_to_mapping(sub_pb) @@ -335,8 +334,8 @@ def subscription_delete(self, subscription_path): """ try: self._gax_api.delete_subscription(subscription_path) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(subscription_path) raise @@ -360,8 +359,8 @@ def subscription_modify_push_config(self, subscription_path, push_config = PushConfig(push_endpoint=push_endpoint) try: self._gax_api.modify_push_config(subscription_path, push_config) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(subscription_path) raise @@ -392,8 +391,8 @@ def subscription_pull(self, subscription_path, return_immediately=False, try: response_pb = self._gax_api.pull( subscription_path, max_messages, return_immediately) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(subscription_path) raise return [_received_message_pb_to_mapping(rmpb) @@ -415,8 +414,8 @@ def subscription_acknowledge(self, subscription_path, ack_ids): """ try: self._gax_api.acknowledge(subscription_path, ack_ids) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(subscription_path) raise @@ -442,8 +441,8 @@ def subscription_modify_ack_deadline(self, subscription_path, ack_ids, try: self._gax_api.modify_ack_deadline( subscription_path, ack_ids, ack_deadline) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except GrpcRendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(subscription_path) raise @@ -520,7 +519,7 @@ def make_gax_publisher_api(connection): """ channel = None if connection.in_emulator: - channel = insecure_channel(connection.host, None) + channel = insecure_channel(connection.host) return PublisherApi(channel=channel) @@ -540,5 +539,5 @@ def make_gax_subscriber_api(connection): """ channel = None if connection.in_emulator: - channel = insecure_channel(connection.host, None) + channel = insecure_channel(connection.host) return SubscriberApi(channel=channel) diff --git a/system_tests/bigtable.py b/system_tests/bigtable.py index ae92bf5956c6..6ef94f91c0a1 100644 --- a/system_tests/bigtable.py +++ b/system_tests/bigtable.py @@ -89,10 +89,11 @@ def _retry_on_unavailable(exc): def setUpModule(): - from grpc._channel import _Rendezvous + from google.cloud.exceptions import GrpcRendezvous + Config.CLIENT = Client(admin=True) Config.INSTANCE = Config.CLIENT.instance(INSTANCE_ID, LOCATION_ID) - retry = RetryErrors(_Rendezvous, error_predicate=_retry_on_unavailable) + retry = RetryErrors(GrpcRendezvous, error_predicate=_retry_on_unavailable) instances, failed_locations = retry(Config.CLIENT.list_instances)() if len(failed_locations) != 0: diff --git a/system_tests/logging_.py b/system_tests/logging_.py index 3ba39edc8912..5292c0afce64 100644 --- a/system_tests/logging_.py +++ b/system_tests/logging_.py @@ -73,9 +73,9 @@ def _logger_name(): return 'system-tests-logger' + unique_resource_id('-') def _list_entries(self, logger): - from grpc._channel import _Rendezvous + from google.cloud.exceptions import GrpcRendezvous inner = RetryResult(_has_entries)(logger.list_entries) - outer = RetryErrors(_Rendezvous, _retry_on_unavailable)(inner) + outer = RetryErrors(GrpcRendezvous, _retry_on_unavailable)(inner) return outer() def test_log_text(self): diff --git a/system_tests/pubsub.py b/system_tests/pubsub.py index 2d512dd38002..98158433e5a5 100644 --- a/system_tests/pubsub.py +++ b/system_tests/pubsub.py @@ -15,14 +15,12 @@ import os import unittest -from google.gax.errors import GaxError from grpc import StatusCode -from grpc._channel import _Rendezvous import httplib2 # pylint: disable=ungrouped-imports -from google.cloud import _helpers from google.cloud.environment_vars import PUBSUB_EMULATOR +from google.cloud.exceptions import GrpcRendezvous from google.cloud.pubsub import client # pylint: enable=ungrouped-imports @@ -34,10 +32,10 @@ def _unavailable(exc): - return _helpers.exc_to_code(exc) == StatusCode.UNAVAILABLE + return exc.code() == StatusCode.UNAVAILABLE -retry_unavailable = RetryErrors((GaxError, _Rendezvous), _unavailable) +retry_unavailable = RetryErrors(GrpcRendezvous, _unavailable) class Config(object): diff --git a/unit_tests/_testing.py b/unit_tests/_testing.py index d3a1a268b5b4..f181b7c94a6b 100644 --- a/unit_tests/_testing.py +++ b/unit_tests/_testing.py @@ -57,13 +57,17 @@ class _GAXBaseAPI(object): def __init__(self, **kw): self.__dict__.update(kw) - def _make_grpc_error(self, status_code): - from grpc._channel import _Rendezvous + def _make_grpc_error(self, status_code=None): from grpc._channel import _RPCState + from grpc import StatusCode + from google.cloud.exceptions import GrpcRendezvous + + if status_code is None: + status_code = StatusCode.UNKNOWN details = 'Some error details.' exc_state = _RPCState((), None, None, status_code, details) - return _Rendezvous(exc_state, None, None, None) + return GrpcRendezvous(exc_state, None, None, None) def _make_grpc_not_found(self): from grpc import StatusCode diff --git a/unit_tests/datastore/test_connection.py b/unit_tests/datastore/test_connection.py index 0b80815b4697..244975f063f0 100644 --- a/unit_tests/datastore/test_connection.py +++ b/unit_tests/datastore/test_connection.py @@ -243,24 +243,24 @@ def _commit_failure_helper(self, exc, err_class): @unittest.skipUnless(_HAVE_GRPC, 'No gRPC') def test_commit_failure_aborted(self): from grpc import StatusCode - from grpc._channel import _Rendezvous from grpc._channel import _RPCState from google.cloud.exceptions import Conflict + from google.cloud.exceptions import GrpcRendezvous details = 'Bad things.' exc_state = _RPCState((), None, None, StatusCode.ABORTED, details) - exc = _Rendezvous(exc_state, None, None, None) + exc = GrpcRendezvous(exc_state, None, None, None) self._commit_failure_helper(exc, Conflict) @unittest.skipUnless(_HAVE_GRPC, 'No gRPC') def test_commit_failure_cancelled(self): from grpc import StatusCode - from grpc._channel import _Rendezvous from grpc._channel import _RPCState + from google.cloud.exceptions import GrpcRendezvous exc_state = _RPCState((), None, None, StatusCode.CANCELLED, None) - exc = _Rendezvous(exc_state, None, None, None) - self._commit_failure_helper(exc, _Rendezvous) + exc = GrpcRendezvous(exc_state, None, None, None) + self._commit_failure_helper(exc, GrpcRendezvous) @unittest.skipUnless(_HAVE_GRPC, 'No gRPC') def test_commit_failure_non_grpc_err(self): diff --git a/unit_tests/logging/test__gax.py b/unit_tests/logging/test__gax.py index 13880bfed1b1..f406b178d4ac 100644 --- a/unit_tests/logging/test__gax.py +++ b/unit_tests/logging/test__gax.py @@ -423,12 +423,13 @@ def test_logger_delete_not_found(self): self.assertEqual(options, None) def test_logger_delete_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + LOG_PATH = 'projects/%s/logs/%s' % (self.PROJECT, self.LOG_NAME) gax_api = _GAXLoggingAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.logger_delete(self.PROJECT, self.LOG_NAME) log_name, options = gax_api._delete_log_called_with @@ -503,11 +504,12 @@ def test_list_sinks_w_paging(self): self.assertEqual(options.page_token, TOKEN) def test_sink_create_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXSinksAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.sink_create( self.PROJECT, self.SINK_NAME, self.FILTER, self.DESTINATION_URI) @@ -548,11 +550,12 @@ def test_sink_get_error(self): api.sink_get(self.PROJECT, self.SINK_NAME) def test_sink_get_miss(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXSinksAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.sink_get(self.PROJECT, self.SINK_NAME) def test_sink_get_hit(self): @@ -575,11 +578,12 @@ def test_sink_get_hit(self): self.assertEqual(options, None) def test_sink_update_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXSinksAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.sink_update( self.PROJECT, self.SINK_NAME, self.FILTER, self.DESTINATION_URI) @@ -614,11 +618,12 @@ def test_sink_update_hit(self): self.assertEqual(options, None) def test_sink_delete_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXSinksAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.sink_delete(self.PROJECT, self.SINK_NAME) def test_sink_delete_miss(self): @@ -707,11 +712,12 @@ def test_list_metrics_w_paging(self): self.assertEqual(options.page_token, TOKEN) def test_metric_create_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXMetricsAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.metric_create( self.PROJECT, self.METRIC_NAME, self.FILTER, self.DESCRIPTION) @@ -752,11 +758,12 @@ def test_metric_get_error(self): api.metric_get(self.PROJECT, self.METRIC_NAME) def test_metric_get_miss(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXMetricsAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.metric_get(self.PROJECT, self.METRIC_NAME) def test_metric_get_hit(self): @@ -779,11 +786,12 @@ def test_metric_get_hit(self): self.assertEqual(options, None) def test_metric_update_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXMetricsAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.metric_update( self.PROJECT, self.METRIC_NAME, self.FILTER, self.DESCRIPTION) @@ -818,11 +826,12 @@ def test_metric_update_hit(self): self.assertEqual(options, None) def test_metric_delete_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXMetricsAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.metric_delete(self.PROJECT, self.METRIC_NAME) def test_metric_delete_miss(self): @@ -929,12 +938,11 @@ def write_log_entries(self, entries, log_name, resource, labels, entries, log_name, resource, labels, partial_success, options) def delete_log(self, log_name, options): - from google.gax.errors import GaxError self._delete_log_called_with = log_name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._delete_not_found: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() class _GAXSinksAPI(_GAXBaseAPI): @@ -947,40 +955,36 @@ def list_sinks(self, parent, page_size, options): return self._list_sinks_response def create_sink(self, parent, sink, options): - from google.gax.errors import GaxError self._create_sink_called_with = parent, sink, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._create_sink_conflict: - raise GaxError('conflict', self._make_grpc_failed_precondition()) + raise self._make_grpc_failed_precondition() def get_sink(self, sink_name, options): - from google.gax.errors import GaxError self._get_sink_called_with = sink_name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._get_sink_response except AttributeError: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def update_sink(self, sink_name, sink, options=None): - from google.gax.errors import GaxError self._update_sink_called_with = sink_name, sink, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._update_sink_response except AttributeError: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def delete_sink(self, sink_name, options=None): - from google.gax.errors import GaxError self._delete_sink_called_with = sink_name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._sink_not_found: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() class _GAXMetricsAPI(_GAXBaseAPI): @@ -993,40 +997,36 @@ def list_log_metrics(self, parent, page_size, options): return self._list_log_metrics_response def create_log_metric(self, parent, metric, options): - from google.gax.errors import GaxError self._create_log_metric_called_with = parent, metric, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._create_log_metric_conflict: - raise GaxError('conflict', self._make_grpc_failed_precondition()) + raise self._make_grpc_failed_precondition() def get_log_metric(self, metric_name, options): - from google.gax.errors import GaxError self._get_log_metric_called_with = metric_name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._get_log_metric_response except AttributeError: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def update_log_metric(self, metric_name, metric, options=None): - from google.gax.errors import GaxError self._update_log_metric_called_with = metric_name, metric, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._update_log_metric_response except AttributeError: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def delete_log_metric(self, metric_name, options=None): - from google.gax.errors import GaxError self._delete_log_metric_called_with = metric_name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._log_metric_not_found: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() class _HTTPRequestPB(object): diff --git a/unit_tests/pubsub/test__gax.py b/unit_tests/pubsub/test__gax.py index a49af1b6f46a..794db56f9738 100644 --- a/unit_tests/pubsub/test__gax.py +++ b/unit_tests/pubsub/test__gax.py @@ -123,11 +123,12 @@ def test_topic_create_already_exists(self): self.assertEqual(options, None) def test_topic_create_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXPublisherAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.topic_create(self.TOPIC_PATH) topic_path, options = gax_api._create_topic_called_with @@ -159,11 +160,12 @@ def test_topic_get_miss(self): self.assertEqual(options, None) def test_topic_get_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXPublisherAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.topic_get(self.TOPIC_PATH) topic_path, options = gax_api._get_topic_called_with @@ -193,11 +195,12 @@ def test_topic_delete_miss(self): self.assertEqual(options, None) def test_topic_delete_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXPublisherAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.topic_delete(self.TOPIC_PATH) topic_path, options = gax_api._delete_topic_called_with @@ -245,14 +248,15 @@ def test_topic_publish_miss_w_attrs_w_bytes_payload(self): def test_topic_publish_error(self): import base64 - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + PAYLOAD = b'This is the message text' B64 = base64.b64encode(PAYLOAD).decode('ascii') MESSAGE = {'data': B64, 'attributes': {}} gax_api = _GAXPublisherAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.topic_publish(self.TOPIC_PATH, [MESSAGE]) topic_path, message_pbs, options = gax_api._publish_called_with @@ -329,11 +333,12 @@ def test_topic_list_subscriptions_miss(self): def test_topic_list_subscriptions_error(self): from google.gax import INITIAL_PAGE - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXPublisherAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.topic_list_subscriptions(self.TOPIC_PATH) topic_path, page_size, options = ( @@ -450,11 +455,12 @@ def test_subscription_create_already_exists(self): self.assertEqual(options, None) def test_subscription_create_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.subscription_create(self.SUB_PATH, self.TOPIC_PATH) name, topic, push_config, ack_deadline, options = ( @@ -499,11 +505,12 @@ def test_subscription_get_miss(self): self.assertEqual(options, None) def test_subscription_get_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.subscription_get(self.SUB_PATH) sub_path, options = gax_api._get_subscription_called_with @@ -533,11 +540,12 @@ def test_subscription_delete_miss(self): self.assertEqual(options, None) def test_subscription_delete_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.subscription_delete(self.TOPIC_PATH) sub_path, options = gax_api._delete_subscription_called_with @@ -570,11 +578,12 @@ def test_subscription_modify_push_config_miss(self): self.assertEqual(options, None) def test_subscription_modify_push_config_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.subscription_modify_push_config( self.SUB_PATH, self.PUSH_ENDPOINT) @@ -636,11 +645,12 @@ def test_subscription_pull_defaults_miss(self): self.assertEqual(options, None) def test_subscription_pull_defaults_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.subscription_pull(self.SUB_PATH) sub_path, max_messages, return_immediately, options = ( @@ -679,13 +689,14 @@ def test_subscription_acknowledge_miss(self): self.assertEqual(options, None) def test_subscription_acknowledge_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + ACK_ID1 = 'DEADBEEF' ACK_ID2 = 'BEADCAFE' gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.subscription_acknowledge(self.SUB_PATH, [ACK_ID1, ACK_ID2]) sub_path, ack_ids, options = gax_api._acknowledge_called_with @@ -730,14 +741,15 @@ def test_subscription_modify_ack_deadline_miss(self): self.assertEqual(options, None) def test_subscription_modify_ack_deadline_error(self): - from google.gax.errors import GaxError + from google.cloud.exceptions import GrpcRendezvous + ACK_ID1 = 'DEADBEEF' ACK_ID2 = 'BEADCAFE' NEW_DEADLINE = 90 gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(GrpcRendezvous): api.subscription_modify_ack_deadline( self.SUB_PATH, [ACK_ID1, ACK_ID2], NEW_DEADLINE) @@ -787,8 +799,8 @@ def mock_publisher_api(channel): channels.append(channel) return mock_result - def mock_insecure_channel(host, port): - insecure_args.append((host, port)) + def mock_insecure_channel(host): + insecure_args.append(host) return mock_channel host = 'CURR_HOST:1234' @@ -799,7 +811,7 @@ def mock_insecure_channel(host, port): self.assertIs(result, mock_result) self.assertEqual(channels, [mock_channel]) - self.assertEqual(insecure_args, [(host, None)]) + self.assertEqual(insecure_args, [host]) @unittest.skipUnless(_HAVE_GAX, 'No gax-python') @@ -840,8 +852,8 @@ def mock_subscriber_api(channel): channels.append(channel) return mock_result - def mock_insecure_channel(host, port): - insecure_args.append((host, port)) + def mock_insecure_channel(host): + insecure_args.append(host) return mock_channel host = 'CURR_HOST:1234' @@ -852,7 +864,7 @@ def mock_insecure_channel(host, port): self.assertIs(result, mock_result) self.assertEqual(channels, [mock_channel]) - self.assertEqual(insecure_args, [(host, None)]) + self.assertEqual(insecure_args, [host]) class _GAXPublisherAPI(_GAXBaseAPI): @@ -864,51 +876,46 @@ def list_topics(self, name, page_size, options): return self._list_topics_response def create_topic(self, name, options=None): - from google.gax.errors import GaxError self._create_topic_called_with = name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._create_topic_conflict: - raise GaxError('conflict', self._make_grpc_failed_precondition()) + raise self._make_grpc_failed_precondition() return self._create_topic_response def get_topic(self, name, options=None): - from google.gax.errors import GaxError self._get_topic_called_with = name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._get_topic_response except AttributeError: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def delete_topic(self, name, options=None): - from google.gax.errors import GaxError self._delete_topic_called_with = name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if not self._delete_topic_ok: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def publish(self, topic, messages, options=None): - from google.gax.errors import GaxError self._publish_called_with = topic, messages, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._publish_response except AttributeError: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def list_topic_subscriptions(self, topic, page_size, options=None): - from google.gax.errors import GaxError self._list_topic_subscriptions_called_with = topic, page_size, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._list_topic_subscriptions_response except AttributeError: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() class _GAXSubscriberAPI(_GAXBaseAPI): @@ -925,68 +932,61 @@ def list_subscriptions(self, project, page_size, options=None): def create_subscription(self, name, topic, push_config, ack_deadline_seconds, options=None): - from google.gax.errors import GaxError self._create_subscription_called_with = ( name, topic, push_config, ack_deadline_seconds, options) if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._create_subscription_conflict: - raise GaxError('conflict', self._make_grpc_failed_precondition()) + raise self._make_grpc_failed_precondition() return self._create_subscription_response def get_subscription(self, name, options=None): - from google.gax.errors import GaxError self._get_subscription_called_with = name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._get_subscription_response except AttributeError: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def delete_subscription(self, name, options=None): - from google.gax.errors import GaxError self._delete_subscription_called_with = name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if not self._delete_subscription_ok: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def modify_push_config(self, name, push_config, options=None): - from google.gax.errors import GaxError self._modify_push_config_called_with = name, push_config, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if not self._modify_push_config_ok: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def pull(self, name, max_messages, return_immediately, options=None): - from google.gax.errors import GaxError self._pull_called_with = ( name, max_messages, return_immediately, options) if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._pull_response except AttributeError: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def acknowledge(self, name, ack_ids, options=None): - from google.gax.errors import GaxError self._acknowledge_called_with = name, ack_ids, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if not self._acknowledge_ok: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def modify_ack_deadline(self, name, ack_ids, deadline, options=None): - from google.gax.errors import GaxError self._modify_ack_deadline_called_with = ( name, ack_ids, deadline, options) if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if not self._modify_ack_deadline_ok: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() class _TopicPB(object): diff --git a/unit_tests/test__helpers.py b/unit_tests/test__helpers.py index 908f5cd49344..ccebcc208a95 100644 --- a/unit_tests/test__helpers.py +++ b/unit_tests/test__helpers.py @@ -1025,37 +1025,6 @@ def test_without_port_argument(self): self._helper(host, host) -class Test_exc_to_code(unittest.TestCase): - - def _callFUT(self, exc): - from google.cloud._helpers import exc_to_code - return exc_to_code(exc) - - def test_with_stable(self): - from grpc._channel import _Rendezvous - from grpc._channel import _RPCState - from grpc import StatusCode - - status_code = StatusCode.FAILED_PRECONDITION - exc_state = _RPCState((), None, None, status_code, None) - exc = _Rendezvous(exc_state, None, None, None) - result = self._callFUT(exc) - self.assertEqual(result, status_code) - - def test_with_beta(self): - from grpc import StatusCode - from grpc.framework.interfaces.face.face import AbortionError - - status_code = StatusCode.UNIMPLEMENTED - exc = AbortionError(None, None, status_code, None) - result = self._callFUT(exc) - self.assertEqual(result, status_code) - - def test_with_none(self): - result = self._callFUT(None) - self.assertIsNone(result) - - class _AppIdentity(object): def __init__(self, app_id):