fix: include underlying error message in connection error (#2326)

* fix: include underlying error message in connection error

* fix: clippy

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
pull/24376/head
Andrew Lamb 2021-08-18 07:33:30 -04:00 committed by GitHub
parent a0e06014b7
commit e62790cf4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 10 deletions

View File

@ -15,10 +15,6 @@ pub enum Error {
#[error("Received invalid response: {}", .0)]
InvalidResponse(i32),
/// Error connecting to the server
#[error("Connection error: {}", .0)]
ConnectionError(#[from] tonic::transport::Error),
/// Client received an unexpected error from the server
#[error("Unexpected server error: {}: {}", .0.code(), .0.message())]
UnexpectedError(#[from] tonic::Status),

View File

@ -20,14 +20,33 @@ pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
#[derive(Debug, Error)]
pub enum Error {
/// Server returned an invalid argument error
#[error("Connection error: {}", .0)]
TransportError(#[from] tonic::transport::Error),
#[error("Connection error: {}{}", source, details)]
TransportError {
/// underlying [`tonic::transport::Error`]
source: tonic::transport::Error,
/// stringified version of the tonic error's source
details: String,
},
/// Client received an unexpected error from the server
#[error("Invalid URI: {}", .0)]
InvalidUri(#[from] InvalidUri),
}
// Custom impl to include underlying source (not included in tonic
// transport error)
impl From<tonic::transport::Error> for Error {
fn from(source: tonic::transport::Error) -> Self {
use std::error::Error;
let details = source
.source()
.map(|e| format!(" ({})", e))
.unwrap_or_else(|| "".to_string());
Self::TransportError { source, details }
}
}
/// Result type for the ConnectionBuilder
pub type Result<T, E = Error> = std::result::Result<T, E>;

View File

@ -23,8 +23,11 @@ mod write_pb;
#[derive(Debug, Snafu)]
pub enum Error {
#[snafu(display("gRPC transport error: {}", source))]
TransportError { source: tonic::transport::Error },
#[snafu(display("gRPC transport error: {}{}", source, details))]
TransportError {
source: tonic::transport::Error,
details: String,
},
#[snafu(display("gRPC reflection error: {}", source))]
ReflectionError {
@ -34,6 +37,20 @@ pub enum Error {
pub type Result<T, E = Error> = std::result::Result<T, E>;
// Custom impl to include underlying source (not included in tonic
// transport error)
impl From<tonic::transport::Error> for Error {
fn from(source: tonic::transport::Error) -> Self {
use std::error::Error;
let details = source
.source()
.map(|e| format!(" ({})", e))
.unwrap_or_else(|| "".to_string());
Self::TransportError { source, details }
}
}
/// Returns the name of the gRPC service S.
fn service_name<S: NamedService>(_: &S) -> &'static str {
S::NAME
@ -139,6 +156,7 @@ where
builder
.serve_with_incoming_shutdown(stream, shutdown.cancelled())
.await
.context(TransportError)
.await?;
Ok(())
}