From c97e63ac8874c695da8c2bc62b89e2976d3a8006 Mon Sep 17 00:00:00 2001 From: nazeh Date: Sat, 14 Dec 2024 20:39:08 +0300 Subject: [PATCH] feat(js): update error handling after removing crate::error --- Cargo.lock | 4 +-- pubky/src/wasm/api/auth.rs | 33 +++++++++++------------- pubky/src/wasm/api/public.rs | 40 +++++------------------------ pubky/src/wasm/api/recovery_file.rs | 6 ++--- pubky/src/wasm/wrappers/keys.rs | 8 +++--- 5 files changed, 29 insertions(+), 62 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d0284a1..5095164 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1921,7 +1921,7 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkarr" version = "3.0.0" -source = "git+https://github.com/Pubky/pkarr?branch=v3#6821166474cd567a1e11f39e335b42c0029d4063" +source = "git+https://github.com/Pubky/pkarr?branch=v3#95f106e5a8a1d6275738aabf25c32c7cc7932a50" dependencies = [ "base32", "byteorder", @@ -1958,7 +1958,7 @@ dependencies = [ [[package]] name = "pkarr-relay" version = "0.1.0" -source = "git+https://github.com/Pubky/pkarr?branch=v3#6821166474cd567a1e11f39e335b42c0029d4063" +source = "git+https://github.com/Pubky/pkarr?branch=v3#95f106e5a8a1d6275738aabf25c32c7cc7932a50" dependencies = [ "anyhow", "axum", diff --git a/pubky/src/wasm/api/auth.rs b/pubky/src/wasm/api/auth.rs index f277035..931b2a6 100644 --- a/pubky/src/wasm/api/auth.rs +++ b/pubky/src/wasm/api/auth.rs @@ -6,8 +6,6 @@ use pubky_common::capabilities::Capabilities; use crate::Client; -use crate::Error; - use crate::wasm::wrappers::keys::{Keypair, PublicKey}; use crate::wasm::wrappers::session::Session; @@ -28,7 +26,7 @@ impl Client { Ok(Session( self.inner_signup(keypair.as_inner(), homeserver.as_inner()) .await - .map_err(JsValue::from)?, + .map_err(|e| JsValue::from_str(&e.to_string()))?, )) } @@ -41,7 +39,7 @@ impl Client { self.inner_session(pubky.as_inner()) .await .map(|s| s.map(Session)) - .map_err(|e| e.into()) + .map_err(|e| JsValue::from_str(&e.to_string())) } /// Signout from a homeserver. @@ -49,7 +47,7 @@ impl Client { pub async fn signout(&self, pubky: &PublicKey) -> Result<(), JsValue> { self.inner_signout(pubky.as_inner()) .await - .map_err(|e| e.into()) + .map_err(|e| JsValue::from_str(&e.to_string())) } /// Signin to a homeserver using the root Keypair. @@ -58,7 +56,7 @@ impl Client { self.inner_signin(keypair.as_inner()) .await .map(|_| ()) - .map_err(|e| e.into()) + .map_err(|e| JsValue::from_str(&e.to_string())) } /// Return `pubkyauth://` url and wait for the incoming [AuthToken] @@ -68,14 +66,14 @@ impl Client { /// Returns a tuple of [pubkyAuthUrl, Promise] #[wasm_bindgen(js_name = "authRequest")] pub fn auth_request(&self, relay: &str, capabilities: &str) -> Result { - let mut relay: Url = relay - .try_into() - .map_err(|_| Error::Generic("Invalid relay Url".into()))?; + let mut relay: Url = relay.try_into().map_err(|_| "Invalid relay Url")?; - let (pubkyauth_url, client_secret) = self.create_auth_request( - &mut relay, - &Capabilities::try_from(capabilities).map_err(|_| "Invalid capaiblities")?, - )?; + let (pubkyauth_url, client_secret) = self + .create_auth_request( + &mut relay, + &Capabilities::try_from(capabilities).map_err(|_| "Invalid capaiblities")?, + ) + .map_err(|e| JsValue::from_str(&e.to_string()))?; let this = self.clone(); @@ -83,7 +81,7 @@ impl Client { this.subscribe_to_auth_response(relay, &client_secret) .await .map(|pubky| JsValue::from(PublicKey(pubky))) - .map_err(|err| JsValue::from_str(&format!("{:?}", err))) + .map_err(|e| JsValue::from_str(&e.to_string())) }; let promise = wasm_bindgen_futures::future_to_promise(future); @@ -104,12 +102,11 @@ impl Client { keypair: &Keypair, pubkyauth_url: &str, ) -> Result<(), JsValue> { - let pubkyauth_url: Url = pubkyauth_url - .try_into() - .map_err(|_| Error::Generic("Invalid relay Url".into()))?; + let pubkyauth_url: Url = pubkyauth_url.try_into().map_err(|_| "Invalid relay Url")?; self.inner_send_auth_token(keypair.as_inner(), pubkyauth_url) - .await?; + .await + .map_err(|e| JsValue::from_str(&e.to_string()))?; Ok(()) } diff --git a/pubky/src/wasm/api/public.rs b/pubky/src/wasm/api/public.rs index fc62b8e..febc952 100644 --- a/pubky/src/wasm/api/public.rs +++ b/pubky/src/wasm/api/public.rs @@ -1,40 +1,12 @@ //! Wasm bindings for the /pub/ api +use js_sys::Array; use wasm_bindgen::prelude::*; -use reqwest::{Method, StatusCode}; - -use js_sys::{Array, Uint8Array}; - use crate::Client; #[wasm_bindgen] impl Client { - #[wasm_bindgen] - /// Upload a small payload to a given path. - pub async fn put(&self, url: &str, content: &[u8]) -> Result<(), JsValue> { - self.inner_put(url, content).await.map_err(|e| e.into()) - } - - /// Download a small payload from a given path relative to a pubky author. - #[wasm_bindgen] - pub async fn get(&self, url: &str) -> Result, JsValue> { - self.inner_get(url) - .await - .map(|b| b.map(|b| (&*b).into())) - .map_err(|e| e.into()) - } - - /// Delete a file at a path relative to a pubky author. - #[wasm_bindgen] - pub async fn delete(&self, url: &str) -> Result<(), JsValue> { - self.inner_request(Method::DELETE, url) - .await - .send() - .await - .map_err(|e| e.into()) - } - /// Returns a list of Pubky urls (as strings). /// /// - `url`: The Pubky url (string) to the directory you want to list its content. @@ -56,7 +28,8 @@ impl Client { if let Some(cursor) = cursor { return self - .inner_list(url)? + .inner_list(url) + .map_err(|e| JsValue::from_str(&e.to_string()))? .reverse(reverse.unwrap_or(false)) .limit(limit.unwrap_or(u16::MAX)) .cursor(&cursor) @@ -72,10 +45,11 @@ impl Client { js_array }) - .map_err(|e| e.into()); + .map_err(|e| JsValue::from_str(&e.to_string())); } - self.inner_list(url)? + self.inner_list(url) + .map_err(|e| JsValue::from_str(&e.to_string()))? .reverse(reverse.unwrap_or(false)) .limit(limit.unwrap_or(u16::MAX)) .shallow(shallow.unwrap_or(false)) @@ -90,6 +64,6 @@ impl Client { js_array }) - .map_err(|e| e.into()) + .map_err(|e| JsValue::from_str(&e.to_string())) } } diff --git a/pubky/src/wasm/api/recovery_file.rs b/pubky/src/wasm/api/recovery_file.rs index 89cff37..8a968f4 100644 --- a/pubky/src/wasm/api/recovery_file.rs +++ b/pubky/src/wasm/api/recovery_file.rs @@ -1,8 +1,6 @@ use js_sys::Uint8Array; use wasm_bindgen::prelude::{wasm_bindgen, JsValue}; -use crate::error::Error; - use crate::wasm::wrappers::keys::Keypair; /// Create a recovery file of the `keypair`, containing the secret key encrypted @@ -11,7 +9,7 @@ use crate::wasm::wrappers::keys::Keypair; pub fn create_recovery_file(keypair: &Keypair, passphrase: &str) -> Result { pubky_common::recovery_file::create_recovery_file(keypair.as_inner(), passphrase) .map(|b| b.as_slice().into()) - .map_err(|e| Error::from(e).into()) + .map_err(|e| JsValue::from_str(&e.to_string())) } /// Create a recovery file of the `keypair`, containing the secret key encrypted @@ -20,5 +18,5 @@ pub fn create_recovery_file(keypair: &Keypair, passphrase: &str) -> Result Result { pubky_common::recovery_file::decrypt_recovery_file(recovery_file, passphrase) .map(Keypair::from) - .map_err(|e| Error::from(e).into()) + .map_err(|e| JsValue::from_str(&e.to_string())) } diff --git a/pubky/src/wasm/wrappers/keys.rs b/pubky/src/wasm/wrappers/keys.rs index 29ac3d3..8888894 100644 --- a/pubky/src/wasm/wrappers/keys.rs +++ b/pubky/src/wasm/wrappers/keys.rs @@ -1,7 +1,5 @@ use wasm_bindgen::prelude::*; -use crate::Error; - #[wasm_bindgen] pub struct Keypair(pkarr::Keypair); @@ -80,9 +78,9 @@ impl PublicKey { .as_string() .ok_or("Couldn't create a PublicKey from this type of value")?; - Ok(PublicKey( - pkarr::PublicKey::try_from(string).map_err(Error::PublicKeyError)?, - )) + Ok(PublicKey(pkarr::PublicKey::try_from(string).map_err( + |_| "Couldn't create a PublicKey from this type of value", + )?)) } }