diff --git a/google/ads/googleads/interceptors/exception_interceptor.py b/google/ads/googleads/interceptors/exception_interceptor.py index 5fd3c21cd..3d0fc6d26 100644 --- a/google/ads/googleads/interceptors/exception_interceptor.py +++ b/google/ads/googleads/interceptors/exception_interceptor.py @@ -159,8 +159,8 @@ def __await__(self): return response else: return util.convert_proto_plus_to_protobuf(response) - except grpc.RpcError: - yield from self._interceptor._handle_grpc_failure_async(self._call).__await__() + except grpc.RpcError as exception: + yield from self._interceptor._handle_grpc_failure_async(self._call, exception).__await__() raise def cancel(self): @@ -209,8 +209,8 @@ async def _wrapped_aiter(): yield response else: yield util.convert_proto_plus_to_protobuf(response) - except grpc.RpcError: - await self._interceptor._handle_grpc_failure_async(self._call) + except grpc.RpcError as exception: + await self._interceptor._handle_grpc_failure_async(self._call, exception) raise return _wrapped_aiter() @@ -252,19 +252,30 @@ async def read(self): return response else: return util.convert_proto_plus_to_protobuf(response) - except grpc.RpcError: - await self._interceptor._handle_grpc_failure_async(self._call) + except grpc.RpcError as exception: + await self._interceptor._handle_grpc_failure_async(self._call, exception) raise -class _AsyncExceptionInterceptor( - ExceptionInterceptor, -): +class _AsyncExceptionInterceptor(Interceptor, grpc.aio.UnaryUnaryClientInterceptor, grpc.aio.UnaryStreamClientInterceptor,): """An interceptor that wraps rpc exceptions.""" - async def _handle_grpc_failure_async(self, response: grpc.aio.Call): + def __init__(self, api_version: str, use_proto_plus: bool = False): + """Initializes the ExceptionInterceptor. + + Args: + api_version: a str of the API version of the request. + use_proto_plus: a boolean of whether returned messages should be + proto_plus or protobuf. + """ + super().__init__(api_version) + self._api_version = api_version + self._use_proto_plus = use_proto_plus + + async def _handle_grpc_failure_async(self, response: grpc.aio.Call, exception: grpc.RpcError): """Async version of _handle_grpc_failure.""" - status_code = response.code() - response_exception = response.exception() + status_code = await response.code() + + response_exception = exception # We need to access _RETRY_STATUS_CODES from interceptor module? # 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): raise response_exception # If we got here, maybe no exception? But we only call this on error. - raise response.exception() + raise response_exception async def intercept_unary_unary( self, diff --git a/google/ads/googleads/interceptors/logging_interceptor.py b/google/ads/googleads/interceptors/logging_interceptor.py index c57108b48..6e5c47783 100644 --- a/google/ads/googleads/interceptors/logging_interceptor.py +++ b/google/ads/googleads/interceptors/logging_interceptor.py @@ -468,10 +468,13 @@ async def _log_request_async( # Since this is called in on_done, it is done. try: - # This might raise if cancelled? - exception = response.exception() - except Exception: - exception = None + if hasattr(response, "exception"): + exception = response.code() + else: + await response.code() + exception = None + except Exception as ex: + exception = ex if exception: # We need to adapt exception logging for async exception?