Skip to content

Commit 48ba11b

Browse files
authored
Merge pull request #726 from itowlson/diagnose-http-request-errors
Log outbound HTTP request error detail
2 parents 65c78d7 + e989790 commit 48ba11b

File tree

1 file changed

+29
-3
lines changed
  • crates/outbound-http/src

1 file changed

+29
-3
lines changed

crates/outbound-http/src/lib.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ impl wasi_outbound_http::WasiOutboundHttp for OutboundHttp {
6767
.headers(headers)
6868
.body(body)
6969
.send(),
70-
)?;
71-
70+
);
71+
let res = log_request_error(res)?;
7272
Response::try_from(res)
7373
}))
7474
.map_err(|_| HttpError::RuntimeError)?,
@@ -77,13 +77,39 @@ impl wasi_outbound_http::WasiOutboundHttp for OutboundHttp {
7777
.request(method, url)
7878
.headers(headers)
7979
.body(body)
80-
.send()?;
80+
.send();
81+
let res = log_request_error(res)?;
8182
Ok(Response::try_from(res)?)
8283
}
8384
}
8485
}
8586
}
8687

88+
fn log_request_error<R>(response: Result<R, reqwest::Error>) -> Result<R, reqwest::Error> {
89+
if let Err(e) = &response {
90+
let error_desc = if e.is_timeout() {
91+
"timeout error"
92+
} else if e.is_connect() {
93+
"connection error"
94+
} else if e.is_body() || e.is_decode() {
95+
"message body error"
96+
} else if e.is_request() {
97+
"request error"
98+
} else {
99+
"error"
100+
};
101+
tracing::warn!(
102+
"Outbound HTTP {}: URL {}, error detail {:?}",
103+
error_desc,
104+
e.url()
105+
.map(|u| u.to_string())
106+
.unwrap_or_else(|| "<unknown>".to_owned()),
107+
e
108+
);
109+
}
110+
response
111+
}
112+
87113
impl From<Method> for http::Method {
88114
fn from(m: Method) -> Self {
89115
match m {

0 commit comments

Comments
 (0)