Improving error information for address parser
This commit is contained in:
parent
eeeb722155
commit
c1991d748f
|
@ -61,7 +61,12 @@ pub enum Error {
|
||||||
/// The bech32 payload was empty
|
/// The bech32 payload was empty
|
||||||
EmptyBech32Payload,
|
EmptyBech32Payload,
|
||||||
/// The wrong checksum algorithm was used. See BIP-0350.
|
/// The wrong checksum algorithm was used. See BIP-0350.
|
||||||
InvalidBech32Variant,
|
InvalidBech32Variant {
|
||||||
|
/// Bech32 variant that is required by the used Witness version
|
||||||
|
expected: bech32::Variant,
|
||||||
|
/// The actual Bech32 variant encoded in the address representation
|
||||||
|
found: bech32::Variant
|
||||||
|
},
|
||||||
/// Script version must be 0 to 16 inclusive
|
/// Script version must be 0 to 16 inclusive
|
||||||
InvalidWitnessVersion(u8),
|
InvalidWitnessVersion(u8),
|
||||||
/// Unable to parse witness version from string
|
/// Unable to parse witness version from string
|
||||||
|
@ -81,12 +86,12 @@ pub enum Error {
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
Error::Base58(ref e) => write!(f, "base58: {}", e),
|
Error::Base58(_) => write!(f, "base58 address encoding error"),
|
||||||
Error::Bech32(ref e) => write!(f, "bech32: {}", e),
|
Error::Bech32(_) => write!(f, "bech32 address encoding error"),
|
||||||
Error::EmptyBech32Payload => write!(f, "the bech32 payload was empty"),
|
Error::EmptyBech32Payload => write!(f, "the bech32 payload was empty"),
|
||||||
Error::InvalidBech32Variant => write!(f, "invalid bech32 checksum variant"),
|
Error::InvalidBech32Variant { expected, found } => write!(f, "invalid bech32 checksum variant found {:?} when {:?} was expected", found, expected),
|
||||||
Error::InvalidWitnessVersion(v) => write!(f, "invalid witness script version: {}", v),
|
Error::InvalidWitnessVersion(v) => write!(f, "invalid witness script version: {}", v),
|
||||||
Error::UnparsableWitnessVersion(ref e) => write!(f, "Incorrect format of a witness version byte: {}", e),
|
Error::UnparsableWitnessVersion(_) => write!(f, "incorrect format of a witness version byte"),
|
||||||
Error::MalformedWitnessVersion => f.write_str("bitcoin script opcode does not match any known witness version, the script is malformed"),
|
Error::MalformedWitnessVersion => f.write_str("bitcoin script opcode does not match any known witness version, the script is malformed"),
|
||||||
Error::InvalidWitnessProgramLength(l) => write!(f,
|
Error::InvalidWitnessProgramLength(l) => write!(f,
|
||||||
"the witness program must be between 2 and 40 bytes in length: length={}", l,
|
"the witness program must be between 2 and 40 bytes in length: length={}", l,
|
||||||
|
@ -716,8 +721,9 @@ impl FromStr for Address {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encoding check
|
// Encoding check
|
||||||
if version.bech32_variant() != variant {
|
let expected = version.bech32_variant();
|
||||||
return Err(Error::InvalidBech32Variant);
|
if expected != variant {
|
||||||
|
return Err(Error::InvalidBech32Variant { expected, found: variant });
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(Address {
|
return Ok(Address {
|
||||||
|
|
Loading…
Reference in New Issue