diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2020-01-28 10:07:23 +0100 |
---|---|---|
committer | Robin Krahl <robin.krahl@ireas.org> | 2020-01-28 11:54:20 +0100 |
commit | da8727996efacec4280696caefee3feecea4eae7 (patch) | |
tree | 82f410d4009b698b630a90bcfa6255c07d743a33 /src/util.rs | |
parent | 47795368c32c0c47a9c3da761c3adf0a36ca419f (diff) | |
download | nitrokey-rs-da8727996efacec4280696caefee3feecea4eae7.tar.gz nitrokey-rs-da8727996efacec4280696caefee3feecea4eae7.tar.bz2 |
Add String value to the Error::UnexpectedError variant
To make debugging of unexpected errors easier, this patch adds an
associated String value with a description of the unexpected behavior to
the UnexpectedError variant of the Error enum.
Diffstat (limited to 'src/util.rs')
-rw-r--r-- | src/util.rs | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/util.rs b/src/util.rs index 5a56c55..1b52c3d 100644 --- a/src/util.rs +++ b/src/util.rs @@ -39,7 +39,9 @@ pub fn owned_str_from_ptr(ptr: *const c_char) -> Result<String, Error> { pub fn result_from_string(ptr: *const c_char) -> Result<String, Error> { if ptr.is_null() { - return Err(Error::UnexpectedError); + return Err(Error::UnexpectedError( + "libnitrokey returned a null pointer".to_owned(), + )); } let s = owned_str_from_ptr(ptr)?; unsafe { free(ptr as *mut c_void) }; @@ -69,10 +71,9 @@ pub fn get_last_result() -> Result<(), Error> { } pub fn get_last_error() -> Error { - match get_last_result() { - Ok(()) => Error::UnexpectedError, - Err(err) => err, - } + get_last_result().err().unwrap_or_else(|| { + Error::UnexpectedError("Expected an error, but command status is zero".to_owned()) + }) } pub fn generate_password(length: usize) -> Result<Vec<u8>, Error> { |