From 6c9d9d9c36b5cc34b84e5472b0a30b6a6dba2597 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 18 May 2023 15:15:02 +1000 Subject: [PATCH] Improve error display imlps Improve the `Error` `Display` impls by doing: - Be more terse by importing the error enum's variants. - Do not use capital letters for error messages. --- bitcoin/src/crypto/ecdsa.rs | 12 +++++++----- bitcoin/src/crypto/key.rs | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/bitcoin/src/crypto/ecdsa.rs b/bitcoin/src/crypto/ecdsa.rs index 1bdb347d..1b0c3c69 100644 --- a/bitcoin/src/crypto/ecdsa.rs +++ b/bitcoin/src/crypto/ecdsa.rs @@ -199,12 +199,14 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use Error::*; + match *self { - Error::Hex(ref e) => write_err!(f, "Signature hex decoding error"; e), - Error::NonStandardSighashType(hash_ty) => - write!(f, "Non standard signature hash type {}", hash_ty), - Error::EmptySignature => write!(f, "Empty ECDSA signature"), - Error::Secp256k1(ref e) => write_err!(f, "invalid ECDSA signature"; e), + Hex(ref e) => write_err!(f, "signature hex decoding error"; e), + NonStandardSighashType(hash_ty) => + write!(f, "non-standard signature hash type {}", hash_ty), + EmptySignature => write!(f, "empty ECDSA signature"), + Secp256k1(ref e) => write_err!(f, "invalid ECDSA signature"; e), } } } diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index e870592c..dbe6e97d 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -41,12 +41,14 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use Error::*; + match *self { - Error::Base58(ref e) => write_err!(f, "key base58 error"; e), - Error::Secp256k1(ref e) => write_err!(f, "key secp256k1 error"; e), - Error::InvalidKeyPrefix(ref b) => write!(f, "key prefix invalid: {}", b), - Error::Hex(ref e) => write_err!(f, "key hex decoding error"; e), - Error::InvalidHexLength(got) => + Base58(ref e) => write_err!(f, "key base58 error"; e), + Secp256k1(ref e) => write_err!(f, "key secp256k1 error"; e), + InvalidKeyPrefix(ref b) => write!(f, "key prefix invalid: {}", b), + Hex(ref e) => write_err!(f, "key hex decoding error"; e), + InvalidHexLength(got) => write!(f, "PublicKey hex should be 66 or 130 digits long, got: {}", got), } }