-
Notifications
You must be signed in to change notification settings - Fork 997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add h2::Error as a source
for tonic::Status when converting from h2::Error
#612
Changes from 11 commits
331b455
ab31a94
14ca795
0a0a0e9
051650d
a9d0a49
5b1fc1c
a5739a9
8edb9ac
92aaafe
cdbc5d7
ac8aad5
c10da2c
368fadd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ use crate::metadata::MetadataMap; | |
use bytes::Bytes; | ||
use http::header::{HeaderMap, HeaderValue}; | ||
use percent_encoding::{percent_decode, percent_encode, AsciiSet, CONTROLS}; | ||
use std::convert::TryFrom; | ||
use std::{borrow::Cow, error::Error, fmt}; | ||
use tracing::{debug, trace, warn}; | ||
|
||
|
@@ -33,7 +34,6 @@ const GRPC_STATUS_DETAILS_HEADER: &str = "grpc-status-details-bin"; | |
/// assert_eq!(status1.code(), Code::InvalidArgument); | ||
/// assert_eq!(status1.code(), status2.code()); | ||
/// ``` | ||
#[derive(Clone)] | ||
pub struct Status { | ||
/// The gRPC status code, found in the `grpc-status` header. | ||
code: Code, | ||
|
@@ -45,6 +45,8 @@ pub struct Status { | |
/// If the metadata contains any headers with names reserved either by the gRPC spec | ||
/// or by `Status` fields above, they will be ignored. | ||
metadata: MetadataMap, | ||
/// Optional underlying error. | ||
source: Option<Box<dyn Error + Send + Sync + 'static>>, | ||
} | ||
|
||
/// gRPC status codes used by [`Status`]. | ||
|
@@ -162,6 +164,7 @@ impl Status { | |
message: message.into(), | ||
details: Bytes::new(), | ||
metadata: MetadataMap::new(), | ||
source: None, | ||
} | ||
} | ||
|
||
|
@@ -302,38 +305,34 @@ impl Status { | |
} | ||
|
||
#[cfg_attr(not(feature = "transport"), allow(dead_code))] | ||
pub(crate) fn from_error(err: &(dyn Error + 'static)) -> Status { | ||
Status::try_from_error(err).unwrap_or_else(|| Status::new(Code::Unknown, err.to_string())) | ||
pub(crate) fn from_error(err: Box<dyn Error + Send + Sync + 'static>) -> Status { | ||
Status::try_from_error(err) | ||
.unwrap_or_else(|err| Status::new(Code::Unknown, err.to_string())) | ||
} | ||
|
||
pub(crate) fn try_from_error(err: &(dyn Error + 'static)) -> Option<Status> { | ||
let mut cause = Some(err); | ||
|
||
while let Some(err) = cause { | ||
if let Some(status) = err.downcast_ref::<Status>() { | ||
return Some(Status { | ||
code: status.code, | ||
message: status.message.clone(), | ||
details: status.details.clone(), | ||
metadata: status.metadata.clone(), | ||
}); | ||
pub(crate) fn try_from_error( | ||
err: Box<dyn Error + Send + Sync + 'static>, | ||
) -> Result<Status, Box<dyn Error + Send + Sync + 'static>> { | ||
let err = match err.downcast::<Status>() { | ||
Ok(status) => { | ||
return Ok(*status); | ||
} | ||
Err(err) => err, | ||
}; | ||
|
||
#[cfg(feature = "transport")] | ||
{ | ||
if let Some(h2) = err.downcast_ref::<h2::Error>() { | ||
return Some(Status::from_h2_error(h2)); | ||
} | ||
|
||
if let Some(timeout) = err.downcast_ref::<crate::transport::TimeoutExpired>() { | ||
return Some(Status::cancelled(timeout.to_string())); | ||
} | ||
#[cfg(feature = "transport")] | ||
let err = match err.downcast::<h2::Error>() { | ||
Ok(h2) => { | ||
return Ok(Status::from_h2_error(&*h2)); | ||
} | ||
Err(err) => err, | ||
}; | ||
|
||
cause = err.source(); | ||
if let Some(status) = find_status_in_source_chain(&*err) { | ||
return Ok(status); | ||
} | ||
|
||
None | ||
Err(err) | ||
} | ||
|
||
// FIXME: bubble this into `transport` and expose generic http2 reasons. | ||
|
@@ -356,7 +355,13 @@ impl Status { | |
_ => Code::Unknown, | ||
}; | ||
|
||
Status::new(code, format!("h2 protocol error: {}", err)) | ||
let mut status = Self::new(code, format!("h2 protocol error: {}", err)); | ||
let error: Option<Box<dyn Error + Send + Sync + 'static>> = err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you need this extra type hint? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't recall. I removed it and it still compiles so pushed the removal of it. |
||
.reason() | ||
.map(h2::Error::from) | ||
.map(|err| Box::new(err) as Box<dyn Error + Send + Sync + 'static>); | ||
davidpdrsn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
status.source = error; | ||
status | ||
} | ||
|
||
#[cfg(feature = "transport")] | ||
|
@@ -374,7 +379,8 @@ impl Status { | |
where | ||
E: Into<Box<dyn Error + Send + Sync>>, | ||
{ | ||
Status::from_error(&*err.into()) | ||
let err: Box<dyn Error + Send + Sync> = err.into(); | ||
Status::from_error(err) | ||
} | ||
|
||
/// Extract a `Status` from a hyper `HeaderMap`. | ||
|
@@ -410,6 +416,7 @@ impl Status { | |
message, | ||
details, | ||
metadata: MetadataMap::from_headers(other_headers), | ||
source: None, | ||
}, | ||
Err(err) => { | ||
warn!("Error deserializing status message header: {}", err); | ||
|
@@ -418,6 +425,7 @@ impl Status { | |
message: format!("Error deserializing status message header: {}", err), | ||
details, | ||
metadata: MetadataMap::from_headers(other_headers), | ||
source: None, | ||
} | ||
} | ||
} | ||
|
@@ -505,6 +513,7 @@ impl Status { | |
message: message.into(), | ||
details, | ||
metadata, | ||
source: None, | ||
} | ||
} | ||
|
||
|
@@ -524,6 +533,32 @@ impl Status { | |
} | ||
} | ||
|
||
fn find_status_in_source_chain(err: &(dyn Error + 'static)) -> Option<Status> { | ||
let mut source = Some(err); | ||
|
||
while let Some(err) = source { | ||
if let Some(status) = err.downcast_ref::<Status>() { | ||
return Some(Status { | ||
code: status.code, | ||
message: status.message.clone(), | ||
details: status.details.clone(), | ||
metadata: status.metadata.clone(), | ||
// Since `Status` is not `Clone`, any `source` on the original Status | ||
// cannot be cloned so must remain with the original `Status`. | ||
source: None, | ||
}); | ||
} | ||
|
||
#[cfg(feature = "transport")] | ||
if let Some(timeout) = err.downcast_ref::<crate::transport::TimeoutExpired>() { | ||
return Some(Status::cancelled(timeout.to_string())); | ||
} | ||
|
||
source = err.source(); | ||
} | ||
|
||
None | ||
} | ||
impl fmt::Debug for Status { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
// A manual impl to reduce the noise of frequently empty fields. | ||
|
@@ -543,6 +578,8 @@ impl fmt::Debug for Status { | |
builder.field("metadata", &self.metadata); | ||
} | ||
|
||
builder.field("source", &self.source); | ||
|
||
builder.finish() | ||
} | ||
} | ||
|
@@ -555,6 +592,14 @@ fn invalid_header_value_byte<Error: fmt::Display>(err: Error) -> Status { | |
) | ||
} | ||
|
||
impl TryFrom<Box<dyn Error + Send + Sync + 'static>> for Status { | ||
type Error = Box<dyn Error + Send + Sync + 'static>; | ||
|
||
fn try_from(err: Box<dyn Error + Send + Sync + 'static>) -> Result<Self, Self::Error> { | ||
Status::try_from_error(err) | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a specific use-case this impl enables, or is mainly for convenience? I dunno, feels a bit out of place for me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't recall, and I can't find a relevant use of this in the main code bases in which I work. Shall I remove it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I think we should just remove it. |
||
#[cfg(feature = "transport")] | ||
impl From<h2::Error> for Status { | ||
fn from(err: h2::Error) -> Self { | ||
|
@@ -609,7 +654,11 @@ impl fmt::Display for Status { | |
} | ||
} | ||
|
||
impl Error for Status {} | ||
impl Error for Status { | ||
fn source(&self) -> Option<&(dyn Error + 'static)> { | ||
self.source.as_ref().map(|err| (&**err) as _) | ||
} | ||
} | ||
|
||
/// | ||
/// Take the `Status` value from `trailers` if it is available, else from `status_code`. | ||
|
@@ -775,25 +824,25 @@ mod tests { | |
#[test] | ||
fn from_error_status() { | ||
let orig = Status::new(Code::OutOfRange, "weeaboo"); | ||
let found = Status::from_error(&orig); | ||
let found = Status::from_error(Box::new(orig)); | ||
|
||
assert_eq!(orig.code(), found.code()); | ||
assert_eq!(orig.message(), found.message()); | ||
assert_eq!(found.code(), Code::OutOfRange); | ||
assert_eq!(found.message(), "weeaboo"); | ||
} | ||
|
||
#[test] | ||
fn from_error_unknown() { | ||
let orig: Error = "peek-a-boo".into(); | ||
let found = Status::from_error(&*orig); | ||
let found = Status::from_error(orig); | ||
|
||
assert_eq!(found.code(), Code::Unknown); | ||
assert_eq!(found.message(), orig.to_string()); | ||
assert_eq!(found.message(), "peek-a-boo".to_string()); | ||
} | ||
|
||
#[test] | ||
fn from_error_nested() { | ||
let orig = Nested(Box::new(Status::new(Code::OutOfRange, "weeaboo"))); | ||
let found = Status::from_error(&orig); | ||
let found = Status::from_error(Box::new(orig)); | ||
|
||
assert_eq!(found.code(), Code::OutOfRange); | ||
assert_eq!(found.message(), "weeaboo"); | ||
|
@@ -802,10 +851,18 @@ mod tests { | |
#[test] | ||
#[cfg(feature = "transport")] | ||
fn from_error_h2() { | ||
use std::error::Error as _; | ||
|
||
let orig = h2::Error::from(h2::Reason::CANCEL); | ||
let found = Status::from_error(&orig); | ||
let found = Status::from_error(Box::new(orig)); | ||
|
||
assert_eq!(found.code(), Code::Cancelled); | ||
|
||
let source = found | ||
.source() | ||
.and_then(|err| err.downcast_ref::<h2::Error>()) | ||
.unwrap(); | ||
assert_eq!(source.reason(), Some(h2::Reason::CANCEL)); | ||
} | ||
|
||
#[test] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this is failing CI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like
deny(warnings)
is only set in CI? When I runcargo clippy
, it only warned for this and some other issues. It would probably be useful to make the crates enable#![deny(warnings)]
(but in a separate PR obviously).