Skip to content

Commit

Permalink
Map tonic::transport::Error in tonic::Status::Unavailable
Browse files Browse the repository at this point in the history
This came up when testing retry logic when using the
tonic::transport::Error.  Specifically, we would run into Unknown errors
during the following sequence:

* unplug ethernet
* create client with connect_lazy
* unary call -> Unknown error, representing a tonic::transport::Error

Fixes #628
  • Loading branch information
David Freese committed May 4, 2021
1 parent 4b26e78 commit 22ba74a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tonic/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ impl Status {
return Some(Status::from_h2_error(h2));
}

if let Some(transport) = err.downcast_ref::<crate::transport::Error>() {
return Some(Status::unavailable(transport.to_string()));
}

if let Some(timeout) = err.downcast_ref::<crate::transport::TimeoutExpired>() {
return Some(Status::cancelled(timeout.to_string()));
}
Expand Down Expand Up @@ -819,6 +823,25 @@ mod tests {
assert_eq!(err.reason(), Some(h2::Reason::CANCEL));
}

#[test]
#[cfg(feature = "transport")]
fn from_error_transport() {
#[derive(Debug)]
struct TestErr {}
impl fmt::Display for TestErr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "hello, it's me")
}
}
impl std::error::Error for TestErr {}

let orig = crate::transport::Error::from_source(TestErr {});
let found = Status::from_error(&orig);

assert_eq!(found.code(), Code::Unavailable);
assert_eq!(found.message(), "hello, it's me");
}

#[test]
fn code_from_i32() {
// This for loop should catch if we ever add a new variant and don't
Expand Down

0 comments on commit 22ba74a

Please sign in to comment.