Skip to content

Commit 2acda27

Browse files
authored
Merge pull request #2496 from dhermes/fix-2494
Catching gRPC error in datastore run_query and converting to our error.
2 parents 83c197c + 8c21ded commit 2acda27

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

datastore/google/cloud/datastore/connection.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,13 @@ def run_query(self, project, request_pb):
276276
:returns: The returned protobuf response object.
277277
"""
278278
request_pb.project_id = project
279-
return self._stub.RunQuery(request_pb)
279+
try:
280+
return self._stub.RunQuery(request_pb)
281+
except GrpcRendezvous as exc:
282+
error_code = exc.code()
283+
if error_code == StatusCode.INVALID_ARGUMENT:
284+
raise BadRequest(exc.details())
285+
raise
280286

281287
def begin_transaction(self, project, request_pb):
282288
"""Perform a ``beginTransaction`` request.

datastore/unit_tests/test_connection.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,43 @@ def test_run_query(self):
200200
self.assertEqual(stub.method_calls,
201201
[(request_pb, 'RunQuery')])
202202

203+
def _run_query_failure_helper(self, exc, err_class):
204+
stub = _GRPCStub(side_effect=exc)
205+
datastore_api = self._makeOne(stub=stub)
206+
207+
request_pb = _RequestPB()
208+
project = 'PROJECT'
209+
with self.assertRaises(err_class):
210+
datastore_api.run_query(project, request_pb)
211+
212+
self.assertEqual(request_pb.project_id, project)
213+
self.assertEqual(stub.method_calls,
214+
[(request_pb, 'RunQuery')])
215+
216+
@unittest.skipUnless(_HAVE_GRPC, 'No gRPC')
217+
def test_run_query_invalid_argument(self):
218+
from grpc import StatusCode
219+
from grpc._channel import _RPCState
220+
from google.cloud.exceptions import BadRequest
221+
from google.cloud.exceptions import GrpcRendezvous
222+
223+
details = ('Cannot have inequality filters on multiple '
224+
'properties: [created, priority]')
225+
exc_state = _RPCState((), None, None,
226+
StatusCode.INVALID_ARGUMENT, details)
227+
exc = GrpcRendezvous(exc_state, None, None, None)
228+
self._run_query_failure_helper(exc, BadRequest)
229+
230+
@unittest.skipUnless(_HAVE_GRPC, 'No gRPC')
231+
def test_run_query_cancelled(self):
232+
from grpc import StatusCode
233+
from grpc._channel import _RPCState
234+
from google.cloud.exceptions import GrpcRendezvous
235+
236+
exc_state = _RPCState((), None, None, StatusCode.CANCELLED, None)
237+
exc = GrpcRendezvous(exc_state, None, None, None)
238+
self._run_query_failure_helper(exc, GrpcRendezvous)
239+
203240
def test_begin_transaction(self):
204241
return_val = object()
205242
stub = _GRPCStub(return_val)
@@ -1130,7 +1167,11 @@ def Lookup(self, request_pb):
11301167
return self._method(request_pb, 'Lookup')
11311168

11321169
def RunQuery(self, request_pb):
1133-
return self._method(request_pb, 'RunQuery')
1170+
result = self._method(request_pb, 'RunQuery')
1171+
if self.side_effect is Exception:
1172+
return result
1173+
else:
1174+
raise self.side_effect
11341175

11351176
def BeginTransaction(self, request_pb):
11361177
return self._method(request_pb, 'BeginTransaction')

0 commit comments

Comments
 (0)