From e9a89be939acf22cf17e3d6bc505a968a98d3dee Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 30 Aug 2020 18:43:31 +0200 Subject: Remove custom source implementation for Error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch removes the custom implementation of the source method of the std::error::Error trait for the error::Error type. This means that the default implementation is used that always returns None. The reason for this change is that we already print the error message of the source error in the Display implementation. This leads to a duplicated error message if both Display and source are checked, for example with anyhow’s error formatting. See this thread for more information: https://lists.sr.ht/~ireas/nitrokey-rs-dev/%3C6e0b4dc8-9059-a113-e98e-b49e52818c75%40posteo.net%3E --- CHANGELOG.md | 4 ++++ src/error.rs | 16 +--------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 736620d..e89fc44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ Copyright (C) 2019-2020 Robin Krahl SPDX-License-Identifier: CC0-1.0 --> +# Unreleased +- Remove the custom `std::error::Error::source` implementation for + `error::Error` to avoid duplicate error messages. + # v0.7.0 (2020-07-14) - Refactor the `Error` enum so that it is `Send`, `Sync` and `'static`: - Remove the `sync::PoisonError` from the `PoisonError` variant. diff --git a/src/error.rs b/src/error.rs index 1aa1793..64a2ce0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -91,21 +91,7 @@ impl<'a, T: device::Device<'a>> From<(T, Error)> for Error { } } -impl error::Error for Error { - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - match *self { - Error::CommandError(ref err) => Some(err), - Error::CommunicationError(ref err) => Some(err), - Error::ConcurrentAccessError => None, - Error::LibraryError(ref err) => Some(err), - Error::PoisonError => None, - Error::UnexpectedError(_) => None, - Error::UnknownError(_) => None, - Error::UnsupportedModelError => None, - Error::Utf8Error(ref err) => Some(err), - } - } -} +impl error::Error for Error {} impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { -- cgit v1.2.3 From b2607cbd3f239be9d09204921db278424cc7a6be Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 30 Aug 2020 18:52:15 +0200 Subject: Use map_err(|_| x) instead of or_else(|_| Err(x)) --- src/util.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.rs b/src/util.rs index b17b071..26942cf 100644 --- a/src/util.rs +++ b/src/util.rs @@ -99,7 +99,7 @@ pub fn generate_password(length: usize) -> Result { } pub fn get_cstring>>(s: T) -> Result { - CString::new(s).or_else(|_| Err(LibraryError::InvalidString.into())) + CString::new(s).map_err(|_| LibraryError::InvalidString.into()) } impl Into for LogLevel { -- cgit v1.2.3 From 1d36e2e1195fc9046e36f38dfe943198027e84ca Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sun, 30 Aug 2020 19:02:36 +0200 Subject: Release v0.7.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch release contains a minor change to the Error enum so that error handling frameworks like anyhow don’t produce redundant error messages. --- CHANGELOG.md | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e89fc44..121d0a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ Copyright (C) 2019-2020 Robin Krahl SPDX-License-Identifier: CC0-1.0 --> -# Unreleased +# v0.7.1 (2020-08-30) - Remove the custom `std::error::Error::source` implementation for `error::Error` to avoid duplicate error messages. diff --git a/Cargo.toml b/Cargo.toml index 00c41ce..6eb52eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ [package] name = "nitrokey" -version = "0.7.0" +version = "0.7.1" authors = ["Robin Krahl "] edition = "2018" homepage = "https://code.ireas.org/nitrokey-rs/" -- cgit v1.2.3