Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle bare 'grpc.Call' in 'from_grpc_error' #298

Merged
merged 6 commits into from Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
atulep marked this conversation as resolved.
Show resolved Hide resolved
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)
atulep marked this conversation as resolved.
Show resolved Hide resolved
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