diff --git a/bindings/nostr-sdk-ffi/src/error.rs b/bindings/nostr-sdk-ffi/src/error.rs index 2f5fa6505..450bd296d 100644 --- a/bindings/nostr-sdk-ffi/src/error.rs +++ b/bindings/nostr-sdk-ffi/src/error.rs @@ -3,10 +3,7 @@ // Distributed under the MIT software license use std::fmt; -use std::net::AddrParseError; -use nostr::message::MessageHandleError; -use tracing::subscriber::SetGlobalDefaultError; use uniffi::Error; pub type Result = std::result::Result; @@ -17,8 +14,6 @@ pub enum NostrSdkError { Generic(String), } -impl std::error::Error for NostrSdkError {} - impl fmt::Display for NostrSdkError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { @@ -27,254 +22,11 @@ impl fmt::Display for NostrSdkError { } } -impl From for NostrSdkError { - fn from(e: std::num::ParseIntError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: std::char::ParseCharError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::signer::SignerError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::key::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::key::vanity::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: MessageHandleError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::types::metadata::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::event::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::event::builder::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::event::unsigned::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::event::tag::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip01::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip04::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip05::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip06::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip11::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip19::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip21::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip26::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip44::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip46::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip47::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip49::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip53::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip57::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip59::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::nips::nip90::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::secp256k1::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::types::url::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::types::url::ParseError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::hashes::hex::HexToArrayError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::serde_json::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr::event::id::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: AddrParseError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: SetGlobalDefaultError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_sdk::client::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_sdk::pool::relay::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_sdk::pool::pool::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_sdk::prelude::DatabaseError) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_connect::error::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_sdk::nwc::Error) -> NostrSdkError { - Self::Generic(e.to_string()) - } -} - -impl From for NostrSdkError { - fn from(e: nostr_relay_builder::error::Error) -> NostrSdkError { +impl From for NostrSdkError +where + T: std::error::Error, +{ + fn from(e: T) -> NostrSdkError { Self::Generic(e.to_string()) } } diff --git a/bindings/nostr-sdk-ffi/src/protocol/signer/custom.rs b/bindings/nostr-sdk-ffi/src/protocol/signer/custom.rs index a82bbecc3..cb7960577 100644 --- a/bindings/nostr-sdk-ffi/src/protocol/signer/custom.rs +++ b/bindings/nostr-sdk-ffi/src/protocol/signer/custom.rs @@ -50,6 +50,7 @@ impl fmt::Debug for IntermediateCustomNostrSigner { } mod inner { + use std::fmt; use std::ops::Deref; use std::sync::Arc; @@ -57,7 +58,17 @@ mod inner { use nostr::prelude::*; use super::IntermediateCustomNostrSigner; - use crate::error::NostrSdkError; + + #[derive(Debug)] + struct Error(String); + + impl std::error::Error for Error {} + + impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } + } #[async_trait] impl NostrSigner for IntermediateCustomNostrSigner { @@ -70,11 +81,9 @@ mod inner { .inner .get_public_key() .await - .map_err(SignerError::backend)? + .map_err(|e| SignerError::backend(Error(e.to_string())))? .ok_or_else(|| { - SignerError::backend(NostrSdkError::Generic(String::from( - "Received None instead of public key", - ))) + SignerError::backend(Error(String::from("Received None instead of public key"))) })?; Ok(**public_key) } @@ -85,11 +94,9 @@ mod inner { .inner .sign_event(unsigned) .await - .map_err(SignerError::backend)? + .map_err(|e| SignerError::backend(Error(e.to_string())))? .ok_or_else(|| { - SignerError::backend(NostrSdkError::Generic(String::from( - "Received None instead of event", - ))) + SignerError::backend(Error(String::from("Received None instead of event"))) })?; Ok(event.as_ref().deref().clone()) } @@ -103,7 +110,7 @@ mod inner { self.inner .nip04_encrypt(public_key, content.to_string()) .await - .map_err(SignerError::backend) + .map_err(|e| SignerError::backend(Error(e.to_string()))) } async fn nip04_decrypt( @@ -115,7 +122,7 @@ mod inner { self.inner .nip04_decrypt(public_key, encrypted_content.to_string()) .await - .map_err(SignerError::backend) + .map_err(|e| SignerError::backend(Error(e.to_string()))) } async fn nip44_encrypt( @@ -127,7 +134,7 @@ mod inner { self.inner .nip44_encrypt(public_key, content.to_string()) .await - .map_err(SignerError::backend) + .map_err(|e| SignerError::backend(Error(e.to_string()))) } async fn nip44_decrypt( @@ -139,7 +146,7 @@ mod inner { self.inner .nip44_decrypt(public_key, payload.to_string()) .await - .map_err(SignerError::backend) + .map_err(|e| SignerError::backend(Error(e.to_string()))) } } }