Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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'
  • Loading branch information
tseaver committed Nov 3, 2021
1 parent 214d88b commit 060b339
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
7 changes: 6 additions & 1 deletion google/api_core/exceptions.py
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_exceptions.py
Expand Up @@ -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()
Expand Down

0 comments on commit 060b339

Please sign in to comment.