Skip to content
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

Restore connect info in errors #135

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/client/legacy/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ struct Config {
pub struct Error {
kind: ErrorKind,
source: Option<Box<dyn StdError + Send + Sync>>,
#[cfg(any(feature = "http1", feature = "http2"))]
connect_info: Option<Connected>,
}

#[derive(Debug)]
Expand All @@ -74,12 +76,14 @@ macro_rules! e {
Error {
kind: ErrorKind::$kind,
source: None,
connect_info: None,
}
};
($kind:ident, $src:expr) => {
Error {
kind: ErrorKind::$kind,
source: Some($src.into()),
connect_info: None,
}
};
}
Expand Down Expand Up @@ -277,7 +281,9 @@ where
if pooled.is_http1() {
if req.version() == Version::HTTP_2 {
warn!("Connection is HTTP/1, but request requires HTTP/2");
return Err(TrySendError::Nope(e!(UserUnsupportedVersion)));
return Err(TrySendError::Nope(
e!(UserUnsupportedVersion).with_connect_info(pooled.conn_info.clone()),
));
}

if self.config.set_host {
Expand Down Expand Up @@ -311,11 +317,15 @@ where
Err(mut err) => {
return if let Some(req) = err.take_message() {
Err(TrySendError::Retryable {
error: e!(Canceled, err.into_error()),
error: e!(Canceled, err.into_error())
.with_connect_info(pooled.conn_info.clone()),
req,
})
} else {
Err(TrySendError::Nope(e!(SendRequest, err.into_error())))
Err(TrySendError::Nope(
e!(SendRequest, err.into_error())
.with_connect_info(pooled.conn_info.clone()),
))
}
}
};
Expand Down Expand Up @@ -789,6 +799,7 @@ impl<B: Body + 'static> PoolClient<B> {
PoolTx::Http2(ref mut tx) => tx.try_send_request(req),
};
}

/*
//TODO: can we re-introduce this somehow? Or must people use tower::retry?
fn send_request_retryable(
Expand Down Expand Up @@ -1602,6 +1613,19 @@ impl Error {
matches!(self.kind, ErrorKind::Connect)
}

/// Returns the info of the client connection on which this error occurred.
#[cfg(any(feature = "http1", feature = "http2"))]
pub fn connect_info(&self) -> Option<&Connected> {
self.connect_info.as_ref()
}

#[cfg(any(feature = "http1", feature = "http2"))]
fn with_connect_info(self, connect_info: Connected) -> Self {
Self {
connect_info: Some(connect_info),
..self
}
}
fn is_canceled(&self) -> bool {
matches!(self.kind, ErrorKind::Canceled)
}
Expand Down