From 4386dee6e1e2544c9298799ac50182f879a7a73f Mon Sep 17 00:00:00 2001 From: Mikkel ALMONTE--RINGAUD Date: Fri, 3 Jan 2025 04:07:08 +0100 Subject: [PATCH] fix(fetcher): also use a `String` for Error non wasm implementation --- fetcher/Cargo.toml | 2 +- fetcher/src/lib.rs | 23 +++++++++-------------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/fetcher/Cargo.toml b/fetcher/Cargo.toml index 7816cdc..819d94a 100644 --- a/fetcher/Cargo.toml +++ b/fetcher/Cargo.toml @@ -8,6 +8,7 @@ repository.workspace = true edition = "2021" [dependencies] +thiserror = "2.0" http = "1.2" url = "2.5" @@ -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" diff --git a/fetcher/src/lib.rs b/fetcher/src/lib.rs index adc0417..ffbf85c 100644 --- a/fetcher/src/lib.rs +++ b/fetcher/src/lib.rs @@ -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) @@ -31,7 +19,14 @@ impl std::fmt::Display for FetcherError { #[cfg(target_arch = "wasm32")] impl From 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 for FetcherError { + fn from(error: reqwest::Error) -> Self { + FetcherError(error.to_string()) } }