Skip to content

Commit 97de55b

Browse files
committed
Update errors to http_types errors
1 parent 20e1a14 commit 97de55b

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/wasm.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,13 @@ mod fetch {
7575
use wasm_bindgen_futures::JsFuture;
7676
use web_sys::{window, RequestInit};
7777

78-
use std::io;
7978
use std::iter::{IntoIterator, Iterator};
8079
use std::pin::Pin;
8180

81+
use http_types::StatusCode;
82+
83+
use crate::Error;
84+
8285
/// Create a new fetch request.
8386
8487
/// An HTTP Fetch Request.
@@ -91,7 +94,7 @@ mod fetch {
9194

9295
impl Request {
9396
/// Create a new instance.
94-
pub(crate) async fn new(mut req: super::Request) -> Result<Self, io::Error> {
97+
pub(crate) async fn new(mut req: super::Request) -> Result<Self, Error> {
9598
// create a fetch request initaliser
9699
let mut init = RequestInit::new();
97100

@@ -106,7 +109,7 @@ mod fetch {
106109
// js is just a portal into WASM linear memory, and if the underlying data is moved the
107110
// js ref will become silently invalid
108111
let body_buf = body.into_bytes().await.map_err(|_| {
109-
io::Error::new(io::ErrorKind::Other, "could not read body into a buffer")
112+
Error::from_str(StatusCode::BadRequest, "could not read body into a buffer")
110113
})?;
111114
let body_pinned = Pin::new(body_buf);
112115
if body_pinned.len() > 0 {
@@ -115,8 +118,8 @@ mod fetch {
115118
}
116119

117120
let request = web_sys::Request::new_with_str_and_init(&uri, &init).map_err(|e| {
118-
io::Error::new(
119-
io::ErrorKind::Other,
121+
Error::from_str(
122+
StatusCode::BadRequest,
120123
format!("failed to create request: {:?}", e),
121124
)
122125
})?;
@@ -128,8 +131,8 @@ mod fetch {
128131
let value = value.as_str();
129132

130133
request.headers().set(name, value).map_err(|_| {
131-
io::Error::new(
132-
io::ErrorKind::Other,
134+
Error::from_str(
135+
StatusCode::BadRequest,
133136
format!("could not add header: {} = {}", name, value),
134137
)
135138
})?;
@@ -143,11 +146,14 @@ mod fetch {
143146

144147
/// Submit a request
145148
// TODO(yoshuawuyts): turn this into a `Future` impl on `Request` instead.
146-
pub(crate) async fn send(self) -> Result<Response, io::Error> {
149+
pub(crate) async fn send(self) -> Result<Response, Error> {
147150
// Send the request.
148151
let window = window().expect("A global window object could not be found");
149152
let promise = window.fetch_with_request(&self.request);
150-
let resp = JsFuture::from(promise).await.unwrap();
153+
let resp = JsFuture::from(promise)
154+
.await
155+
.map_err(|e| Error::from_str(StatusCode::BadRequest, format!("{:?}", e)))?;
156+
151157
debug_assert!(resp.is_instance_of::<web_sys::Response>());
152158
let res: web_sys::Response = resp.dyn_into().unwrap();
153159

0 commit comments

Comments
 (0)