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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::{BoxError, Context, Error};
use backtrace::Backtrace;
use serde::{ser, Serialize};
use std::{
error::Error as _,
fmt::{Debug, Display},
ops::Deref,
};
#[derive(Clone, Debug, Serialize)]
pub struct SerializedError<Kind>
where
Kind: Clone + Debug + Serialize,
{
#[serde(rename = "type")]
pub type_name: String,
pub kind: Kind,
pub msg: String,
pub source: Option<String>,
pub backtrace: Option<Backtrace>,
}
impl<Kind> From<&Context<Kind>> for SerializedError<Kind>
where
Kind: Clone + Debug + Display + Into<BoxError> + Serialize,
{
fn from(context: &Context<Kind>) -> Self {
Self {
type_name: std::any::type_name::<Kind>().to_owned(),
kind: context.kind().clone(),
msg: context.kind().to_string(),
source: context.source().map(ToString::to_string),
backtrace: context.backtrace().cloned(),
}
}
}
impl<Kind> Serialize for Context<Kind>
where
Kind: Clone + Debug + Display + Into<BoxError> + Serialize,
{
fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
SerializedError::from(self).serialize(serializer)
}
}
impl<Kind> Serialize for Error<Kind>
where
Kind: Clone + Debug + Display + Into<BoxError> + Serialize,
{
fn serialize<S: ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
SerializedError::from(self.deref()).serialize(serializer)
}
}