1 个不稳定版本
| 0.1.1 | 2021年1月8日 | 
|---|---|
| 0.1.0 |  | 
#19 在 #panics
10KB
93 行
用于报告致命错误并带有错误代码退出的实用程序。
该crate中的行为与基于panic!退出的行为不同,因为这里的退出更适合向终端用户显示,即没有“thread main panicked at”,没有回溯提及等。
使用方法
用于解包Result
- 使用 expect/expect_fatal带上下文报告错误。
- 使用 unwrap_message!使用格式化expect。
- 使用 unwrap_format!以获得对消息格式的更多控制。
- 使用 unwrap/unwrap_fatal在提供/明显提供上下文时报告错误。
用于中止
(伪)示例
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());
}
依赖项
~0–7MB
~38K SLoC