-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
50 lines (48 loc) · 1.43 KB
/
error.rs
File metadata and controls
50 lines (48 loc) · 1.43 KB
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
40
41
42
43
44
45
46
47
48
49
50
use serde::Serialize;
use snafu::Snafu;
/// Wrapped version of the errors returned by [`sysinfo`], since they are bare [`str`]s.
#[derive(Debug, Snafu)]
#[snafu(display("{msg}"))]
pub struct SysinfoError {
pub msg: &'static str,
}
/// Wraps errors returned by a component to present them consistently for serialization.
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum ComponentResult<T> {
Ok(T),
Err {
#[serde(rename = "$error")]
inner: ComponentError,
},
}
impl<T> ComponentResult<T> {
#[track_caller]
pub fn report_from_result<E: std::error::Error + 'static>(
component: &str,
result: Result<T, E>,
) -> ComponentResult<T> {
match result {
Ok(x) => ComponentResult::Ok(x),
Err(err) => {
tracing::error!(
error = &err as &dyn std::error::Error,
"error reported by {component}, ignoring...",
);
ComponentResult::Err {
inner: ComponentError {
message: err.to_string(),
causes: std::iter::successors(err.source(), |err| err.source())
.map(|err| err.to_string())
.collect(),
},
}
}
}
}
}
#[derive(Debug, Serialize)]
pub struct ComponentError {
message: String,
causes: Vec<String>,
}