diff options
author | Yusuke Tanaka <yusuktan@maguro.dev> | 2024-08-09 00:47:15 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-08 08:47:15 -0700 |
commit | e36b1a3aa88b31435b18a33448fc75eeb6dc8017 (patch) | |
tree | bc29141f8d59c74db46fee49426a9bc1a77c1d10 /ext/fetch | |
parent | 18b9b43c3631053e2c8b4c293b9e1f44dee7bfa8 (diff) |
fix(ext/fetch): include TCP src/dst socket info in error messages (#24939)
This commit makes `fetch` error messages include source and destination TCP
socket info i.e. port number and IP address for better debuggability.
Closes #24922
Diffstat (limited to 'ext/fetch')
-rw-r--r-- | ext/fetch/lib.rs | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/ext/fetch/lib.rs b/ext/fetch/lib.rs index 798acad0b..fa85824f4 100644 --- a/ext/fetch/lib.rs +++ b/ext/fetch/lib.rs @@ -62,11 +62,13 @@ use http::header::HOST; use http::header::PROXY_AUTHORIZATION; use http::header::RANGE; use http::header::USER_AGENT; +use http::Extensions; use http::Method; use http::Uri; use http_body_util::BodyExt; use hyper::body::Frame; use hyper_util::client::legacy::connect::HttpConnector; +use hyper_util::client::legacy::connect::HttpInfo; use hyper_util::rt::TokioExecutor; use hyper_util::rt::TokioIo; use hyper_util::rt::TokioTimer; @@ -1104,17 +1106,39 @@ impl ClientSendError { pub fn is_connect_error(&self) -> bool { self.source.is_connect() } + + fn http_info(&self) -> Option<HttpInfo> { + let mut exts = Extensions::new(); + self.source.connect_info()?.get_extras(&mut exts); + exts.remove::<HttpInfo>() + } } impl std::fmt::Display for ClientSendError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!( - f, - "error sending request for url ({uri}): {source}", - uri = self.uri, - // NOTE: we can use `std::error::Report` instead once it's stabilized. - source = error_reporter::Report::new(&self.source), - ) + // NOTE: we can use `std::error::Report` instead once it's stabilized. + let detail = error_reporter::Report::new(&self.source); + + match self.http_info() { + Some(http_info) => { + write!( + f, + "error sending request from {src} for {uri} ({dst}): {detail}", + src = http_info.local_addr(), + uri = self.uri, + dst = http_info.remote_addr(), + detail = detail, + ) + } + None => { + write!( + f, + "error sending request for url ({uri}): {detail}", + uri = self.uri, + detail = detail, + ) + } + } } } |