diff --git a/tower-grpc/src/status.rs b/tower-grpc/src/status.rs index d3d2f7a8..a1178a94 100644 --- a/tower-grpc/src/status.rs +++ b/tower-grpc/src/status.rs @@ -4,6 +4,7 @@ use http::header::HeaderValue; use http::{self, HeaderMap}; use log::{debug, trace, warn}; use percent_encoding::{percent_decode, percent_encode, EncodeSet, DEFAULT_ENCODE_SET}; +use prost::Message; use std::{error::Error, fmt}; const GRPC_STATUS_HEADER_CODE: &str = "grpc-status"; @@ -59,6 +60,24 @@ impl Status { } } + /// Create a new `Status` with the associated code, message, and binary details field. + pub fn with_raw_details(code: Code, message: impl Into, details: Bytes) -> Status { + Status { + code, + message: message.into(), + details: details, + } + } + + /// Create a new `Status` with the associated code, message, and protobuf details field. + pub fn with_details(code: Code, message: impl Into, details: impl Message) -> Status { + let mut bytes = vec![]; + details + .encode(&mut bytes) + .expect("Message only errors if not enough space"); + Status::with_raw_details(code, message, bytes.into()) + } + // Deprecated: this constructor encourages creating statuses with no // message, hurting later debugging. #[doc(hidden)]