Migrate custom error trait impls to thiserror (#1856)

* Migrate to thiserror

* missing bits

* review comment

* Apply suggestions from code review

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* From<scale_decode::visitor::Error> to remove Into::intos

* scale crates for core::error::Error

* bump msrv 1.81

* make signer crate compile

---------

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
Co-authored-by: James Wilson <james.wilson@parity.io>
This commit is contained in:
Pavlo Khrystenko
2024-11-18 10:39:14 +01:00
committed by GitHub
parent 137701757e
commit 7d1002192e
17 changed files with 329 additions and 477 deletions
+17 -15
View File
@@ -7,11 +7,13 @@ use codec::Encode;
use polkadot_sdk::sp_crypto_hashing;
use crate::crypto::{seed_from_entropy, DeriveJunction, SecretUri};
use core::{fmt::Display, str::FromStr};
use core::str::FromStr;
use hex::FromHex;
use secp256k1::{ecdsa::RecoverableSignature, Message, Secp256k1, SecretKey};
use secrecy::ExposeSecret;
use thiserror::Error as DeriveError;
const SECRET_KEY_LENGTH: usize = 32;
/// Seed bytes used to generate a key pair.
@@ -222,33 +224,33 @@ pub(crate) mod internal {
}
/// An error handed back if creating a keypair fails.
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, DeriveError)]
pub enum Error {
/// Invalid seed.
#[error("Invalid seed (was it the wrong length?)")]
InvalidSeed,
/// Invalid seed.
#[error("Invalid seed for ECDSA, contained soft junction")]
SoftJunction,
/// Invalid phrase.
#[error("Cannot parse phrase: {0}")]
Phrase(bip39::Error),
/// Invalid hex.
#[error("Cannot parse hex string: {0}")]
Hex(hex::FromHexError),
}
impl Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::InvalidSeed => write!(f, "Invalid seed (was it the wrong length?)"),
Error::SoftJunction => write!(f, "Invalid seed for ECDSA, contained soft junction"),
Error::Phrase(e) => write!(f, "Cannot parse phrase: {e}"),
Error::Hex(e) => write!(f, "Cannot parse hex string: {e}"),
}
impl From<hex::FromHexError> for Error {
fn from(err: hex::FromHexError) -> Self {
Error::Hex(err)
}
}
impl_from!(bip39::Error => Error::Phrase);
impl_from!(hex::FromHexError => Error::Hex);
#[cfg(feature = "std")]
impl std::error::Error for Error {}
impl From<bip39::Error> for Error {
fn from(err: bip39::Error) -> Self {
Error::Phrase(err)
}
}
/// Dev accounts, helpful for testing but not to be used in production,
/// since the secret keys are known.