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

Expose RetryError exceptions. #3087

Closed
Closed
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
9 changes: 8 additions & 1 deletion vision/google/cloud/vision/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

"""GAX Client for interacting with the Google Cloud Vision API."""

from google.gax.errors import RetryError

from google.cloud.gapic.vision.v1 import image_annotator_client
from google.cloud.proto.vision.v1 import image_annotator_pb2

Expand Down Expand Up @@ -66,7 +68,12 @@ def annotate(self, images=None, requests_pb=None):
requests = requests_pb

annotator_client = self._annotator_client
responses = annotator_client.batch_annotate_images(requests).responses
try:
api_result = annotator_client.batch_annotate_images(requests)
responses = api_result.responses
except RetryError as exception:

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

raise exception.cause.exception()

return [Annotations.from_pb(response) for response in responses]


Expand Down
26 changes: 26 additions & 0 deletions vision/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,32 @@ def test_annotate_with_pb_requests_results(self):
self.assertIsInstance(annotation, Annotations)
gax_api._annotator_client.batch_annotate_images.assert_called()

def test_handle_retry_error(self):
from grpc._channel import _Rendezvous
from google.gax.errors import GaxError
from google.gax.errors import RetryError
from google.cloud.proto.vision.v1 import image_annotator_pb2

client = mock.Mock(spec_set=['_credentials'])
request = image_annotator_pb2.AnnotateImageRequest()

# In the case of a RetryError being raised, we want to get to the root
# exception. i.e. RetryError.cause.exception()
mock_rendezvous = mock.Mock(spec=_Rendezvous)
mock_rendezvous.exception.return_value = GaxError('gax')

with mock.patch('google.cloud.vision._gax.image_annotator_client.'
'ImageAnnotatorClient'):
gax_api = self._make_one(client)

gax_api._annotator_client = mock.Mock(
spec_set=['batch_annotate_images'])

gax_api._annotator_client.batch_annotate_images = mock.Mock(
side_effect=RetryError('retry', cause=mock_rendezvous))
with self.assertRaises(GaxError):
gax_api.annotate(requests_pb=[request])


class Test__to_gapic_feature(unittest.TestCase):
def _call_fut(self, feature):
Expand Down