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
commit1af85ad50523427384234313fe00aa8bfa89135f (patch)
tree129af98f7e3e352a6d230b3a0e0c8aa429429c37
parent4d779638826bee5e4ee5c7ea2ed2fc8d0d7275be (diff)
downloadnitrocli-1af85ad50523427384234313fe00aa8bfa89135f.tar.gz
nitrocli-1af85ad50523427384234313fe00aa8bfa89135f.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 ebb28ca..64da866 100644
--- a/src/commands.rs
+++ b/src/commands.rs
@@ -306,7 +306,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)
@@ -552,7 +552,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")
@@ -571,7 +571,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)
@@ -744,7 +744,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.
@@ -817,7 +817,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(()),
@@ -884,7 +884,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")