Skip to content

Commit

Permalink
Merge #446: Introduce write_err macro
Browse files Browse the repository at this point in the history
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
  • Loading branch information
apoelstra committed Jun 15, 2022
2 parents aab77b1 + 946cd83 commit 6577fba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
35 changes: 15 additions & 20 deletions src/lib.rs
Expand Up @@ -349,28 +349,23 @@ pub enum Error {
InvalidParityValue(key::InvalidParityValue),
}

impl Error {
fn as_str(&self) -> &str {
match *self {
Error::IncorrectSignature => "secp: signature failed verification",
Error::InvalidMessage => "secp: message was not 32 bytes (do you need to hash?)",
Error::InvalidPublicKey => "secp: malformed public key",
Error::InvalidSignature => "secp: malformed signature",
Error::InvalidSecretKey => "secp: malformed or out-of-range secret key",
Error::InvalidSharedSecret => "secp: malformed or out-of-range shared secret",
Error::InvalidRecoveryId => "secp: bad recovery id",
Error::InvalidTweak => "secp: bad tweak",
Error::NotEnoughMemory => "secp: not enough memory allocated",
Error::InvalidPublicKeySum => "secp: the sum of public keys was invalid or the input vector lengths was less than 1",
Error::InvalidParityValue(_) => "couldn't create parity",
}
}
}

// Passthrough Debug to Display, since errors should be user-visible.
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str(self.as_str())
use Error::*;

match *self {
IncorrectSignature => f.write_str("signature failed verification"),
InvalidMessage => f.write_str("message was not 32 bytes (do you need to hash?)"),
InvalidPublicKey => f.write_str("malformed public key"),
InvalidSignature => f.write_str("malformed signature"),
InvalidSecretKey => f.write_str("malformed or out-of-range secret key"),
InvalidSharedSecret => f.write_str("malformed or out-of-range shared secret"),
InvalidRecoveryId => f.write_str("bad recovery id"),
InvalidTweak => f.write_str("bad tweak"),
NotEnoughMemory => f.write_str("not enough memory allocated"),
InvalidPublicKeySum => f.write_str("the sum of public keys was invalid or the input vector lengths was less than 1"),
InvalidParityValue(e) => write_err!(f, "couldn't create parity"; e),
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/macros.rs
Expand Up @@ -26,3 +26,22 @@ macro_rules! impl_pretty_debug {
}
}
}

/// Formats error. If `std` feature is OFF appends error source (delimited by `: `). We do this
/// because `e.source()` is only available in std builds, without this macro the error source is
/// lost for no-std builds.
macro_rules! write_err {
($writer:expr, $string:literal $(, $args:expr),*; $source:expr) => {
{
#[cfg(feature = "std")]
{
let _ = &$source; // Prevents clippy warnings.
write!($writer, $string $(, $args)*)
}
#[cfg(not(feature = "std"))]
{
write!($writer, concat!($string, ": {}") $(, $args)*, $source)
}
}
}
}

0 comments on commit 6577fba

Please sign in to comment.