Skip to content

Commit 6577fba

Browse files
committed
Merge #446: Introduce write_err macro
946cd83 Improve Error display (Tobin C. Harding) Pull request description: Introduce `write_err` macro and improve the `Display` implementation on `Error`. This PR is re-written after review below. The `std::error::Error` implementation is removed in favour of #409. Now only includes the `Display` stuff. ACKs for top commit: apoelstra: ACK 946cd83 nice! Tree-SHA512: a62e9593c2ed1ba6136136e6b575219d68c25736069f55448f8411048efae27f2467bcb65260a78ff065de9c8c2994873804c687fb37a55b705cddfe20544f37
2 parents aab77b1 + 946cd83 commit 6577fba

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

src/lib.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -349,28 +349,23 @@ pub enum Error {
349349
InvalidParityValue(key::InvalidParityValue),
350350
}
351351

352-
impl Error {
353-
fn as_str(&self) -> &str {
354-
match *self {
355-
Error::IncorrectSignature => "secp: signature failed verification",
356-
Error::InvalidMessage => "secp: message was not 32 bytes (do you need to hash?)",
357-
Error::InvalidPublicKey => "secp: malformed public key",
358-
Error::InvalidSignature => "secp: malformed signature",
359-
Error::InvalidSecretKey => "secp: malformed or out-of-range secret key",
360-
Error::InvalidSharedSecret => "secp: malformed or out-of-range shared secret",
361-
Error::InvalidRecoveryId => "secp: bad recovery id",
362-
Error::InvalidTweak => "secp: bad tweak",
363-
Error::NotEnoughMemory => "secp: not enough memory allocated",
364-
Error::InvalidPublicKeySum => "secp: the sum of public keys was invalid or the input vector lengths was less than 1",
365-
Error::InvalidParityValue(_) => "couldn't create parity",
366-
}
367-
}
368-
}
369-
370-
// Passthrough Debug to Display, since errors should be user-visible.
371352
impl fmt::Display for Error {
372353
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
373-
f.write_str(self.as_str())
354+
use Error::*;
355+
356+
match *self {
357+
IncorrectSignature => f.write_str("signature failed verification"),
358+
InvalidMessage => f.write_str("message was not 32 bytes (do you need to hash?)"),
359+
InvalidPublicKey => f.write_str("malformed public key"),
360+
InvalidSignature => f.write_str("malformed signature"),
361+
InvalidSecretKey => f.write_str("malformed or out-of-range secret key"),
362+
InvalidSharedSecret => f.write_str("malformed or out-of-range shared secret"),
363+
InvalidRecoveryId => f.write_str("bad recovery id"),
364+
InvalidTweak => f.write_str("bad tweak"),
365+
NotEnoughMemory => f.write_str("not enough memory allocated"),
366+
InvalidPublicKeySum => f.write_str("the sum of public keys was invalid or the input vector lengths was less than 1"),
367+
InvalidParityValue(e) => write_err!(f, "couldn't create parity"; e),
368+
}
374369
}
375370
}
376371

src/macros.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,22 @@ macro_rules! impl_pretty_debug {
2626
}
2727
}
2828
}
29+
30+
/// Formats error. If `std` feature is OFF appends error source (delimited by `: `). We do this
31+
/// because `e.source()` is only available in std builds, without this macro the error source is
32+
/// lost for no-std builds.
33+
macro_rules! write_err {
34+
($writer:expr, $string:literal $(, $args:expr),*; $source:expr) => {
35+
{
36+
#[cfg(feature = "std")]
37+
{
38+
let _ = &$source; // Prevents clippy warnings.
39+
write!($writer, $string $(, $args)*)
40+
}
41+
#[cfg(not(feature = "std"))]
42+
{
43+
write!($writer, concat!($string, ": {}") $(, $args)*, $source)
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)