Correctly handle bicoinconsensus::Error

With the latest release of `rust-bitcoinconsensus` the
`bicoinconsensus::Error` now implements `std::error::Error`.

Correctly handle `bicoinconsensus::Error` by returning `Some(e)` and
using `write_err` as is standard across the code base.
This commit is contained in:
Tobin C. Harding 2022-09-02 13:19:02 +10:00
parent 556f85d993
commit a013847bd3
1 changed files with 12 additions and 11 deletions

View File

@ -19,6 +19,8 @@ use core::convert::TryFrom;
use core::{fmt, default::Default}; use core::{fmt, default::Default};
use core::ops::Index; use core::ops::Index;
use crate::internal_macros::debug_from_display; use crate::internal_macros::debug_from_display;
#[cfg(feature = "bitcoinconsensus")]
use crate::internal_macros::write_err;
#[cfg(feature = "serde")] use serde; #[cfg(feature = "serde")] use serde;
@ -169,16 +171,15 @@ 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 {
let str = match *self { match *self {
Error::NonMinimalPush => "non-minimal datapush", Error::NonMinimalPush => f.write_str("non-minimal datapush"),
Error::EarlyEndOfScript => "unexpected end of script", Error::EarlyEndOfScript => f.write_str("unexpected end of script"),
Error::NumericOverflow => "numeric overflow (number on stack larger than 4 bytes)", Error::NumericOverflow => f.write_str("numeric overflow (number on stack larger than 4 bytes)"),
#[cfg(feature = "bitcoinconsensus")] #[cfg(feature = "bitcoinconsensus")]
Error::BitcoinConsensus(ref _n) => "bitcoinconsensus verification failed", Error::BitcoinConsensus(ref e) => write_err!(f, "bitcoinconsensus verification failed"; e),
Error::UnknownSpentOutput(ref _point) => "unknown spent output Transaction::verify()", Error::UnknownSpentOutput(ref point) => write!(f, "unknown spent output: {}", point),
Error::Serialization => "can not serialize the spending transaction in Transaction::verify()", Error::Serialization => f.write_str("can not serialize the spending transaction in Transaction::verify()"),
}; }
f.write_str(str)
} }
} }
@ -188,14 +189,14 @@ impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use self::Error::*; use self::Error::*;
match self { match *self {
NonMinimalPush NonMinimalPush
| EarlyEndOfScript | EarlyEndOfScript
| NumericOverflow | NumericOverflow
| UnknownSpentOutput(_) | UnknownSpentOutput(_)
| Serialization => None, | Serialization => None,
#[cfg(feature = "bitcoinconsensus")] #[cfg(feature = "bitcoinconsensus")]
BitcoinConsensus(_) => None, BitcoinConsensus(ref e) => Some(e),
} }
} }
} }