Skip to content

Commit

Permalink
Make WalletAddressMismatch error a struct variant (#2160)
Browse files Browse the repository at this point in the history
* make it a struct variant

* nit

* fix test

---------

Co-authored-by: DaughterOfMars <[email protected]>
  • Loading branch information
Alex6323 and DaughterOfMars authored Mar 8, 2024
1 parent 67ea8cb commit c6b4d2f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bindings/core/tests/serialize_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
})
);
Expand Down
5 changes: 4 additions & 1 deletion sdk/src/wallet/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
50 changes: 28 additions & 22 deletions sdk/src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bip44>,
old_bip_path: Option<Bip44>,
},
/// 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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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}"
)]
Expand All @@ -115,33 +116,36 @@ pub enum WalletError {
/// Errors not covered by other variants.
#[error(transparent)]
Other(#[from] Box<dyn std::error::Error + Send + Sync>),
/// Participation error
/// Participation error.
#[cfg(feature = "participation")]
#[cfg_attr(docsrs, doc(cfg(feature = "participation")))]
#[error("participation error {0}")]
Participation(#[from] crate::types::api::plugins::participation::error::Error),
/// 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.
Expand All @@ -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),
}
Expand Down

0 comments on commit c6b4d2f

Please sign in to comment.