-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
39 lines (35 loc) · 1.05 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use anyhow::anyhow;
use valuable_derive::Valuable;
use valuable_serde::Serializable;
#[derive(Valuable)]
pub struct ValuableAnyhow {
message: String,
root_cause: String,
chain: Vec<String>,
}
impl From<&anyhow::Error> for ValuableAnyhow {
fn from(error: &anyhow::Error) -> Self {
ValuableAnyhow {
message: error.to_string(),
root_cause: format!("{:?}", error.root_cause()),
chain: error.chain().skip(1).map(|v| v.to_string()).collect(),
}
}
}
impl From<anyhow::Error> for ValuableAnyhow {
fn from(error: anyhow::Error) -> Self {
ValuableAnyhow {
message: error.to_string(),
root_cause: format!("{:?}", error.root_cause()),
chain: error.chain().skip(1).map(|v| v.to_string()).collect(),
}
}
}
fn main() {
let err = anyhow!("asdf FOO");
let ser = Serializable::new(ValuableAnyhow::from(err));
match serde_json::to_string(&ser) {
Ok(json) => println!("JSON: {}", json),
Err(e) => println!("FAILED: {:?}", e),
}
}