grpc_utils
drunc.utils.grpc_utils
Classes
GrpcErrorDetails(code, message, details)
dataclass
A structured representation of a gRPC error, including its status code, message, and any extracted rich error details. Used to extract and format detailed error information on the client side.
Attributes:
| Name | Type | Description |
|---|---|---|
code |
str
|
The gRPC status code name (e.g., "NOT_FOUND") |
message |
str
|
The error message from the gRPC status |
details |
List[str]
|
A list of formatted error detail strings |
Functions
__str__()
Return a human-readable string representation of the error.
RichErrorServerInterceptor
Bases: ServerInterceptor
A gRPC server interceptor that catches exceptions and converts them into rich error statuses with structured error details.
Functions
intercept_service(continuation, handler_call_details)
Intercept gRPC service calls to handle exceptions and convert them into rich error statuses.
Source code in drunc/utils/grpc_utils.py
Functions
abort_with_rich_error_status(context, grpc_error_code, message, error_obj)
Aborts the current gRPC call with a rich error status containing structured error details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
ServicerContext
|
The gRPC context used to abort the RPC |
required |
grpc_error_code
|
Code
|
A gRPC status code from |
required |
message
|
str
|
Quick description of the error |
required |
error_obj
|
Message
|
A protobuf message providing additional structured error details. It will be packed into a google.protobuf.Any |
required |
Raises: grpc.RpcError: Terminate the RPC with the constructed error status
Source code in drunc/utils/grpc_utils.py
copy_token(token)
Create a copy of the original token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
Token
|
The original token to copy. |
required |
Returns:
| Type | Description |
|---|---|
Token
|
A copy of the original token. |
Source code in drunc/utils/grpc_utils.py
dict_to_grpc_proto(data, proto_class_instance)
Converts a Python dictionary into an instance of a gRPC Protobuf message. 'proto_class_instance' should be an empty instance, e.g., Token()
Source code in drunc/utils/grpc_utils.py
extract_grpc_rich_error(grpc_error)
Extract rich error details from a gRPC error using Google's error model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grpc_error
|
RpcError
|
The gRPC error to parse |
required |
Returns:
| Type | Description |
|---|---|
GrpcErrorDetails
|
GrpcErrorDetails with structured error information |
Source code in drunc/utils/grpc_utils.py
format_error_details(detail)
Format protobuf message fields into human-readable strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
detail
|
Message
|
A protobuf message representing a gRPC error detail |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: A list of formatted strings describing the message's fields and values. Format: "field_name: value" for simple messages or "field_name: field1=value1, field2=value2" for nested messages |
Source code in drunc/utils/grpc_utils.py
handle_grpc_error(error)
Handle gRPC errors by rethrowing them with appropriate context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
RpcError
|
The gRPC error to handle. |
required |
Raises: A custom exception if the error matches a known category, or the original gRPC error if no classification applies.
Source code in drunc/utils/grpc_utils.py
interrupt_if_unreachable_server(grpc_error)
Interrupt if server is not reachable and return the error details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grpc_error
|
RpcError
|
The gRPC error |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
str | None: The internal error details if the server is unreachable and details are available; otherwise, returns None. |
Source code in drunc/utils/grpc_utils.py
rethrow_if_timeout(grpc_error)
Raise a ServerTimeout if timeout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grpc_error
|
RpcError
|
The gRPC error |
required |
Raises:
| Type | Description |
|---|---|
ServerTimeout
|
If the error code is DEADLINE_EXCEEDED |
Source code in drunc/utils/grpc_utils.py
rethrow_if_unreachable_server(grpc_error)
Raise a ServerUnreachable exception if the gRPC error indicates the server is unreachable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grpc_error
|
RpcError
|
The gRPC error |
required |
Raises:
| Type | Description |
|---|---|
ServerUnreachable
|
If the error indicates the server is unavailable |
Source code in drunc/utils/grpc_utils.py
server_is_reachable(grpc_error)
Check if server is reachable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grpc_error
|
RpcError
|
The gRPC error |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the server is reachable, False if the error indicates it is unavailable |
Source code in drunc/utils/grpc_utils.py
unpack_error_response(name, text, token)
Create a response for unpacking errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the command or service. |
required |
text
|
str
|
The error message to include in the response. |
required |
token
|
Token
|
The token associated with the request. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
response |
Response
|
the response object containing the error message. |