1 个不稳定版本
0.1.0 | 2023年5月15日 |
---|
#194 在 #add
6KB
107 代码行
reveal
此库提供了一个宏,可自动为错误添加上下文。
示例
use std::fs;
use std::path::Path;
use reveal::error;
fn main() {
let e = save_user_data(1, "test").unwrap_err();
println!("{}", e);
}
#[error]
fn save_user_data(uid: usize, data: &str) -> reveal::Result<()> {
file_put_contents(Path::new(&format!("/root/{}", uid)), data.as_bytes())?;
Ok(())
}
// `content = "_"` excludes argument `content` from context
#[error(content = "_")]
fn file_put_contents(path: &Path, content: &[u8]) -> reveal::Result<()> {
fs::write(path, content)?;
Ok(())
}
Permission denied (os error 13)
0: fs :: write(path, content)
in file_put_contents(path = "/root/1")
at src/main.rs:21
1: file_put_contents(Path :: new(& format! ("/root/{}", uid)), data.as_bytes())
in save_user_data(uid = 1, data = "test")
at src/main.rs:14
依赖项
~270–720KB
~17K SLoC