diff options
author | Tres Seaver <tseaver@palladion.com> | 2021-11-03 14:18:12 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-03 12:18:12 -0600 |
commit | 060b339e3af296dd1772bfc1b4a0d2b4264cae1f (patch) | |
tree | 9d92f63acfa66b0810c455709cc4777fc30e0084 | |
parent | 214d88b54142628f7ecf5d49d3515f353db8aaf5 (diff) | |
download | python-api-core-060b339e3af296dd1772bfc1b4a0d2b4264cae1f.tar.gz |
fix: handle bare 'grpc.Call' in 'from_grpc_error' (#298)
* fix: handle bare 'grpc.Call' in 'from_grpc_error'
Fixes: #297.
* tests: add assertion for 'exception.details'
-rw-r--r-- | google/api_core/exceptions.py | 7 | ||||
-rw-r--r-- | tests/unit/test_exceptions.py | 28 |
2 files changed, 34 insertions, 1 deletions
diff --git a/google/api_core/exceptions.py b/google/api_core/exceptions.py index fdb2109..6b1b6f7 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 f6345fe..622f58a 100644 --- a/tests/unit/test_exceptions.py +++ b/tests/unit/test_exceptions.py @@ -237,6 +237,34 @@ 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 + assert exception.details == [] + + def create_bad_request_details(): bad_request_details = error_details_pb2.BadRequest() field_violation = bad_request_details.field_violations.add() |