Skip to content

Commit

Permalink
custom error serializations
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Oct 31, 2023
1 parent 2aab93c commit f71d140
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions bindings/core/tests/serialize_error.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use iota_sdk::{client::Error as ClientError, wallet::Error as WalletError};
use crypto::keys::bip44::Bip44;
use iota_sdk::{
client::{constants::SHIMMER_COIN_TYPE, Error as ClientError},
wallet::Error as WalletError,
};
use iota_sdk_bindings_core::Error;
use pretty_assertions::assert_eq;

#[test]
fn custom_error_serialization() {
// testing a unit-type-like error
let error = Error::Client(ClientError::HealthyNodePoolEmpty);
assert_eq!(
serde_json::to_string(&error).unwrap(),
"{\"type\":\"client\",\"error\":\"no healthy node available\"}"
);
let error = Error::Wallet(WalletError::MissingBipPath);
// testing a tuple-like error
let error = Error::Wallet(WalletError::InvalidMnemonic("nilly willy".to_string()));
assert_eq!(
serde_json::to_string(&error).unwrap(),
"{\"type\":\"wallet\",\"error\":\"missing BIP path\"}"
"{\"type\":\"wallet\",\"error\":\"invalid mnemonic: nilly willy\"}"
);
// testing a struct-like error
let error = Error::Wallet(WalletError::BipPathMismatch {
old_bip_path: None,
new_bip_path: Some(Bip44::new(SHIMMER_COIN_TYPE)),
});
assert_eq!(
serde_json::to_string(&error).unwrap(),
"{\"type\":\"wallet\",\"error\":\"BIP44 mismatch: Some(Bip44 { coin_type: 4219, account: 0, change: 0, address_index: 0 }), existing bip path is: None\"}"
);
}

0 comments on commit f71d140

Please sign in to comment.