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
//! `serde` serialization support - allows for producing e.g. JSON
//! serializations of errors.

use crate::{BoxError, Context, Error};
use backtrace::Backtrace;
use serde::{ser, Serialize};
use std::{
    error::Error as _,
    fmt::{Debug, Display},
    ops::Deref,
};

/// Serializable error type, useful for sending to exception reporting systems.
#[derive(Clone, Debug, Serialize)]
pub struct SerializedError<Kind>
where
    Kind: Clone + Debug + Serialize,
{
    /// Name of the error kind type
    #[serde(rename = "type")]
    pub type_name: String,

    /// Kind of error
    pub kind: Kind,

    /// Error message
    pub msg: String,

    /// Error source
    pub source: Option<String>,

    /// Error backtrace
    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)
    }
}