From 73e876ffd4b553d9fe3656cc9a9ba7569ff4416f Mon Sep 17 00:00:00 2001 From: Subhradeep Chakraborty Date: Wed, 1 Mar 2023 19:40:28 +0530 Subject: [PATCH] Include address in Error::NetworkValidation --- bitcoin/src/address.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bitcoin/src/address.rs b/bitcoin/src/address.rs index e83eb8bd..bdf4e7e6 100644 --- a/bitcoin/src/address.rs +++ b/bitcoin/src/address.rs @@ -92,7 +92,9 @@ pub enum Error { /// Network that was required. required: Network, /// Network on which the address was found to be valid. - found: Network + found: Network, + /// The address itself + address: Address } } @@ -112,7 +114,11 @@ impl fmt::Display for Error { Error::ExcessiveScriptSize => write!(f, "script size exceed 520 bytes"), Error::UnrecognizedScript => write!(f, "script is not a p2pkh, p2sh or witness program"), Error::UnknownAddressType(ref s) => write!(f, "unknown address type: '{}' is either invalid or not supported in rust-bitcoin", s), - Error::NetworkValidation { required, found } => write!(f, "address's network {} is different from required {}", found, required), + Error::NetworkValidation { required, found, ref address } => { + write!(f, "address ")?; + address.fmt_internal(f)?; // Using fmt_internal in order to remove the "Address(..)" wrapper + write!(f, " belongs to network {} which is different from required {}", found, required) + } } } } @@ -1003,7 +1009,7 @@ impl Address { if self.is_valid_for_network(required) { Ok(self.assume_checked()) } else { - Err(Error::NetworkValidation { found: self.network, required }) + Err(Error::NetworkValidation { found: self.network, required, address: self }) } }