From 3b00ec73e47e60c5a05abeb28b57afb5e80f7902 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 27 Oct 2021 15:19:34 -0400 Subject: [PATCH 1/2] fix: handle bare 'grpc.Call' in 'from_grpc_error' Fixes: #297. --- google/api_core/exceptions.py | 7 ++++++- tests/unit/test_exceptions.py | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/google/api_core/exceptions.py b/google/api_core/exceptions.py index fdb21090..6b1b6f7e 100644 --- a/google/api_core/exceptions.py +++ b/google/api_core/exceptions.py @@ -487,9 +487,14 @@ def _is_informative_grpc_error(rpc_exc): def _parse_grpc_error_details(rpc_exc): - status = rpc_status.from_call(rpc_exc) + try: + status = rpc_status.from_call(rpc_exc) + except NotImplementedError: # workaround + return [] + if not status: return [] + possible_errors = [ error_details_pb2.BadRequest, error_details_pb2.PreconditionFailure, diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py index f6345fe1..7b9cf74c 100644 --- a/tests/unit/test_exceptions.py +++ b/tests/unit/test_exceptions.py @@ -237,6 +237,33 @@ def test_from_grpc_error_non_call(): assert exception.response == error +@pytest.mark.skipif(grpc is None, reason="No grpc") +def test_from_grpc_error_bare_call(): + message = "Testing" + + class TestingError(grpc.Call, grpc.RpcError): + def __init__(self, exception): + self.exception = exception + + def code(self): + return self.exception.grpc_status_code + + def details(self): + return message + + nested_message = "message" + error = TestingError(exceptions.GoogleAPICallError(nested_message)) + + exception = exceptions.from_grpc_error(error) + + assert isinstance(exception, exceptions.GoogleAPICallError) + assert exception.code is None + assert exception.grpc_status_code is None + assert exception.message == message + assert exception.errors == [error] + assert exception.response == error + + def create_bad_request_details(): bad_request_details = error_details_pb2.BadRequest() field_violation = bad_request_details.field_violations.add() From 0885647cb29ce13e34d912e4576ffdf4155670d2 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 2 Nov 2021 16:03:16 -0400 Subject: [PATCH 2/2] tests: add assertion for 'exception.details' --- tests/unit/test_exceptions.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/test_exceptions.py b/tests/unit/test_exceptions.py index 7b9cf74c..622f58ab 100644 --- a/tests/unit/test_exceptions.py +++ b/tests/unit/test_exceptions.py @@ -262,6 +262,7 @@ def details(self): assert exception.message == message assert exception.errors == [error] assert exception.response == error + assert exception.details == [] def create_bad_request_details():