Skip to content

Commit 529fc0d

Browse files
authored
Allow handling ErrorCode from outgoing_handler::handle (#121)
* Allow catching `ErrorCode` from `outgoing_handler::handle` * Test `ErrorCode` handling * Reformat test file
1 parent 3b97964 commit 529fc0d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/http/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Client {
3030
let wasi_body = wasi_req.body().unwrap();
3131

3232
// 1. Start sending the request head
33-
let res = wasip2::http::outgoing_handler::handle(wasi_req, self.wasi_options()?).unwrap();
33+
let res = wasip2::http::outgoing_handler::handle(wasi_req, self.wasi_options()?)?;
3434

3535
let ((), body) = futures_lite::future::try_zip(
3636
async move {

tests/http_handle_error_code.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use wstd::http::{Body, Client, Request, error::ErrorCode};
2+
3+
/// Test that `outgoing_handler::handle` errors are properly propagated.
4+
#[wstd::test]
5+
async fn handle_returns_error_code() -> Result<(), Box<dyn std::error::Error>> {
6+
let request = Request::get("ftp://example.com/").body(Body::empty())?;
7+
8+
let result = Client::new().send(request).await;
9+
10+
assert!(
11+
result.is_err(),
12+
"request with unsupported scheme should fail"
13+
);
14+
let error = result.unwrap_err();
15+
assert!(
16+
error.downcast_ref::<ErrorCode>().is_some(),
17+
"expected an ErrorCode, got: {error:?}"
18+
);
19+
20+
Ok(())
21+
}

0 commit comments

Comments
 (0)