Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web: Improve opaque network error message #33

Merged
merged 3 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ehttp/src/streaming/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use futures_util::Stream;
use futures_util::StreamExt;
use wasm_bindgen::prelude::*;

use crate::web::{fetch_base, get_response_base, spawn_future, string_from_js_value};
use crate::web::{fetch_base, get_response_base, spawn_future, string_from_fetch_error};
use crate::Request;

use super::types::Part;
Expand All @@ -19,8 +19,8 @@ pub async fn fetch_async_streaming(
) -> crate::Result<impl Stream<Item = crate::Result<Part>>> {
let stream = fetch_jsvalue_stream(request)
.await
.map_err(string_from_js_value)?;
Ok(stream.map(|result| result.map_err(string_from_js_value)))
.map_err(string_from_fetch_error)?;
Ok(stream.map(|result| result.map_err(string_from_fetch_error)))
}

#[cfg(feature = "streaming")]
Expand Down Expand Up @@ -54,7 +54,7 @@ pub(crate) fn fetch_streaming(
let mut stream = match fetch_jsvalue_stream(&request).await {
Ok(stream) => stream,
Err(e) => {
on_data(Err(string_from_js_value(e)));
on_data(Err(string_from_fetch_error(e)));
return;
}
};
Expand All @@ -67,7 +67,7 @@ pub(crate) fn fetch_streaming(
}
}
Err(e) => {
on_data(Err(string_from_js_value(e)));
on_data(Err(string_from_fetch_error(e)));
return;
}
}
Expand Down
18 changes: 15 additions & 3 deletions ehttp/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@ use crate::{Request, Response};
/// NOTE: `Ok(…)` is returned on network error.
/// `Err` is only for failure to use the fetch API.
pub async fn fetch_async(request: &Request) -> crate::Result<Response> {
fetch_jsvalue(request).await.map_err(string_from_js_value)
fetch_jsvalue(request)
.await
.map_err(string_from_fetch_error)
}

pub(crate) fn string_from_js_value(value: JsValue) -> String {
value.as_string().unwrap_or_else(|| format!("{:#?}", value))
/// This should only be used to handle opaque exceptions thrown by the `fetch` call.
pub(crate) fn string_from_fetch_error(value: JsValue) -> String {
value.as_string().unwrap_or_else(|| {
// TypeError means that this is an opaque `network error`, as defined by the spec:
// https://fetch.spec.whatwg.org/
if value.has_type::<js_sys::TypeError>() {
web_sys::console::error_1(&value);
"Failed to fetch, check the developer console for details".to_owned()
} else {
format!("{:#?}", value)
}
})
}

pub(crate) async fn fetch_base(request: &Request) -> Result<web_sys::Response, JsValue> {
Expand Down
Loading