Skip to content

Commit deddd82

Browse files
DumengBenRKarl
authored andcommitted
Fix async interceptor issues
1 parent 001e6ae commit deddd82

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

google/ads/googleads/interceptors/exception_interceptor.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ def __await__(self):
159159
return response
160160
else:
161161
return util.convert_proto_plus_to_protobuf(response)
162-
except grpc.RpcError:
163-
yield from self._interceptor._handle_grpc_failure_async(self._call).__await__()
162+
except grpc.RpcError as exception:
163+
yield from self._interceptor._handle_grpc_failure_async(self._call, exception).__await__()
164164
raise
165165

166166
def cancel(self):
@@ -209,8 +209,8 @@ async def _wrapped_aiter():
209209
yield response
210210
else:
211211
yield util.convert_proto_plus_to_protobuf(response)
212-
except grpc.RpcError:
213-
await self._interceptor._handle_grpc_failure_async(self._call)
212+
except grpc.RpcError as exception:
213+
await self._interceptor._handle_grpc_failure_async(self._call, exception)
214214
raise
215215
return _wrapped_aiter()
216216

@@ -252,19 +252,30 @@ async def read(self):
252252
return response
253253
else:
254254
return util.convert_proto_plus_to_protobuf(response)
255-
except grpc.RpcError:
256-
await self._interceptor._handle_grpc_failure_async(self._call)
255+
except grpc.RpcError as exception:
256+
await self._interceptor._handle_grpc_failure_async(self._call, exception)
257257
raise
258258

259-
class _AsyncExceptionInterceptor(
260-
ExceptionInterceptor,
261-
):
259+
class _AsyncExceptionInterceptor(Interceptor, grpc.aio.UnaryUnaryClientInterceptor, grpc.aio.UnaryStreamClientInterceptor,):
262260
"""An interceptor that wraps rpc exceptions."""
263261

264-
async def _handle_grpc_failure_async(self, response: grpc.aio.Call):
262+
def __init__(self, api_version: str, use_proto_plus: bool = False):
263+
"""Initializes the ExceptionInterceptor.
264+
265+
Args:
266+
api_version: a str of the API version of the request.
267+
use_proto_plus: a boolean of whether returned messages should be
268+
proto_plus or protobuf.
269+
"""
270+
super().__init__(api_version)
271+
self._api_version = api_version
272+
self._use_proto_plus = use_proto_plus
273+
274+
async def _handle_grpc_failure_async(self, response: grpc.aio.Call, exception: grpc.RpcError):
265275
"""Async version of _handle_grpc_failure."""
266-
status_code = response.code()
267-
response_exception = response.exception()
276+
status_code = await response.code()
277+
278+
response_exception = exception
268279

269280
# We need to access _RETRY_STATUS_CODES from interceptor module?
270281
# It's imported in interceptor.py but not exposed in ExceptionInterceptor?
@@ -300,7 +311,7 @@ async def _handle_grpc_failure_async(self, response: grpc.aio.Call):
300311
raise response_exception
301312

302313
# If we got here, maybe no exception? But we only call this on error.
303-
raise response.exception()
314+
raise response_exception
304315

305316
async def intercept_unary_unary(
306317
self,

google/ads/googleads/interceptors/logging_interceptor.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,10 +468,13 @@ async def _log_request_async(
468468
# Since this is called in on_done, it is done.
469469

470470
try:
471-
# This might raise if cancelled?
472-
exception = response.exception()
473-
except Exception:
474-
exception = None
471+
if hasattr(response, "exception"):
472+
exception = response.code()
473+
else:
474+
await response.code()
475+
exception = None
476+
except Exception as ex:
477+
exception = ex
475478

476479
if exception:
477480
# We need to adapt exception logging for async exception?

0 commit comments

Comments
 (0)