diff options
author | Daniel Mueller <deso@posteo.net> | 2019-01-09 11:16:03 -0800 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2019-01-09 11:16:03 -0800 |
commit | 3a99e7417e4c4eb30df9e711077c89b75764c029 (patch) | |
tree | bd56330e4809a9a1a7ed677c26a1b4805b31d30d /nitrocli/src/commands.rs | |
parent | 6e0efd98e9ee07a01241e2187b4c820770c3c478 (diff) | |
download | nitrocli-3a99e7417e4c4eb30df9e711077c89b75764c029.tar.gz nitrocli-3a99e7417e4c4eb30df9e711077c89b75764c029.tar.bz2 |
Report command errors properly
So far we have taken all nitrokey::CommandError objects and put them in
formatted form into the Error::Error variant. What we really should do,
though, is to preserve the original error, with the additional context
provided by the caller, and report that up the stack directly. Doing so
has at least the benefit that we are able to check for expected errors
without hard coding the textual representation as maintained by the
nitrokey create.
This change refactors the code accordingly and adds two tests for such
expected error codes.
Diffstat (limited to 'nitrocli/src/commands.rs')
-rw-r--r-- | nitrocli/src/commands.rs | 7 |
1 files changed, 3 insertions, 4 deletions
diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index f8765cc..ed3c2c4 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -34,8 +34,8 @@ use crate::pinentry; use crate::Result; /// Create an `error::Error` with an error message of the format `msg: err`. -fn get_error(msg: &str, err: nitrokey::CommandError) -> Error { - Error::Error(format!("{}: {}", msg, err)) +fn get_error(msg: &'static str, err: nitrokey::CommandError) -> Error { + Error::CommandError(Some(msg), err) } /// Set `libnitrokey`'s log level based on the execution context's verbosity. @@ -201,8 +201,7 @@ where data = new_data; continue; } - let error = format!("{}: Wrong password", msg); - return Err(Error::Error(error)); + return Err(get_error(msg, err)); } err => return Err(get_error(msg, err)), }, |