Skip to content

Commit 3f76dc6

Browse files
committed
Do not print error when displaying for std builds
We implement `source` for all our error types. This means that we should not display the source error explicitly because users can call `source` to get the source error. However, `std::Error::source()` is only available for "std" builds, so that we do not loose the error source information in "no-std" builds add a macro that conditionally adds the source onto the error message.
1 parent a863cfd commit 3f76dc6

File tree

16 files changed

+57
-34
lines changed

16 files changed

+57
-34
lines changed

src/blockdata/transaction.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ pub enum ParseOutPointError {
130130
impl fmt::Display for ParseOutPointError {
131131
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
132132
match *self {
133-
ParseOutPointError::Txid(ref e) => write!(f, "error parsing TXID: {}", e),
134-
ParseOutPointError::Vout(ref e) => write!(f, "error parsing vout: {}", e),
133+
ParseOutPointError::Txid(ref e) => write_err!(f, "error parsing TXID"; e),
134+
ParseOutPointError::Vout(ref e) => write_err!(f, "error parsing vout"; e),
135135
ParseOutPointError::Format => write!(f, "OutPoint not in <txid>:<vout> format"),
136136
ParseOutPointError::TooLong => write!(f, "vout should be at most 10 digits"),
137137
ParseOutPointError::VoutNotCanonical => write!(f, "no leading zeroes or + allowed in vout part"),

src/consensus/encode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ pub enum Error {
8686
impl fmt::Display for Error {
8787
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8888
match *self {
89-
Error::Io(ref e) => write!(f, "I/O error: {}", e),
90-
Error::Psbt(ref e) => write!(f, "PSBT error: {}", e),
89+
Error::Io(ref e) => write_err!(f, "IO error"; e),
90+
Error::Psbt(ref e) => write_err!(f, "PSBT error"; e),
9191
Error::UnexpectedNetworkMagic { expected: ref e, actual: ref a } => write!(f,
9292
"unexpected network magic: expected {}, actual {}", e, a),
9393
Error::OversizedVectorAllocation { requested: ref r, max: ref m } => write!(f,

src/internal_macros.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,3 +564,22 @@ macro_rules! user_enum {
564564
}
565565
);
566566
}
567+
568+
/// Formats error. If `std` feature is OFF appends error source (delimited by `: `). We do this
569+
/// because `e.source()` is only available in std builds, without this macro the error source is
570+
/// lost for no-std builds.
571+
macro_rules! write_err {
572+
($writer:expr, $string:literal $(, $args:expr),*; $source:expr) => {
573+
{
574+
#[cfg(feature = "std")]
575+
{
576+
let _ = &$source; // Prevents clippy warnings.
577+
write!($writer, $string $(, $args)*)
578+
}
579+
#[cfg(not(feature = "std"))]
580+
{
581+
write!($writer, concat!($string, ": {}") $(, $args)*, $source)
582+
}
583+
}
584+
}
585+
}

src/network/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub enum Error {
5858
impl fmt::Display for Error {
5959
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6060
match *self {
61-
Error::Io(ref e) => fmt::Display::fmt(e, f),
61+
Error::Io(ref e) => write_err!(f, "IO error"; e),
6262
}
6363
}
6464
}

src/util/address.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ pub enum Error {
8686
impl fmt::Display for Error {
8787
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8888
match *self {
89-
Error::Base58(_) => write!(f, "base58 address encoding error"),
90-
Error::Bech32(_) => write!(f, "bech32 address encoding error"),
89+
Error::Base58(ref e) => write_err!(f, "base58 address encoding error"; e),
90+
Error::Bech32(ref e) => write_err!(f, "bech32 address encoding error"; e),
9191
Error::EmptyBech32Payload => write!(f, "the bech32 payload was empty"),
9292
Error::InvalidBech32Variant { expected, found } => write!(f, "invalid bech32 checksum variant found {:?} when {:?} was expected", found, expected),
9393
Error::InvalidWitnessVersion(v) => write!(f, "invalid witness script version: {}", v),
94-
Error::UnparsableWitnessVersion(_) => write!(f, "incorrect format of a witness version byte"),
94+
Error::UnparsableWitnessVersion(ref e) => write_err!(f, "incorrect format of a witness version byte"; e),
9595
Error::MalformedWitnessVersion => f.write_str("bitcoin script opcode does not match any known witness version, the script is malformed"),
9696
Error::InvalidWitnessProgramLength(l) => write!(f, "the witness program must be between 2 and 40 bytes in length: length={}", l),
9797
Error::InvalidSegwitV0ProgramLength(l) => write!(f, "a v0 witness program must be either of length 20 or 32 bytes: length={}", l),

src/util/base58.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ impl fmt::Display for Error {
6060
Error::InvalidExtendedKeyVersion(ref v) => write!(f, "extended key version {:#04x?} is invalid for this base58 type", v),
6161
Error::InvalidAddressVersion(ref v) => write!(f, "address version {} is invalid for this base58 type", v),
6262
Error::TooShort(_) => write!(f, "base58ck data not even long enough for a checksum"),
63-
Error::Secp256k1(ref e) => fmt::Display::fmt(&e, f),
64-
Error::Hex(ref e) => write!(f, "Hexadecimal decoding error: {}", e)
63+
Error::Secp256k1(ref e) => write_err!(f, "secp256k1 error while parsing secret key"; e),
64+
Error::Hex(ref e) => write_err!(f, "hexadecimal decoding error"; e)
6565
}
6666
}
6767
}

src/util/bip158.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Display for Error {
7878
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
7979
match *self {
8080
Error::UtxoMissing(ref coin) => write!(f, "unresolved UTXO {}", coin),
81-
Error::Io(ref io) => write!(f, "{}", io)
81+
Error::Io(ref e) => write_err!(f, "IO error"; e),
8282
}
8383
}
8484
}

src/util/bip32.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,14 +482,14 @@ impl fmt::Display for Error {
482482
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
483483
match *self {
484484
Error::CannotDeriveFromHardenedKey => f.write_str("cannot derive hardened key from public key"),
485-
Error::Secp256k1(ref e) => fmt::Display::fmt(e, f),
485+
Error::Secp256k1(ref e) => write_err!(f, "secp256k1 error"; e),
486486
Error::InvalidChildNumber(ref n) => write!(f, "child number {} is invalid (not within [0, 2^31 - 1])", n),
487487
Error::InvalidChildNumberFormat => f.write_str("invalid child number format"),
488488
Error::InvalidDerivationPathFormat => f.write_str("invalid derivation path format"),
489489
Error::UnknownVersion(ref bytes) => write!(f, "unknown version magic bytes: {:?}", bytes),
490490
Error::WrongExtendedKeyLength(ref len) => write!(f, "encoded extended key data has wrong length {}", len),
491-
Error::Base58(ref err) => write!(f, "base58 encoding error: {}", err),
492-
Error::Hex(ref e) => write!(f, "Hexadecimal decoding error: {}", e)
491+
Error::Base58(ref e) => write_err!(f, "base58 encoding error"; e),
492+
Error::Hex(ref e) => write_err!(f, "Hexadecimal decoding error"; e),
493493
}
494494
}
495495
}

src/util/ecdsa.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ pub enum EcdsaSigError {
102102
impl fmt::Display for EcdsaSigError {
103103
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
104104
match *self {
105-
EcdsaSigError::HexEncoding(e) =>
106-
write!(f, "EcdsaSig hex encoding error: {}", e),
105+
EcdsaSigError::HexEncoding(ref e) =>
106+
write_err!(f, "EcdsaSig hex encoding error"; e),
107107
EcdsaSigError::NonStandardSighashType(hash_ty) =>
108108
write!(f, "Non standard signature hash type {}", hash_ty),
109109
EcdsaSigError::EmptySignature =>
110110
write!(f, "Empty ECDSA signature"),
111111
EcdsaSigError::Secp256k1(ref e) =>
112-
write!(f, "Invalid Ecdsa signature: {}", e),
112+
write_err!(f, "invalid ECDSA signature"; e),
113113
}
114114
}
115115
}

src/util/key.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ pub enum Error {
4545
impl fmt::Display for Error {
4646
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4747
match *self {
48-
Error::Base58(ref e) => write!(f, "Key base58 error: {}", e),
49-
Error::Secp256k1(ref e) => write!(f, "Key secp256k1 error: {}", e),
48+
Error::Base58(ref e) => write_err!(f, "key base58 error"; e),
49+
Error::Secp256k1(ref e) => write_err!(f, "key secp256k1 error"; e),
5050
Error::InvalidKeyPrefix(ref b) => write!(f, "key prefix invalid: {}", b),
51-
Error::Hex(ref e) => write!(f, "Key hex decoding error: {}", e)
51+
Error::Hex(ref e) => write_err!(f, "key hex decoding error"; e)
5252
}
5353
}
5454
}

0 commit comments

Comments
 (0)