Skip to content

Commit

Permalink
fix(fetcher): also use a String for Error non wasm implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexcited committed Jan 3, 2025
1 parent 3d83a7d commit 4386dee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
2 changes: 1 addition & 1 deletion fetcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repository.workspace = true
edition = "2021"

[dependencies]
thiserror = "2.0"
http = "1.2"
url = "2.5"

Expand All @@ -18,7 +19,6 @@ serde-wasm-bindgen = "0.6"
serde_bytes = "0.11"
wasm-bindgen = "0.2"
js-sys = "0.3"
thiserror = "2.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
reqwest = "0.12"
23 changes: 9 additions & 14 deletions fetcher/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
pub use http::{HeaderMap, HeaderName, Method};
pub use url::Url;

#[cfg(not(target_arch = "wasm32"))]
pub use reqwest::Error as FetcherError;

#[cfg(target_arch = "wasm32")]
use serde::{Deserialize, Serialize, Serializer};

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen::prelude::wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = liUtilsFetcher, js_name = FetcherError)]
type JsFetcherError;
mod bridge;

#[wasm_bindgen(constructor, js_namespace = liUtilsFetcher, js_name = FetcherError)]
fn new(message: &str) -> JsFetcherError;
}

#[cfg(target_arch = "wasm32")]
#[derive(thiserror::Error, Debug)]
pub struct FetcherError(String);

#[cfg(target_arch = "wasm32")]
impl std::fmt::Display for FetcherError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
Expand All @@ -31,7 +19,14 @@ impl std::fmt::Display for FetcherError {
#[cfg(target_arch = "wasm32")]
impl From<FetcherError> for wasm_bindgen::JsValue {
fn from(error: FetcherError) -> Self {
JsFetcherError::new(&error.0).into()
bridge::FetcherError::new(&error.0).into()
}
}

#[cfg(not(target_arch = "wasm32"))]
impl From<reqwest::Error> for FetcherError {
fn from(error: reqwest::Error) -> Self {
FetcherError(error.to_string())
}
}

Expand Down

0 comments on commit 4386dee

Please sign in to comment.