Skip to content

Commit 802d5d5

Browse files
TatriXMartinKavik
authored andcommitted
refactor(fetch): Pass Request construction errors to the caller
1 parent d5d4f20 commit 802d5d5

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/browser/fetch.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
3131
use crate::util::window;
3232
use serde_json;
33+
use std::convert::TryInto;
3334
use wasm_bindgen_futures::JsFuture;
3435
use web_sys;
3536

@@ -72,7 +73,8 @@ pub type Result<T> = std::result::Result<T, FetchError>;
7273
/// even if you get `Ok` from this function, you still need to check
7374
/// `Response` status for HTTP errors.
7475
pub async fn fetch<'a>(request: impl Into<Request<'a>>) -> Result<Response> {
75-
let promise = window().fetch_with_request(&request.into().into());
76+
let request = request.into();
77+
let promise = window().fetch_with_request(&request.try_into()?);
7678

7779
let raw_response = JsFuture::from(promise)
7880
.await
@@ -89,6 +91,8 @@ pub enum FetchError {
8991
DomException(web_sys::DomException),
9092
PromiseError(wasm_bindgen::JsValue),
9193
NetworkError(wasm_bindgen::JsValue),
94+
/// Request construction failed.
95+
RequestError(wasm_bindgen::JsValue),
9296
StatusError(Status),
9397
}
9498

src/browser/fetch/request.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{FetchError, Header, Headers, Method, Result};
44
use crate::browser::Url;
55
use gloo_timers::callback::Timeout;
66
use serde::Serialize;
7-
use std::{borrow::Cow, cell::RefCell, rc::Rc};
7+
use std::{borrow::Cow, cell::RefCell, convert::TryFrom, rc::Rc};
88
use wasm_bindgen::JsValue;
99

1010
/// Its methods configure the request, and handle the response. Many of them return the original
@@ -174,16 +174,17 @@ impl<'a> From<Url> for Request<'a> {
174174
}
175175
}
176176

177-
impl From<Request<'_>> for web_sys::Request {
178-
fn from(request: Request) -> Self {
177+
impl TryFrom<Request<'_>> for web_sys::Request {
178+
type Error = FetchError;
179+
fn try_from(request: Request) -> std::result::Result<Self, Self::Error> {
179180
let mut init = web_sys::RequestInit::new();
180181

181182
// headers
182-
let headers = web_sys::Headers::new().expect("fetch: cannot create headers");
183+
let headers = web_sys::Headers::new().map_err(FetchError::RequestError)?;
183184
for header in request.headers {
184185
headers
185186
.append(&header.name, &header.value)
186-
.expect("fetch: cannot create header")
187+
.map_err(FetchError::RequestError)?;
187188
}
188189
init.headers(&headers);
189190

@@ -250,7 +251,7 @@ impl From<Request<'_>> for web_sys::Request {
250251
//
251252
// See https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#Errors
252253
web_sys::Request::new_with_str_and_init(&request.url, &init)
253-
.expect("fetch: Cannot create request")
254+
.map_err(FetchError::RequestError)
254255
}
255256
}
256257

0 commit comments

Comments
 (0)