1 unstable release

0.1.1 Jan 8, 2021
0.1.0 Jan 8, 2021

#18 in #panics

Download history 3/week @ 2024-02-15 9/week @ 2024-02-22 2/week @ 2024-02-29 1/week @ 2024-03-14 46/week @ 2024-03-21 46/week @ 2024-03-28

93 downloads per month

MIT/Apache

10KB
93 lines

Utilities for reporting fatal errors and exiting with an error code.

The behavior in this crate is different than the one in panic!-based exits, in that the ones here are suited for display to end-users, i.e. no "thread main panicked at", no backtrace mentions, etc.

Usage

For unwrapping Results:

For aborting:

(Pseudo-)Example:

use fatal::UnwrapExt;

const DB_CONSTR_VAR: &str = "DB_CONNECTION_STRING";

fn main() {
    println!("Connecting..");
    let constr: String = fatal::unwrap_message!(std::env::var(DB_CONSTR_VAR), "failed to read the `{}` environment variable", DB_CONSTR_VAR);
    // when doesn't exist, will print: "Error: failed to read the `DB_CONNECTION_STRING` environment variable (environment variable not found)"
    let db: Database = Database::connect(&constr).expect_fatal("failed to connect to database");
    // would also include the actual error as above.

    println!("Querying total users..");
    println!("Total users: {}", db.query_total_users().unwrap_fatal());
}

Dependencies

~190KB