From 7c0e9c853c70a55e67bb0a4742b2c7046d37e579 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 13 Feb 2023 15:29:19 +0000 Subject: [PATCH] Make some functionality of Status public (#1256) * Make some functionality of Status public Signed-off-by: Nick Cameron * Update tonic/src/status.rs --------- Signed-off-by: Nick Cameron Co-authored-by: Lucio Franco --- tonic/src/status.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tonic/src/status.rs b/tonic/src/status.rs index fb73863..1b83c62 100644 --- a/tonic/src/status.rs +++ b/tonic/src/status.rs @@ -325,7 +325,14 @@ impl Status { }) } - pub(crate) fn try_from_error( + /// Create a `Status` from various types of `Error`. + /// + /// Returns the error if a status could not be created. + /// + /// # Downcast stability + /// This function does not provide any stability guarantees around how it will downcast errors into + /// status codes. + pub fn try_from_error( err: Box, ) -> Result> { let err = match err.downcast::() { @@ -501,7 +508,8 @@ impl Status { Ok(header_map) } - pub(crate) fn add_header(&self, header_map: &mut HeaderMap) -> Result<(), Self> { + /// Add headers from this `Status` into `header_map`. + pub fn add_header(&self, header_map: &mut HeaderMap) -> Result<(), Self> { header_map.extend(self.metadata.clone().into_sanitized_headers()); header_map.insert(GRPC_STATUS_HEADER_CODE, self.code.to_header_value()); @@ -555,6 +563,12 @@ impl Status { } } + /// Add a source error to this status. + pub fn set_source(&mut self, source: Arc) -> &mut Status { + self.source = Some(source); + self + } + #[allow(clippy::wrong_self_convention)] /// Build an `http::Response` from the given `Status`. pub fn to_http(self) -> http::Response {