From c6b4d2ff63a8c3307e4592975f77114953f48486 Mon Sep 17 00:00:00 2001 From: /alex/ Date: Fri, 8 Mar 2024 15:31:36 +0100 Subject: [PATCH] Make `WalletAddressMismatch` error a struct variant (#2160) * make it a struct variant * nit * fix test --------- Co-authored-by: DaughterOfMars --- bindings/core/tests/serialize_error.rs | 2 +- sdk/src/wallet/core/builder.rs | 5 ++- sdk/src/wallet/error.rs | 50 ++++++++++++++------------ 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/bindings/core/tests/serialize_error.rs b/bindings/core/tests/serialize_error.rs index a83d4adb9a..1c2505a6b6 100644 --- a/bindings/core/tests/serialize_error.rs +++ b/bindings/core/tests/serialize_error.rs @@ -48,7 +48,7 @@ fn custom_error_serialization() { "type": "wallet", "error": { "type": "bipPathMismatch", - "error": "BIP44 mismatch: Some(Bip44 { coin_type: 4219, account: 0, change: 0, address_index: 0 }), existing bip path is: None" + "error": "bip path mismatch: Some(Bip44 { coin_type: 4219, account: 0, change: 0, address_index: 0 }), existing bip path is: None" } }) ); diff --git a/sdk/src/wallet/core/builder.rs b/sdk/src/wallet/core/builder.rs index 7848733294..bf5c7fb7c1 100644 --- a/sdk/src/wallet/core/builder.rs +++ b/sdk/src/wallet/core/builder.rs @@ -187,7 +187,10 @@ where if let Some(address) = &self.address { if let Some(loaded_address) = &loaded_address { if address != loaded_address { - return Err(WalletError::WalletAddressMismatch(address.clone())); + return Err(WalletError::WalletAddressMismatch { + provided: address.clone(), + expected: loaded_address.clone(), + }); } } else { verify_address = true; diff --git a/sdk/src/wallet/error.rs b/sdk/src/wallet/error.rs index 50d6f5488f..d306c732b6 100644 --- a/sdk/src/wallet/error.rs +++ b/sdk/src/wallet/error.rs @@ -30,30 +30,31 @@ use crate::{ #[strum(serialize_all = "camelCase")] #[non_exhaustive] pub enum WalletError { - /// Errors during backup creation or restoring + /// Errors during backup creation or restoring. #[error("backup failed {0}")] Backup(&'static str), /// Error from block crate. #[error("{0}")] Block(#[from] BlockError), - /// Burning or melting failed + /// Burning or melting failed. #[error("burning or melting failed: {0}")] BurningOrMeltingFailed(String), /// Client error. #[error("`{0}`")] Client(#[from] ClientError), - /// BIP44 coin type mismatch - #[error("BIP44 mismatch: {new_bip_path:?}, existing bip path is: {old_bip_path:?}")] + /// BIP path mismatch. + #[error("bip path mismatch: {new_bip_path:?}, existing bip path is: {old_bip_path:?}")] BipPathMismatch { new_bip_path: Option, old_bip_path: Option, }, - /// Crypto.rs error + /// Crypto.rs error. #[error("{0}")] Crypto(#[from] crypto::Error), - /// Custom input error + /// Custom input error. #[error("custom input error {0}")] CustomInput(String), + /// Missing delegation. #[error("no delegation output found with id {0}")] MissingDelegation(DelegationId), /// Insufficient funds to send transaction. @@ -67,7 +68,7 @@ pub enum WalletError { #[cfg_attr(docsrs, doc(cfg(feature = "events")))] #[error("invalid event type: {0}")] InvalidEventType(u8), - /// Invalid mnemonic error + /// Invalid mnemonic error. #[error("invalid mnemonic: {0}")] InvalidMnemonic(String), /// Invalid output kind. @@ -76,21 +77,21 @@ pub enum WalletError { /// Invalid parameter. #[error("invalid parameter: {0}")] InvalidParameter(&'static str), - /// Invalid Voting Power + /// Invalid voting power. #[cfg(feature = "participation")] #[cfg_attr(docsrs, doc(cfg(feature = "participation")))] #[error("invalid voting power")] InvalidVotingPower, - /// IO error. (storage, backup, restore) + /// IO error (storage, backup, restore). #[error("`{0}`")] Io(#[from] std::io::Error), /// serde_json error. #[error("`{0}`")] Json(#[from] serde_json::error::Error), - /// Error migrating storage or backup + /// Error migrating storage or backup. #[error("migration failed {0}")] Migration(String), - /// Minting failed + /// Minting failed. #[error("minting failed {0}")] MintingFailed(String), /// Missing BIP path. @@ -99,10 +100,10 @@ pub enum WalletError { /// Missing parameter. #[error("missing parameter: {0}")] MissingParameter(&'static str), - /// Nft not found in unspent outputs + /// Nft not found in unspent outputs. #[error("nft not found in unspent outputs")] NftNotFoundInUnspentOutputs, - /// No outputs available for consolidating + /// No outputs available for consolidating. #[error( "nothing to consolidate: available outputs: {available_outputs}, consolidation threshold: {consolidation_threshold}" )] @@ -115,7 +116,7 @@ pub enum WalletError { /// Errors not covered by other variants. #[error(transparent)] Other(#[from] Box), - /// Participation error + /// Participation error. #[cfg(feature = "participation")] #[cfg_attr(docsrs, doc(cfg(feature = "participation")))] #[error("participation error {0}")] @@ -123,25 +124,28 @@ pub enum WalletError { /// Storage access error. #[error("error accessing storage: {0}")] Storage(String), - /// Can't use Wallet API because the storage is encrypted + /// Can't use Wallet API because the storage is encrypted. #[error("can't perform operation while storage is encrypted; use Wallet::set_storage_password to decrypt storage")] StorageIsEncrypted, - /// Tokio task join error + /// Tokio task join error. #[error("{0}")] TaskJoin(#[from] tokio::task::JoinError), - /// Transaction not found + /// Transaction not found. #[error("transaction {0} not found")] TransactionNotFound(TransactionId), // TODO more precise error - /// Voting error + /// Voting error. #[cfg(feature = "participation")] #[cfg_attr(docsrs, doc(cfg(feature = "participation")))] #[error("voting error {0}")] Voting(String), - /// Address not the wallet address - #[error("address {0} is not the wallet address")] - WalletAddressMismatch(Bech32Address), - /// Action requires the wallet to be Ed25519 address based + /// Wallet address mismatch. + #[error("wallet address mismatch: {provided}, existing address is: {expected}")] + WalletAddressMismatch { + provided: Bech32Address, + expected: Bech32Address, + }, + /// Action requires the wallet to be Ed25519 address based. #[error("tried to perform an action that requires the wallet to be Ed25519 address based")] NonEd25519Address, /// Implicit account not found. @@ -150,8 +154,10 @@ pub enum WalletError { /// Account not found. #[error("account not found")] AccountNotFound, + /// Staking failed. #[error("staking failed: {0}")] StakingFailed(String), + /// Conversion error. #[error("{0}")] Convert(#[from] ConversionError), }