aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Mueller <deso@posteo.net>2020-09-29 20:31:50 -0700
committerDaniel Mueller <deso@posteo.net>2020-09-29 20:31:50 -0700
commit1229dd36306ac43480fcf2bfb91aa47d41ea5e77 (patch)
tree7539a02e3c292358b541d762955e79c81ff23716
parente5149d8a783fc8e397e91415c2e56901f7368349 (diff)
downloadnitrocli-1229dd36306ac43480fcf2bfb91aa47d41ea5e77.tar.gz
nitrocli-1229dd36306ac43480fcf2bfb91aa47d41ea5e77.tar.bz2
Shorten some error handling paths
When we originally switched over to using anyhow for error handling, we evidently missed to take advantage of its context support in a couple of error paths. The result was that we ended up with rather long winded > result.ok_or_else(|| anyhow::anyhow!(...)) constructs. This change shortens them, making use of the anyhow::Context trait.
-rw-r--r--src/commands.rs12
1 files changed, 6 insertions, 6 deletions
diff --git a/src/commands.rs b/src/commands.rs
index 9685050..51ac32f 100644
--- a/src/commands.rs
+++ b/src/commands.rs
@@ -308,7 +308,7 @@ where
if let Some(pin) = pin {
let pin = pin
.to_str()
- .ok_or_else(|| anyhow::anyhow!("Failed to read PIN: Invalid Unicode data found"))?;
+ .context("Failed to read PIN: Invalid Unicode data found")?;
op(ctx, data, &pin).map_err(|(_, err)| err)
} else {
try_with_pin_and_data_with_pinentry(ctx, pin_entry, data, op)
@@ -605,7 +605,7 @@ pub fn hidden_create(ctx: &mut Context<'_>, slot: u8, start: u8, end: u8) -> any
let pwd = if let Some(pwd) = &ctx.password {
pwd
.to_str()
- .ok_or_else(|| anyhow::anyhow!("Failed to read password: invalid unicode data found"))
+ .context("Failed to read password: Invalid Unicode data found")
.map(ToOwned::to_owned)
} else {
pinentry::choose(ctx, &pwd_entry).context("Failed to select new PIN")
@@ -624,7 +624,7 @@ pub fn hidden_open(ctx: &mut Context<'_>) -> anyhow::Result<()> {
let pwd = if let Some(pwd) = &ctx.password {
pwd
.to_str()
- .ok_or_else(|| anyhow::anyhow!("Failed to read password: Invalid Unicode data found"))
+ .context("Failed to read password: Invalid Unicode data found")
.map(ToOwned::to_owned)
} else {
pinentry::inquire(ctx, &pwd_entry, pinentry::Mode::Query, None)
@@ -797,7 +797,7 @@ fn prepare_ascii_secret(secret: &str) -> anyhow::Result<String> {
fn prepare_base32_secret(secret: &str) -> anyhow::Result<String> {
base32::decode(base32::Alphabet::RFC4648 { padding: false }, secret)
.map(|vec| format_bytes(&vec))
- .ok_or_else(|| anyhow::anyhow!("Failed to parse base32 secret"))
+ .context("Failed to parse base32 secret")
}
/// Configure a one-time password slot on the Nitrokey device.
@@ -870,7 +870,7 @@ fn print_otp_status(
};
slot = slot
.checked_add(1)
- .ok_or_else(|| anyhow::anyhow!("Encountered integer overflow when iterating OTP slots"))?;
+ .context("Encountered integer overflow when iterating OTP slots")?;
let name = match result {
Ok(name) => name,
Err(nitrokey::Error::LibraryError(nitrokey::LibraryError::InvalidSlot)) => return Ok(()),
@@ -937,7 +937,7 @@ fn choose_pin(
if let Some(new_pin) = new_pin {
new_pin
.to_str()
- .ok_or_else(|| anyhow::anyhow!("Failed to read PIN: Invalid Unicode data found"))
+ .context("Failed to read PIN: Invalid Unicode data found")
.map(ToOwned::to_owned)
} else {
pinentry::choose(ctx, pin_entry).context("Failed to select PIN")