14 """Reference implementation for status mapping in gRPC Python."""
19 from google.rpc
import status_pb2
22 from ._common
import GRPC_DETAILS_METADATA_KEY
23 from ._common
import code_to_grpc_status_code
27 collections.namedtuple(
'_Status',
28 (
'code',
'details',
'trailing_metadata')),
34 """Returns a google.rpc.status.Status message corresponding to a given grpc.Call.
36 This is an EXPERIMENTAL API.
39 call: A grpc.Call instance.
42 A google.rpc.status.Status message representing the status of the RPC.
45 ValueError: If the gRPC call's code or details are inconsistent with the
46 status code and message inside of the google.rpc.status.Status.
48 if call.trailing_metadata()
is None:
50 for key, value
in call.trailing_metadata():
51 if key == GRPC_DETAILS_METADATA_KEY:
52 rich_status = status_pb2.Status.FromString(value)
53 if call.code().value[0] != rich_status.code:
55 'Code in Status proto (%s) doesn\'t match status code (%s)'
57 if call.details() != rich_status.message:
59 'Message in Status proto (%s) doesn\'t match status details (%s)'
60 % (rich_status.message, call.details()))
66 """Convert a google.rpc.status.Status message to grpc.Status.
68 This is an EXPERIMENTAL API.
71 status: a google.rpc.status.Status message representing the non-OK status
72 to terminate the RPC with and communicate it to the client.
75 A grpc.Status instance representing the input google.rpc.status.Status message.
78 details=status.message,
79 trailing_metadata=((GRPC_DETAILS_METADATA_KEY,
80 status.SerializeToString()),))
88 if sys.version_info[0] >= 3
and sys.version_info[1] >= 6:
89 from .
import _async
as aio