#error-message #exit #panic #expect #terminate

bin+lib expect-exit

Result.expected(): display an error message and exit without a panic

12 releases

0.5.3 Oct 11, 2024
0.5.2 Oct 10, 2022
0.5.1 Aug 21, 2022
0.4.3 Jun 4, 2022
0.2.0 Nov 13, 2020

#282 in Rust patterns

Download history 16/week @ 2024-07-27 64/week @ 2024-08-03 150/week @ 2024-08-10 60/week @ 2024-08-17 90/week @ 2024-08-24 197/week @ 2024-08-31 113/week @ 2024-09-07 46/week @ 2024-09-14 118/week @ 2024-09-21 172/week @ 2024-09-28 364/week @ 2024-10-05 130/week @ 2024-10-12 51/week @ 2024-10-19 54/week @ 2024-10-26 46/week @ 2024-11-02 7/week @ 2024-11-09

172 downloads per month
Used in 3 crates (2 directly)

BSD-2-Clause

42KB
1K SLoC

expect-exit: display an error message and exit without a panic

Note: This library is deprecated, and it is in maintenance mode. The anyhow library may be a better choice for both new and existing programs.

The expect-exit library defines the Expected, ExpectedWithError, and ExpectedResult traits and implements them for the standard Result and Option types as appropriate. This allows a program to display an error message and exit with a non-zero exit code without invoking a Rust panic, yet optionally unwinding the stack so that various objects may perform some clean-up actions.

The methods with an _e suffix append an appropriate error message to the supplied one. The methods with a _ suffix allow the caller to specify an already-constructed message instead of a function that returns it.

Example

use expect_exit::{Expected, ExpectedResult};

{
    env::var(name).or_exit(|| format!("{} not specified in the environment", name))

    fs::read_to_string(path).or_exit_e(|| format!("Could not read {:?}", path))

    tx.send(result).await.or_exit_e_("Could not tell the main thread");

    let config = parse().expect_result_("Could not parse the config")?;
    Ok(config.value + 1)
}

For the crate's change history, see the NEWS.md file in the source distribution.

Author: Peter Pentchev <roam@ringlet.net>

No runtime deps