Skip to content

Commit e989790

Browse files
committed
Log outbound HTTP request error detail
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
1 parent 139c409 commit e989790

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
@@ -88,8 +88,8 @@ impl wasi_outbound_http::WasiOutboundHttp for OutboundHttp {
8888
.headers(headers)
8989
.body(body)
9090
.send(),
91-
)?;
92-
91+
);
92+
let res = log_request_error(res)?;
9393
Response::try_from(res)
9494
}))
9595
.map_err(|_| HttpError::RuntimeError)?,
@@ -98,13 +98,39 @@ impl wasi_outbound_http::WasiOutboundHttp for OutboundHttp {
9898
.request(method, url)
9999
.headers(headers)
100100
.body(body)
101-
.send()?;
101+
.send();
102+
let res = log_request_error(res)?;
102103
Ok(Response::try_from(res)?)
103104
}
104105
}
105106
}
106107
}
107108

109+
fn log_request_error<R>(response: Result<R, reqwest::Error>) -> Result<R, reqwest::Error> {
110+
if let Err(e) = &response {
111+
let error_desc = if e.is_timeout() {
112+
"timeout error"
113+
} else if e.is_connect() {
114+
"connection error"
115+
} else if e.is_body() || e.is_decode() {
116+
"message body error"
117+
} else if e.is_request() {
118+
"request error"
119+
} else {
120+
"error"
121+
};
122+
tracing::warn!(
123+
"Outbound HTTP {}: URL {}, error detail {:?}",
124+
error_desc,
125+
e.url()
126+
.map(|u| u.to_string())
127+
.unwrap_or_else(|| "<unknown>".to_owned()),
128+
e
129+
);
130+
}
131+
response
132+
}
133+
108134
impl From<Method> for http::Method {
109135
fn from(m: Method) -> Self {
110136
match m {

0 commit comments

Comments
 (0)