grpc_utils
drunc.utils.grpc_utils
gRPC utilities for DRUNC.
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 | Message]
|
A list of formatted error detail strings or protobuf Messages. |
Methods:
__str__()
Return a human-readable string representation of the error.
Source code in drunc/utils/grpc_utils.py
RichErrorServerInterceptor
Bases: ServerInterceptor
A gRPC server interceptor that catches exceptions and converts them into rich error statuses with structured error details.
Methods:
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
ServerTimeout(message)
Bases: DruncException
Exception raised when the gRPC server times out.
Initialize the ServerTimeout exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The error message. |
required |
Source code in drunc/utils/grpc_utils.py
Methods:
ServerUnreachable(message)
Bases: DruncException
Exception raised when the gRPC server is unreachable.
Initialize the ServerUnreachable exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
The error message. |
required |
Source code in drunc/utils/grpc_utils.py
Methods:
UnpackingError(data, format)
Bases: DruncCommandException
Exception raised when unpacking gRPC messages fails.
Initialize the UnpackingError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
object
|
The data that failed to unpack. |
required |
format
|
type[Message]
|
The expected format. |
required |
Source code in drunc/utils/grpc_utils.py
Methods:
Functions:
abort_with_rich_details(context, grpc_error_code, message, error_objs)
Pack a list of detail objects into a single gRPC Status.
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_objs
|
list
|
A list of protobuf messages 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()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, object]
|
The dictionary to convert. |
required |
proto_class_instance
|
Message
|
An empty instance of the target protobuf message type. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Message |
Message
|
The converted protobuf message. |
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 |
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 |
|---|---|
str | None
|
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
pack_to_any(data)
Pack a protobuf message into an Any message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Message
|
The protobuf message to pack. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
any_pb2.Any: The packed message. |
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_any(data, format)
Unpack an Any message into a specific protobuf format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The Any message to unpack. |
required |
format
|
type[T]
|
The protobuf message type to unpack into. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Message |
T
|
The unpacked message. |
Raises:
| Type | Description |
|---|---|
UnpackingError
|
If the message cannot be unpacked into the specified format. |
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. |