Skip to content

Commit

Permalink
feat(client): include connection info in Client::send_request errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nox committed Feb 4, 2022
1 parent 1ec1db2 commit 8b16c0a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ where
if req.version() == Version::HTTP_2 {
warn!("Connection is HTTP/1, but request requires HTTP/2");
return Err(ClientError::Normal(
crate::Error::new_user_unsupported_version(),
crate::Error::new_user_unsupported_version().with_client_connect_info(pooled.conn_info.clone()),
));
}

Expand Down Expand Up @@ -272,10 +272,15 @@ where
authority_form(req.uri_mut());
}

let mut res = pooled
.send_request_retryable(req)
.await
.map_err(ClientError::map_with_reused(pooled.is_reused()))?;
let mut res = match pooled.send_request_retryable(req).await {
Err((err, orig_req)) => {
return Err(ClientError::map_with_reused(pooled.is_reused())((
err.with_client_connect_info(pooled.conn_info.clone()),
orig_req,
)));
}
Ok(res) => res,
};

// If the Connector included 'extra' info, add to Response...
if let Some(extra) = &pooled.conn_info.extra {
Expand Down
2 changes: 1 addition & 1 deletion src/client/connect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl Connected {

// Don't public expose that `Connected` is `Clone`, unsure if we want to
// keep that contract...
#[cfg(feature = "http2")]
#[cfg(any(all(feature = "client", feature = "http1"), feature = "http2"))]
pub(super) fn clone(&self) -> Connected {
Connected {
alpn: self.alpn.clone(),
Expand Down
24 changes: 23 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//! Error and Result module.

#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
use crate::client::connect::Connected;
use std::error::Error as StdError;
use std::fmt;

Expand All @@ -15,6 +18,8 @@ pub struct Error {
struct ErrorImpl {
kind: Kind,
cause: Option<Cause>,
#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
connect_info: Option<Connected>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -206,9 +211,20 @@ impl Error {
self.inner.cause
}

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

pub(super) fn new(kind: Kind) -> Error {
Error {
inner: Box::new(ErrorImpl { kind, cause: None }),
inner: Box::new(ErrorImpl {
kind,
cause: None,
#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
connect_info: None,
}),
}
}

Expand All @@ -217,6 +233,12 @@ impl Error {
self
}

#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
pub(super) fn with_client_connect_info(mut self, connect_info: Connected) -> Error {
self.inner.connect_info = Some(connect_info);
self
}

#[cfg(any(all(feature = "http1", feature = "server"), feature = "ffi"))]
pub(super) fn kind(&self) -> &Kind {
&self.inner.kind
Expand Down

0 comments on commit 8b16c0a

Please sign in to comment.