Skip to content

Commit 8b16c0a

Browse files
committed
feat(client): include connection info in Client::send_request errors
1 parent 1ec1db2 commit 8b16c0a

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

src/client/client.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ where
242242
if req.version() == Version::HTTP_2 {
243243
warn!("Connection is HTTP/1, but request requires HTTP/2");
244244
return Err(ClientError::Normal(
245-
crate::Error::new_user_unsupported_version(),
245+
crate::Error::new_user_unsupported_version().with_client_connect_info(pooled.conn_info.clone()),
246246
));
247247
}
248248

@@ -272,10 +272,15 @@ where
272272
authority_form(req.uri_mut());
273273
}
274274

275-
let mut res = pooled
276-
.send_request_retryable(req)
277-
.await
278-
.map_err(ClientError::map_with_reused(pooled.is_reused()))?;
275+
let mut res = match pooled.send_request_retryable(req).await {
276+
Err((err, orig_req)) => {
277+
return Err(ClientError::map_with_reused(pooled.is_reused())((
278+
err.with_client_connect_info(pooled.conn_info.clone()),
279+
orig_req,
280+
)));
281+
}
282+
Ok(res) => res,
283+
};
279284

280285
// If the Connector included 'extra' info, add to Response...
281286
if let Some(extra) = &pooled.conn_info.extra {

src/client/connect/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl Connected {
191191

192192
// Don't public expose that `Connected` is `Clone`, unsure if we want to
193193
// keep that contract...
194-
#[cfg(feature = "http2")]
194+
#[cfg(any(all(feature = "client", feature = "http1"), feature = "http2"))]
195195
pub(super) fn clone(&self) -> Connected {
196196
Connected {
197197
alpn: self.alpn.clone(),

src/error.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
//! Error and Result module.
2+
3+
#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
4+
use crate::client::connect::Connected;
25
use std::error::Error as StdError;
36
use std::fmt;
47

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

2025
#[derive(Debug)]
@@ -206,9 +211,20 @@ impl Error {
206211
self.inner.cause
207212
}
208213

214+
/// Returns the info of the client connection on which this error occurred.
215+
#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
216+
pub fn client_connect_info(&self) -> Option<&Connected> {
217+
self.inner.connect_info.as_ref()
218+
}
219+
209220
pub(super) fn new(kind: Kind) -> Error {
210221
Error {
211-
inner: Box::new(ErrorImpl { kind, cause: None }),
222+
inner: Box::new(ErrorImpl {
223+
kind,
224+
cause: None,
225+
#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
226+
connect_info: None,
227+
}),
212228
}
213229
}
214230

@@ -217,6 +233,12 @@ impl Error {
217233
self
218234
}
219235

236+
#[cfg(all(feature = "client", any(feature = "http1", feature = "http2")))]
237+
pub(super) fn with_client_connect_info(mut self, connect_info: Connected) -> Error {
238+
self.inner.connect_info = Some(connect_info);
239+
self
240+
}
241+
220242
#[cfg(any(all(feature = "http1", feature = "server"), feature = "ffi"))]
221243
pub(super) fn kind(&self) -> &Kind {
222244
&self.inner.kind

0 commit comments

Comments
 (0)