Expand description
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 Result
s:
- Use
expect
/expect_fatal
to report the error with context. - Use
unwrap_message!
toexpect
with formatting. - Use
unwrap_format!
to have more control over the message’s format. - Use
unwrap
/unwrap_fatal
to report the error when context is provided/obvious.
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());
}
Macros§
- Prints an error message to standard-error and exits with an error code.
- Prints to standard-error and exits with an error-code. Returns
!
. - Unwraps the result or formats an error message and exits.
- Unwraps the result or reports the error with the error description and exits.
Traits§
- An extension trait for
unwrap
.
Functions§
- Unwraps a result or reports the given message with the error and exits.
- Unwraps a result or reports its error and exits.