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
+5 -6
View File
@@ -62,7 +62,7 @@ impl InsertCmd {
pub fn run(&self) -> Result<(), Error> {
let suri = utils::read_uri(self.suri.as_ref())?;
let base_path = self.shared_params.base_path.as_ref()
.ok_or_else(|| Error::Other("please supply base path".into()))?;
.ok_or_else(|| Error::MissingBasePath)?;
let (keystore, public) = match self.keystore_params.keystore_config(base_path)? {
KeystoreConfig::Path { path, password } => {
@@ -70,20 +70,19 @@ impl InsertCmd {
self.crypto_scheme.scheme,
to_vec(&suri, password.clone())
)?;
let keystore: SyncCryptoStorePtr = Arc::new(LocalKeystore::open(path, password)
.map_err(|e| format!("{}", e))?);
let keystore: SyncCryptoStorePtr = Arc::new(LocalKeystore::open(path, password)?);
(keystore, public)
},
_ => unreachable!("keystore_config always returns path and password; qed")
};
let key_type = KeyTypeId::try_from(self.key_type.as_str())
.map_err(|_| {
Error::Other("Cannot convert argument to keytype: argument should be 4-character string".into())
.map_err(|_e| {
Error::KeyTypeInvalid
})?;
SyncCryptoStore::insert_unknown(&*keystore, key_type, &suri, &public[..])
.map_err(|e| Error::Other(format!("{:?}", e)))?;
.map_err(|_| Error::KeyStoreOperation)?;
Ok(())
}
+1 -2
View File
@@ -246,8 +246,7 @@ pub fn decode_hex<T: AsRef<[u8]>>(message: T) -> Result<Vec<u8>, Error> {
if message[..2] == [b'0', b'x'] {
message = &message[2..]
}
hex::decode(message)
.map_err(|e| Error::Other(format!("Invalid hex ({})", e)))
Ok(hex::decode(message)?)
}
/// checks if message is Some, otherwise reads message from stdin and optionally decodes hex
+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(())