diff --git a/ehttp/src/streaming/web.rs b/ehttp/src/streaming/web.rs index d6f042a..4f69563 100644 --- a/ehttp/src/streaming/web.rs +++ b/ehttp/src/streaming/web.rs @@ -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; @@ -19,8 +19,8 @@ pub async fn fetch_async_streaming( ) -> crate::Result>> { 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")] @@ -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; } }; @@ -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; } } diff --git a/ehttp/src/web.rs b/ehttp/src/web.rs index e25619a..c56c46c 100644 --- a/ehttp/src/web.rs +++ b/ehttp/src/web.rs @@ -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 { - 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::() { + 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 {