Skip to content

Commit fb2e84a

Browse files
Improve Context Abort Message
1 parent 0426de6 commit fb2e84a

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

Diff for: django_grpc_testtools/context.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
from datetime import datetime
12
from typing import NoReturn
23
from grpc import RpcError, StatusCode
34
from collections.abc import Sequence, Mapping
45
from grpc import ServicerContext
56

67
MetadataType = Sequence[tuple[str, str]]
8+
_NON_OK_RENDEZVOUS_REPR_FORMAT = (
9+
"<{} of RPC that terminated with:\n"
10+
"\tstatus = {}\n"
11+
'\tdetails = "{}"\n'
12+
'\tdebug_error_string = "{}"\n'
13+
">"
14+
)
715

816

917
class FakeServicerContext(ServicerContext):
@@ -24,7 +32,19 @@ def abort(self, status: StatusCode, message: str) -> NoReturn:
2432
"""
2533
self.abort_status = status
2634
self.abort_message = message
27-
raise RpcError(message) # Just like original context
35+
debug_error_string = (
36+
"UNKNOWN:Error received from peer "
37+
"{grpc_message:\"" + message + "\", "
38+
"grpc_status:" + str(status.value[0]) + ", "
39+
"created_time:\"" + str(datetime.now()) + "\"}"
40+
)
41+
stderr = _NON_OK_RENDEZVOUS_REPR_FORMAT.format(
42+
RpcError.__name__,
43+
status,
44+
message,
45+
debug_error_string,
46+
)
47+
raise RpcError(stderr)
2848

2949
def set_trailing_metadata(self, items: MetadataType) -> None:
3050
"""

0 commit comments

Comments
 (0)