improve error handling in cli (#7436)

* other error variant should carry a dyn Error

* introduce thiserror for error derive, add explicit error variants, cleanup dependencies

* cleanup handle dev-deps of sc-cli too

* Update client/cli/src/error.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

Co-authored-by: Bernhard Schuster <bernhard@parity.io>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Bernhard Schuster
2020-10-27 18:50:04 +01:00
committed by GitHub
parent 3ac070b61e
commit f373ecbcf0
8 changed files with 82 additions and 84 deletions
+8 -10
View File
@@ -77,27 +77,25 @@ fn verify<Pair>(sig_data: Vec<u8>, message: Vec<u8>, uri: &str) -> error::Result
{
let mut signature = Pair::Signature::default();
if sig_data.len() != signature.as_ref().len() {
return Err(error::Error::Other(format!(
"signature has an invalid length. read {} bytes, expected {} bytes",
sig_data.len(),
signature.as_ref().len(),
)));
return Err(
error::Error::SignatureInvalidLength {
read: sig_data.len(),
expected: signature.as_ref().len(),
}
);
}
signature.as_mut().copy_from_slice(&sig_data);
let pubkey = if let Ok(pubkey_vec) = hex::decode(uri) {
Pair::Public::from_slice(pubkey_vec.as_slice())
} else {
Pair::Public::from_string(uri)
.map_err(|_| {
error::Error::Other(format!("Invalid URI; expecting either a secret URI or a public URI."))
})?
Pair::Public::from_string(uri)?
};
if Pair::verify(&signature, &message, &pubkey) {
println!("Signature verifies correctly.");
} else {
return Err(error::Error::Other("Signature invalid.".into()))
return Err(error::Error::SignatureInvalid)
}
Ok(())