aboutsummaryrefslogtreecommitdiff
path: root/nitrocli/src/error.rs
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2019-01-09 11:16:03 -0800
committerDaniel Mueller <deso@posteo.net>2019-01-09 11:16:03 -0800
commit3a99e7417e4c4eb30df9e711077c89b75764c029 (patch)
treebd56330e4809a9a1a7ed677c26a1b4805b31d30d /nitrocli/src/error.rs
parent6e0efd98e9ee07a01241e2187b4c820770c3c478 (diff)
downloadnitrocli-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/error.rs')
-rw-r--r--nitrocli/src/error.rs11
1 files changed, 8 insertions, 3 deletions
diff --git a/nitrocli/src/error.rs b/nitrocli/src/error.rs
index ffcc56b..78b8148 100644
--- a/nitrocli/src/error.rs
+++ b/nitrocli/src/error.rs
@@ -25,7 +25,7 @@ use std::string;
#[derive(Debug)]
pub enum Error {
ArgparseError(i32),
- CommandError(nitrokey::CommandError),
+ CommandError(Option<&'static str>, nitrokey::CommandError),
IoError(io::Error),
Utf8Error(str::Utf8Error),
Error(String),
@@ -33,7 +33,7 @@ pub enum Error {
impl From<nitrokey::CommandError> for Error {
fn from(e: nitrokey::CommandError) -> Error {
- Error::CommandError(e)
+ Error::CommandError(None, e)
}
}
@@ -59,7 +59,12 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Error::ArgparseError(_) => write!(f, "Could not parse arguments"),
- Error::CommandError(ref e) => write!(f, "Command error: {}", e),
+ Error::CommandError(ref ctx, ref e) => {
+ if let Some(ctx) = ctx {
+ write!(f, "{}: ", ctx)?;
+ }
+ write!(f, "{}", e)
+ }
Error::Utf8Error(_) => write!(f, "Encountered UTF-8 conversion error"),
Error::IoError(ref e) => write!(f, "IO error: {}", e),
Error::Error(ref e) => write!(f, "{}", e),