1616
1717
1818import contextlib
19+ import sys
1920
2021from google .cloud .gapic .datastore .v1 import datastore_client
2122from google .cloud .proto .datastore .v1 import datastore_pb2_grpc
23+ from google .gax .errors import GaxError
24+ from google .gax .grpc import exc_to_code
2225from google .gax .utils import metrics
2326from grpc import StatusCode
2427import six
@@ -71,13 +74,22 @@ def _grpc_catch_rendezvous():
7174 """
7275 try :
7376 yield
77+ except GaxError as exc :
78+ error_code = exc_to_code (exc .cause )
79+ error_class = _GRPC_ERROR_MAPPING .get (error_code )
80+ if error_class is None :
81+ raise
82+ else :
83+ new_exc = error_class (exc .cause .details ())
84+ six .reraise (error_class , new_exc , sys .exc_info ()[2 ])
7485 except exceptions .GrpcRendezvous as exc :
7586 error_code = exc .code ()
7687 error_class = _GRPC_ERROR_MAPPING .get (error_code )
7788 if error_class is None :
7889 raise
7990 else :
80- raise error_class (exc .details ())
91+ new_exc = error_class (exc .details ())
92+ six .reraise (error_class , new_exc , sys .exc_info ()[2 ])
8193
8294
8395class _DatastoreAPIOverGRPC (object ):
@@ -158,56 +170,38 @@ def begin_transaction(self, project, request_pb):
158170 with _grpc_catch_rendezvous ():
159171 return self ._stub .BeginTransaction (request_pb )
160172
161- def commit (self , project , request_pb ):
162- """Perform a ``commit`` request.
163-
164- :type project: str
165- :param project: The project to connect to. This is
166- usually your project name in the cloud console.
167-
168- :type request_pb: :class:`.datastore_pb2.CommitRequest`
169- :param request_pb: The request protobuf object.
170173
171- :rtype: :class:`.datastore_pb2.CommitResponse`
172- :returns: The returned protobuf response object.
173- """
174- request_pb .project_id = project
175- with _grpc_catch_rendezvous ():
176- return self ._stub .Commit (request_pb )
174+ class GAPICDatastoreAPI (datastore_client .DatastoreClient ):
175+ """An API object that sends proto-over-gRPC requests.
177176
178- def rollback ( self , project , request_pb ):
179- """Perform a ``rollback`` request .
177+ A light wrapper around the parent class, with exception re-mapping
178+ provided (from GaxError to our native errors) .
180179
181- :type project: str
182- :param project: The project to connect to. This is
183- usually your project name in the cloud console.
180+ :type args: tuple
181+ :param args: Positional arguments to pass to constructor.
184182
185- :type request_pb: :class:`.datastore_pb2.RollbackRequest`
186- :param request_pb: The request protobuf object.
183+ :type kwargs: dict
184+ :param kwargs: Keyword arguments to pass to constructor.
185+ """
187186
188- :rtype: :class:`.datastore_pb2.RollbackResponse`
189- :returns: The returned protobuf response object.
190- """
191- request_pb .project_id = project
192- with _grpc_catch_rendezvous ():
193- return self ._stub .Rollback (request_pb )
187+ def commit (self , * args , ** kwargs ):
188+ """Perform a ``commit`` request.
194189
195- def allocate_ids (self , project , request_pb ):
196- """Perform an ``allocateIds`` request.
190+ A light wrapper around the the base method from the parent class.
191+ Intended to provide exception re-mapping (from GaxError to our
192+ native errors).
197193
198- :type project: str
199- :param project: The project to connect to. This is
200- usually your project name in the cloud console.
194+ :type args: tuple
195+ :param args: Positional arguments to pass to base method.
201196
202- :type request_pb: :class:`.datastore_pb2.AllocateIdsRequest`
203- :param request_pb: The request protobuf object .
197+ :type kwargs: dict
198+ :param kwargs: Keyword arguments to pass to base method .
204199
205- :rtype: :class:`.datastore_pb2.AllocateIdsResponse `
200+ :rtype: :class:`.datastore_pb2.CommitResponse `
206201 :returns: The returned protobuf response object.
207202 """
208- request_pb .project_id = project
209203 with _grpc_catch_rendezvous ():
210- return self . _stub . AllocateIds ( request_pb )
204+ return super ( GAPICDatastoreAPI , self ). commit ( * args , ** kwargs )
211205
212206
213207def make_datastore_api (client ):
@@ -222,5 +216,5 @@ def make_datastore_api(client):
222216 channel = make_secure_channel (
223217 client ._credentials , DEFAULT_USER_AGENT ,
224218 datastore_client .DatastoreClient .SERVICE_ADDRESS )
225- return datastore_client . DatastoreClient (
219+ return GAPICDatastoreAPI (
226220 channel = channel , lib_name = 'gccl' , lib_version = __version__ )
0 commit comments