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
|
||||
EmptyBech32Payload,
|
||||
/// 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
|
||||
InvalidWitnessVersion(u8),
|
||||
/// Unable to parse witness version from string
|
||||
|
@ -81,12 +86,12 @@ pub enum Error {
|
|||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Error::Base58(ref e) => write!(f, "base58: {}", e),
|
||||
Error::Bech32(ref e) => write!(f, "bech32: {}", e),
|
||||
Error::Base58(_) => write!(f, "base58 address encoding error"),
|
||||
Error::Bech32(_) => write!(f, "bech32 address encoding error"),
|
||||
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::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::InvalidWitnessProgramLength(l) => write!(f,
|
||||
"the witness program must be between 2 and 40 bytes in length: length={}", l,
|
||||
|
@ -716,8 +721,9 @@ impl FromStr for Address {
|
|||
}
|
||||
|
||||
// Encoding check
|
||||
if version.bech32_variant() != variant {
|
||||
return Err(Error::InvalidBech32Variant);
|
||||
let expected = version.bech32_variant();
|
||||
if expected != variant {
|
||||
return Err(Error::InvalidBech32Variant { expected, found: variant });
|
||||
}
|
||||
|
||||
return Ok(Address {
|
||||
|
|
Loading…
Reference in New Issue