From 3a99e7417e4c4eb30df9e711077c89b75764c029 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Wed, 9 Jan 2019 11:16:03 -0800 Subject: 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. --- nitrocli/src/commands.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'nitrocli/src/commands.rs') 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)), }, -- cgit v1.2.1