Skip to content

Commit 6eebc83

Browse files
committed
Make header_map_to_wasi bubble up its error.
The client can bubble this into a `wstd::http::Error`. The server currently just does `.expect`, but at least now, if we want it to do something different, it's more clear what the options are.
1 parent e44c1ca commit 6eebc83

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/http/error.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::http::fields::ToWasiHeaderError;
12
use std::fmt;
23

34
/// The `http` result type.
@@ -78,9 +79,12 @@ impl From<WasiHttpErrorCode> for Error {
7879
}
7980
}
8081

81-
impl From<WasiHttpHeaderError> for Error {
82-
fn from(e: WasiHttpHeaderError) -> Error {
83-
ErrorVariant::WasiHeader(e).into()
82+
impl From<ToWasiHeaderError> for Error {
83+
fn from(error: ToWasiHeaderError) -> Error {
84+
Error {
85+
variant: ErrorVariant::WasiHeader(error.error),
86+
context: vec![error.context],
87+
}
8488
}
8589
}
8690

src/http/fields.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
pub use http::header::{HeaderMap, HeaderName, HeaderValue};
22

3-
use super::{Error, Result};
4-
use wasi::http::types::Fields;
3+
use super::Error;
4+
use wasi::http::types::{Fields, HeaderError as WasiHttpHeaderError};
55

6-
pub(crate) fn header_map_from_wasi(wasi_fields: Fields) -> Result<HeaderMap> {
6+
pub(crate) fn header_map_from_wasi(wasi_fields: Fields) -> Result<HeaderMap, Error> {
77
let mut output = HeaderMap::new();
88
for (key, value) in wasi_fields.entries() {
99
let key = HeaderName::from_bytes(key.as_bytes())
@@ -15,13 +15,22 @@ pub(crate) fn header_map_from_wasi(wasi_fields: Fields) -> Result<HeaderMap> {
1515
Ok(output)
1616
}
1717

18-
pub(crate) fn header_map_to_wasi(header_map: &HeaderMap) -> Fields {
18+
pub(crate) fn header_map_to_wasi(header_map: &HeaderMap) -> Result<Fields, ToWasiHeaderError> {
1919
let wasi_fields = Fields::new();
2020
for (key, value) in header_map {
2121
// Unwrap because `HeaderMap` has already validated the headers.
2222
wasi_fields
2323
.append(&key.as_str(), &value.as_bytes())
24-
.unwrap_or_else(|err| panic!("header named {key}: {err:?}"));
24+
.map_err(|error| ToWasiHeaderError {
25+
error,
26+
context: format!("header {key}: {value:?}"),
27+
})?;
2528
}
26-
wasi_fields
29+
Ok(wasi_fields)
30+
}
31+
32+
#[derive(Debug)]
33+
pub(crate) struct ToWasiHeaderError {
34+
pub(crate) error: WasiHttpHeaderError,
35+
pub(crate) context: String,
2736
}

src/http/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use wasi::http::types::IncomingRequest;
1313
pub use http::Request;
1414

1515
pub(crate) fn try_into_outgoing<T>(request: Request<T>) -> Result<(OutgoingRequest, T), Error> {
16-
let wasi_req = OutgoingRequest::new(header_map_to_wasi(request.headers()));
16+
let wasi_req = OutgoingRequest::new(header_map_to_wasi(request.headers())?);
1717

1818
let (parts, body) = request.into_parts();
1919

src/http/server.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Responder {
6161
/// # }
6262
/// ```
6363
pub fn start_response(self, response: Response<BodyForthcoming>) -> OutgoingBody {
64-
let wasi_headers = header_map_to_wasi(response.headers());
64+
let wasi_headers = header_map_to_wasi(response.headers()).expect("header error");
6565
let wasi_response = OutgoingResponse::new(wasi_headers);
6666
let wasi_status = response.status().as_u16();
6767

@@ -100,7 +100,7 @@ impl Responder {
100100
let headers = response.headers();
101101
let status = response.status().as_u16();
102102

103-
let wasi_headers = header_map_to_wasi(headers);
103+
let wasi_headers = header_map_to_wasi(headers).expect("header error");
104104

105105
// Consume the `response` and prepare to write the body.
106106
let mut body = response.into_body();
@@ -173,7 +173,8 @@ impl Finished {
173173
// If there was an I/O error, panic and don't call `OutgoingBody::finish`.
174174
let _ = result.expect("I/O error while writing the body");
175175

176-
let wasi_trailers = trailers.map(|trailers| header_map_to_wasi(&trailers));
176+
let wasi_trailers =
177+
trailers.map(|trailers| header_map_to_wasi(&trailers).expect("header error"));
177178

178179
wasi::http::types::OutgoingBody::finish(body, wasi_trailers)
179180
.expect("body length did not match Content-Length header value");

0 commit comments

Comments
 (0)