core_lib/errors/
error.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::error::Error as StdError;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::result::Result as StdResult;

use crate::errors::Metadata;

// Result
pub type Result<T> = StdResult<T, Error>;

// ErrorCode
pub trait Define {
    fn define(&self) -> &str;
}

// Error
#[derive(Debug, Clone, PartialEq)]
pub struct Error {
    code: String,
    message: String,
    cause: Option<Box<Error>>,
    metadata: Metadata,
}

impl Error {
    pub fn new<C, S, M>(code: C, message: S, metadata: M) -> Error
    where
        C: Define,
        S: Into<String>,
        M: Into<Option<Metadata>>,
    {
        let metadata = metadata.into().unwrap_or_else(Metadata::new);

        Error {
            code: code.define().to_owned(),
            message: message.into(),
            cause: None,
            metadata,
        }
    }

    pub fn wrap<C, S, M>(code: C, err: Error, message: S, metadata: M) -> Error
    where
        C: Define,
        S: Into<String>,
        M: Into<Option<Metadata>>,
    {
        let mut error = Error::new(code, message, metadata);
        error.cause = Some(Box::new(err));
        error
    }

    pub fn wrap_raw<C, E, S, M>(code: C, err: &E, message: S, metadata: M) -> Error
    where
        C: Define,
        E: StdError + ?Sized,
        S: Into<String>,
        M: Into<Option<Metadata>>,
    {
        Error::new(
            code,
            format!("{} ({})", message.into(), err.to_string()),
            metadata,
        )
    }

    pub fn code(&self) -> &str {
        &self.code
    }

    pub fn message(&self) -> &str {
        &self.message
    }

    pub fn cause(&self) -> Option<&Box<Error>> {
        self.cause.as_ref()
    }

    pub fn metadata(&self) -> &Metadata {
        &self.metadata
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "{}: {}", self.code, self.message)
    }
}

impl StdError for Error {}

#[cfg(test)]
mod tests {
    use super::*;

    use serde_json::Value;
    use std::io::{Error as IoError, ErrorKind as IoErrorKind};

    #[derive(PartialEq)]
    enum CustomError {
        Internal,
        Validation,
        Application,
    }

    impl Define for CustomError {
        fn define(&self) -> &str {
            match self {
                CustomError::Internal => "internal",
                CustomError::Validation => "validation",
                CustomError::Application => "application",
            }
        }
    }

    #[test]
    fn create_new_error() {
        let err = Error::new(CustomError::Internal, "internal error", None);
        assert_eq!(err.code().to_string(), "internal");
        assert_eq!(err.message(), "internal error");
        assert!(err.cause().is_none());
        assert_eq!(err.metadata().values().len(), 0);
        assert_eq!(err.to_string(), "internal: internal error");
    }

    #[test]
    fn with_metadata() {
        let err = Error::new(
            CustomError::Validation,
            "some error",
            Metadata::with("key1", "value1")
                .and("key2", "value2")
                .and("key3", "value3"),
        );
        assert_eq!(err.metadata().values().len(), 3);

        assert_eq!(
            err.metadata().values()["key1"],
            Value::String("value1".to_string())
        );
        assert_eq!(
            err.metadata().values()["key2"],
            Value::String("value2".to_string())
        );
        assert_eq!(
            err.metadata().values()["key3"],
            Value::String("value3".to_string())
        );
    }

    #[test]
    fn wrap_error() {
        let internal_err = Error::new(
            CustomError::Internal,
            "internal",
            Metadata::with("internal", true),
        );
        let application_err = Error::wrap(
            CustomError::Application,
            internal_err.clone(),
            "application",
            Metadata::with("application", true),
        );

        assert!(application_err.cause().is_some());
        assert_eq!(application_err.cause(), Some(&Box::new(internal_err)));
    }

    #[test]
    fn wrap_raw_error() {
        let raw_err = IoError::new(IoErrorKind::NotFound, "raw_err");
        let err = Error::wrap_raw(CustomError::Internal, &raw_err, "internal error", None);

        assert!(err.cause().is_none());
        assert_eq!(err.message(), "internal error (raw_err)");
    }
}