Skip to content

Commit

Permalink
Use anyhow for error handling
Browse files Browse the repository at this point in the history
This patch changes our error handling approach from the ground up:
instead of having a globally used Error enum that contains variants for
all possible errors, we now use anyhow's Error type. This approach is
more dynamic (and not statically typed), but it allows for more fine
grained error messages and overall more user-friendly error reporting.
Overall it also is a net simplification. While we have one dynamic cast
now, in order to be able to handle erroneous password/PIN entries
correctly, that is considered a reasonable compromise.
  • Loading branch information
d-e-s-o committed Aug 29, 2020
1 parent 1134445 commit b605614
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 240 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased
----------
- Changed default OTP format from `hex` to `base32`
- Improved error reporting format and fidelity
- Added `anyhow` dependency in version `1.0.32`
- Updated minimum required Rust version to `1.42.0`
- Bumped `nitrokey` dependency to `0.7.1`

Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ lto = true
codegen-units = 1
incremental = false

[dependencies.anyhow]
version = "1.0"

[dependencies.base32]
version = "0.4.0"

Expand Down
2 changes: 1 addition & 1 deletion src/arg_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ macro_rules! Command {
pub fn execute(
self,
ctx: &mut crate::ExecCtx<'_>,
) -> crate::Result<()> {
) -> anyhow::Result<()> {
match self {
$(
$name::$var$((tr!(args, $inner)))? => $exec(ctx $(,tr!(args, $inner))?),
Expand Down
16 changes: 7 additions & 9 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,14 @@ pub enum ConfigOption<T> {
}

impl<T> ConfigOption<T> {
pub fn try_from(disable: bool, value: Option<T>, name: &'static str) -> Result<Self, String> {
pub fn try_from(disable: bool, value: Option<T>, name: &'static str) -> anyhow::Result<Self> {
if disable {
if value.is_some() {
Err(format!(
"--{name} and --no-{name} are mutually exclusive",
name = name
))
} else {
Ok(ConfigOption::Disable)
}
anyhow::ensure!(
value.is_none(),
"--{name} and --no-{name} are mutually exclusive",
name = name
);
Ok(ConfigOption::Disable)
} else {
match value {
Some(value) => Ok(ConfigOption::Enable(value)),
Expand Down
Loading

0 comments on commit b605614

Please sign in to comment.