Skip to content

refactor(sns-cli): Use anyhow - #1120

Merged
anchpop merged 1 commit into
masterfrom
@anchpop/use-anyhow-in-sns-cli
Aug 24, 2024
Merged

anchpop merged 1 commit into
masterfrom
@anchpop/use-anyhow-in-sns-cli

Conversation

@anchpop

@anchpop anchpop commented Aug 23, 2024 •

Copy link
Copy Markdown
Contributor

Problem

It is generally not a very good practice to perform an .unwrap() that might plausibly fail, outside of tests. Instead, it is better to return an error. Even in the main function, you can return a Result instead of using .unwrap() or exit(1) (and returning an Err from the main function will automatically set a non-zero exit code). Using exit(1) is particularly dangerous as you may forget to call it, or forget to use eprintln! instead of println!, in both cases doing something somewhat unexpected that the compiler will not warn you about.

Solution

Convert all the sns-cli endpoints to return an anyhow::Result instead of (). This enables them to return errors instead of panicking.

Impoved code

Example 1

Using anyhow makes some of our code a bit more concise as well. For example, compare the original:

let sns_init_payload = args.generate_sns_init_payload().unwrap_or_else(|err| {
    eprintln!(
        "Error encountered when generating the SnsInitPayload: {}",
        err
    );
    exit(1);
});

with:

let sns_init_payload = args
    .generate_sns_init_payload()
    .context("Generating the SnsInitPayload")?;

Which is not exactly equivalent, but the latter returns a (imho) nicer error message. For the unfamiliar,

use anyhow::{anyhow, context};
fn main() {
    return Err(anyhow!("What went wrong.")).context("Error situation");
}

outputs:

Error: Error situation

Caused by:
    What went wrong.

Using .context() gives us something that's almost like a human-readable backtrace:

use anyhow::{anyhow, Context};
fn main() {
    return Err(anyhow!("What went wrong."))
        .context("I think something is about to go wrong")
        .context("Error situation");
}

outputs:

Error: Error situation

Caused by:
    0: I think something is about to go wrong
    1: What went wrong.

Example 2

if let Some(save_to) = &save_to {
    if let Err(err) = ensure_file_exists_and_is_writeable(save_to.as_path()) {
        eprintln!("{}", err);
        std::process::exit(1);
    }
}

Becomes

if let Some(save_to) = &save_to {
    ensure_file_exists_and_is_writeable(save_to.as_path())?
}

Additional notes

I have not actually removed all the occurrences of .unwrap() yet, but I have updated all the cases where we called exit(1).

@anchpop
anchpop requested a review from a team as a code owner August 23, 2024 20:38
@anchpop
anchpop force-pushed the @anchpop/use-anyhow-in-sns-cli branch from 4f0a33a to 7b7fd35 Compare August 23, 2024 20:44
@anchpop
anchpop force-pushed the @anchpop/use-anyhow-in-sns-cli branch 3 times, most recently from f4a1b78 to 9b56cd0 Compare August 23, 2024 21:16
@anchpop
anchpop force-pushed the @anchpop/use-anyhow-in-sns-cli branch from 9b56cd0 to 8a1535c Compare August 23, 2024 21:53
@anchpop
anchpop added this pull request to the merge queue Aug 24, 2024
Merged via the queue into master with commit ba576d1 Aug 24, 2024
@anchpop
anchpop deleted the @anchpop/use-anyhow-in-sns-cli branch August 24, 2024 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants