From 2a777fccedb22f9a4b9fa4a445df43e6b606b482 Mon Sep 17 00:00:00 2001 From: Fusee Date: Sun, 12 Nov 2023 19:11:39 +0100 Subject: [PATCH 1/2] Added which is invalid in AddressError::InvalidBech32String --- data/src/error/address.rs | 2 +- data/src/types/address.rs | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/data/src/error/address.rs b/data/src/error/address.rs index d1c7efd..ae2e5aa 100644 --- a/data/src/error/address.rs +++ b/data/src/error/address.rs @@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, PartialEq, Clone, Debug)] pub enum AddressError { /// Represents an error case where an invalid Bech32 string is provided. - InvalidBech32String, + InvalidBech32String { invalid_value: String }, /// Represents an error case where an `Address` cannot be converted to its Bech32 string representation. CannotConvertToBech32String, } diff --git a/data/src/types/address.rs b/data/src/types/address.rs index 2c8ee21..3dd6bfa 100644 --- a/data/src/types/address.rs +++ b/data/src/types/address.rs @@ -64,7 +64,7 @@ impl Address { /// let address = Address::from_bech32_string("erd1qqqqqqqqqqqqqpgq7ykazrzd905zvnlr88dpfw06677lxe9w0n4suz00uh").unwrap(); /// ``` pub fn from_bech32_string(bech32: &str) -> Result { - let Ok(address) = SDKAddress::from_bech32_string(bech32) else { return Err(AddressError::InvalidBech32String.into()) }; + let Ok(address) = SDKAddress::from_bech32_string(bech32) else { return Err(AddressError::InvalidBech32String { invalid_value: bech32.to_string() }.into()) }; Ok(Address(address)) } @@ -235,7 +235,7 @@ impl<'a> Deserialize<'a> for Address { mod tests { use multiversx_sc::types::ManagedAddress; use multiversx_sc_scenario::api::StaticApi; - use crate::Address; + use crate::{Address, AddressError, DataError}; use crate::types::managed::ManagedConvertible; use crate::types::native::NativeConvertible; @@ -261,4 +261,30 @@ mod tests { managed.to_byte_array() ) } + + #[test] + fn test_from_bech32_string_valid_address() { + Address::from_bech32_string("erd1an4xpn58j7ymd58m2jznr32t0vmas75egrdfa8mta6fzvqn9tkxq4jvghn").unwrap(); + } + + #[test] + fn test_from_bech32_string_invalid_address() { + let str = "erd1an4xpn58j7ymd58m2jznr32t0vmas75egrdfa8mta6fzvqn9tkxq4jvghm"; + let error = Address::from_bech32_string(str).unwrap_err(); + + let expected = DataError::Address(AddressError::InvalidBech32String { invalid_value: str.to_string() }); + + assert_eq!(error, expected); + } + + #[test] + fn test_from_bech32_string_invalid_address_bad_length() { + let str = "erd1an4xpn58j7ymd58m2jznr32t"; + let error = Address::from_bech32_string(str).unwrap_err(); + + let expected = DataError::Address(AddressError::InvalidBech32String { invalid_value: str.to_string() }); + + assert_eq!(error, expected); + } + } \ No newline at end of file From e2f55e886fb1229a89f63ad2b4ddb46796e09270 Mon Sep 17 00:00:00 2001 From: Fusee Date: Sun, 12 Nov 2023 19:16:41 +0100 Subject: [PATCH 2/2] fixes --- data/src/error/address.rs | 20 ++++++++------------ data/src/error/data.rs | 12 ------------ 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/data/src/error/address.rs b/data/src/error/address.rs index ae2e5aa..1f3cb27 100644 --- a/data/src/error/address.rs +++ b/data/src/error/address.rs @@ -10,21 +10,17 @@ use serde::{Deserialize, Serialize}; /// a centralized error type for broader error handling. /// /// # Variants -/// - `InvalidBech32String`: This error occurs when attempting to construct an `Address` -/// from an invalid Bech32 string. -/// - `CannotConvertToBech32String`: This error occurs when attempting to convert an `Address` -/// to its Bech32 string representation. -/// -/// # Example -/// ``` -/// # use novax_data::{AddressError, DataError}; -/// let error = AddressError::InvalidBech32String; -/// let data_error: DataError = error.into(); -/// ``` +/// - `InvalidBech32String`: Represents an error case where an invalid Bech32 string is provided. +/// - `CannotConvertToBech32String`: Represents an error case where an `Address` cannot be converted to its +/// Bech32 string representation. #[derive(Serialize, Deserialize, PartialEq, Clone, Debug)] pub enum AddressError { /// Represents an error case where an invalid Bech32 string is provided. - InvalidBech32String { invalid_value: String }, + InvalidBech32String { + /// The invalid Bech32 string that led to the error. Storing this string helps in diagnosing + /// the specific input that failed to parse, facilitating easier troubleshooting. + invalid_value: String + }, /// Represents an error case where an `Address` cannot be converted to its Bech32 string representation. CannotConvertToBech32String, } diff --git a/data/src/error/data.rs b/data/src/error/data.rs index 975a516..a623c20 100644 --- a/data/src/error/data.rs +++ b/data/src/error/data.rs @@ -13,18 +13,6 @@ use crate::error::utils::UtilsError; /// as represented by the `AddressError` enum. /// - `Utils(UtilsError)`: Encapsulates errors that occur within utility functions, /// as represented by the `UtilsError` enum. -/// -/// # Example -/// ``` -/// # use novax_data::{DataError, AddressError}; -/// let address_error = AddressError::InvalidBech32String; -/// let data_error: DataError = address_error.into(); -/// -/// match data_error { -/// DataError::Address(err) => println!("Address error: {:?}", err), -/// DataError::Utils(err) => println!("Utils error: {:?}", err), -/// } -/// ``` #[derive(Serialize, Deserialize, PartialEq, Clone, Debug)] pub enum DataError { /// Represents an error from address-related operations.