Skip to content

Commit f5af6bc

Browse files
author
travis-ci
committed
Expose RetryError exceptions.
1 parent c090fb5 commit f5af6bc

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

vision/google/cloud/vision/_gax.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

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

17+
from google.gax.errors import RetryError
18+
1719
from google.cloud.gapic.vision.v1 import image_annotator_client
1820
from google.cloud.proto.vision.v1 import image_annotator_pb2
1921

@@ -66,7 +68,12 @@ def annotate(self, images=None, requests_pb=None):
6668
requests = requests_pb
6769

6870
annotator_client = self._annotator_client
69-
responses = annotator_client.batch_annotate_images(requests).responses
71+
try:
72+
api_result = annotator_client.batch_annotate_images(requests)
73+
responses = api_result.responses
74+
except RetryError as exception:
75+
raise exception.cause.exception()
76+
7077
return [Annotations.from_pb(response) for response in responses]
7178

7279

vision/unit_tests/test__gax.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,32 @@ def test_annotate_with_pb_requests_results(self):
221221
self.assertIsInstance(annotation, Annotations)
222222
gax_api._annotator_client.batch_annotate_images.assert_called()
223223

224+
def test_handle_retry_error(self):
225+
from grpc._channel import _Rendezvous
226+
from google.gax.errors import GaxError
227+
from google.gax.errors import RetryError
228+
from google.cloud.proto.vision.v1 import image_annotator_pb2
229+
230+
client = mock.Mock(spec_set=['_credentials'])
231+
request = image_annotator_pb2.AnnotateImageRequest()
232+
233+
# In the case of a RetryError being raised, we want to get to the root
234+
# exception. i.e. RetryError.cause.exception()
235+
mock_rendezvous = mock.Mock(spec=_Rendezvous)
236+
mock_rendezvous.exception.return_value = GaxError('gax')
237+
238+
with mock.patch('google.cloud.vision._gax.image_annotator_client.'
239+
'ImageAnnotatorClient'):
240+
gax_api = self._make_one(client)
241+
242+
gax_api._annotator_client = mock.Mock(
243+
spec_set=['batch_annotate_images'])
244+
245+
gax_api._annotator_client.batch_annotate_images = mock.Mock(
246+
side_effect=RetryError('retry', cause=mock_rendezvous))
247+
with self.assertRaises(GaxError):
248+
gax_api.annotate(requests_pb=[request])
249+
224250

225251
class Test__to_gapic_feature(unittest.TestCase):
226252
def _call_fut(self, feature):

0 commit comments

Comments
 (0)