api_response/
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use std::{self, collections::HashMap, error::Error, fmt, sync::Arc};

use serde::{Deserialize, Serialize};

use crate::{ApiResponse, MaybeString, utils::OrderedHashMap};

/// Struct to represent an error response
#[cfg_attr(feature = "salvo", derive(salvo::prelude::ToSchema))]
#[derive(Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ErrorResponse<Meta> {
    pub error: ApiError,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub meta: Option<Meta>,
}

impl<Meta> ErrorResponse<Meta> {
    #[inline(always)]
    pub const fn new(error: ApiError, meta: Meta) -> Self {
        ErrorResponse {
            error,
            meta: Some(meta),
        }
    }
    #[inline(always)]
    pub const fn from_error(error: ApiError) -> Self {
        ErrorResponse { error, meta: None }
    }
    #[inline(always)]
    pub fn from_error_msg(code: impl Into<i32>, message: impl Into<String>) -> Self {
        Self::from_error(ApiError::new(code, message))
    }
    #[inline(always)]
    pub fn from_error_source(
        code: impl Into<i32>,
        source: impl Error + Send + Sync + 'static,
        set_source_detail: bool,
        message: impl Into<MaybeString>,
    ) -> Self {
        Self::from_error(ApiError::from_source(code, source, set_source_detail, message))
    }
    #[inline(always)]
    pub fn with_meta(mut self, meta: Meta) -> Self {
        self.set_meta(meta);
        self
    }
    #[inline(always)]
    pub fn set_meta(&mut self, meta: Meta) -> &mut Self {
        self.meta = Some(meta);
        self
    }
    #[inline(always)]
    pub fn with_detail(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.set_detail(key, value);
        self
    }
    #[inline(always)]
    pub fn set_detail(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
        self.error.set_detail(key, value);
        self
    }
    #[inline(always)]
    pub fn with_source(mut self, source: impl Error + Send + Sync + 'static, set_source_detail: bool) -> Self {
        self.set_source(source, set_source_detail);
        self
    }
    #[inline(always)]
    pub fn set_source(&mut self, source: impl Error + Send + Sync + 'static, set_source_detail: bool) -> &mut Self {
        self.error.set_source(source, set_source_detail);
        self
    }
    #[inline]
    pub const fn code(&self) -> i32 {
        self.error.code()
    }
    #[inline]
    pub const fn message(&self) -> &String {
        self.error.message()
    }
    #[inline]
    pub fn details(&self) -> Option<&HashMap<String, String>> {
        self.error.details()
    }
    #[inline]
    pub fn detail(&self, key: impl AsRef<str>) -> Option<&String> {
        self.error.detail(key)
    }
    #[inline(always)]
    pub fn is<E: Error + 'static>(&self) -> bool {
        self.error.is::<E>()
    }
    #[inline(always)]
    pub fn downcast_ref<E: Error + 'static>(&self) -> Option<&E> {
        self.error.downcast_ref::<E>()
    }
}
impl<Meta> From<ApiError> for ErrorResponse<Meta> {
    fn from(value: ApiError) -> Self {
        Self::from_error(value)
    }
}
impl<Meta> fmt::Display for ErrorResponse<Meta> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.error)
    }
}
impl<Meta: fmt::Debug> Error for ErrorResponse<Meta> {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.error.source()
    }
}

/// Struct to represent error information
#[cfg_attr(feature = "salvo", derive(salvo::prelude::ToSchema))]
#[derive(Serialize, Deserialize)]
pub struct ApiError {
    pub(crate) code: i32,
    pub(crate) message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) details: Option<OrderedHashMap<String, String>>,
    #[serde(skip)]
    pub(crate) source: Option<Arc<dyn Error + Send + Sync + 'static>>,
}

impl fmt::Debug for ApiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ApiError")
            .field("code", &self.code)
            .field("message", &self.message)
            .field("details", &self.details)
            .finish()
    }
}

impl fmt::Display for ApiError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} Code({})", self.message, self.code)
    }
}

impl Error for ApiError {
    #[allow(clippy::as_conversions)]
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        self.source
            .as_ref()
            .map(|source| source.as_ref() as &(dyn Error + 'static))
    }
}

impl ApiError {
    #[inline(always)]
    pub fn new(code: impl Into<i32>, message: impl Into<String>) -> Self {
        ApiError {
            code: code.into(),
            message: message.into(),
            details: None,
            source: None,
        }
    }
    #[inline(always)]
    pub fn from_source(
        code: impl Into<i32>,
        source: impl Error + Send + Sync + 'static,
        set_source_detail: bool,
        message: impl Into<MaybeString>,
    ) -> Self {
        let mut e = ApiError {
            code: code.into(),
            message: message
                .into()
                .option_string()
                .map_or_else(|| source.to_string(), Into::into),
            details: None,
            source: Some(Arc::new(source)),
        };
        if set_source_detail {
            e.set_source_detail();
        }
        e
    }
    #[inline(always)]
    pub fn with_details(mut self, details: HashMap<String, String>) -> Self {
        self.details = Some(OrderedHashMap(details));
        self
    }
    #[inline]
    pub fn with_detail(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.set_detail(key, value);
        self
    }
    #[inline]
    pub fn set_detail(&mut self, key: impl Into<String>, value: impl Into<String>) -> &mut Self {
        self.details.get_or_insert_default().insert(key.into(), value.into());
        self
    }
    #[inline(always)]
    pub fn with_source(mut self, source: impl Error + Send + Sync + 'static, set_source_detail: bool) -> Self {
        self.set_source(source, set_source_detail);
        self
    }
    #[inline(always)]
    pub fn set_source(&mut self, source: impl Error + Send + Sync + 'static, set_source_detail: bool) -> &mut Self {
        self.source = Some(Arc::new(source));
        if set_source_detail {
            self.set_source_detail();
        }
        self
    }
    /// Insert the setted source error into the detail field.
    #[inline(always)]
    fn set_source_detail(&mut self) -> &mut Self {
        if let Some(source) = &self.source {
            self.set_detail("source", source.to_string());
        }
        self
    }
    #[inline]
    pub const fn code(&self) -> i32 {
        self.code
    }
    #[inline]
    pub const fn message(&self) -> &String {
        &self.message
    }
    #[inline]
    pub fn details(&self) -> Option<&HashMap<String, String>> {
        self.details.as_deref()
    }
    #[inline]
    pub fn detail(&self, key: impl AsRef<str>) -> Option<&String> {
        self.details.as_ref()?.get(key.as_ref())
    }
    pub fn is<E: Error + 'static>(&self) -> bool {
        match &self.source {
            Some(source) => source.is::<E>(),
            None => false,
        }
    }
    pub fn downcast_ref<E: Error + 'static>(&self) -> Option<&E> {
        match &self.source {
            Some(source) => source.downcast_ref(),
            None => None,
        }
    }
    pub const fn api_response<Data, Meta>(self, meta: Option<Meta>) -> ApiResponse<Data, Meta> {
        ApiResponse::Error(ErrorResponse { error: self, meta })
    }
    #[inline(always)]
    pub const fn api_response_without_meta<Data, Meta>(self) -> ApiResponse<Data, Meta>
    where
        Self: Sized,
    {
        self.api_response(None)
    }
    #[inline(always)]
    pub const fn api_response_with_meta<Data, Meta>(self, meta: Meta) -> ApiResponse<Data, Meta>
    where
        Self: Sized,
    {
        self.api_response(Some(meta))
    }
}

#[cfg(test)]
mod tests {
    use super::ApiError;
    use crate::{
        ErrorResponse,
        error_code::{ErrCode, ErrSegment, ErrType, ModPath, ModSection, ModSegment},
    };
    #[test]
    fn display() {
        const ET: ErrType = ErrType::new(ErrSegment::E100, "The operation was cancelled.");
        const MS0: ModSection = ModSection::new(ModSegment::M00, "module 0");
        const MS1: ModSection = ModSection::new(ModSegment::M01, "module 01");
        const MS2: ModSection = ModSection::new(ModSegment::M02, "module 012");
        const MP: ModPath = ModPath::new(MS0, MS1, MS2);
        const EC: ErrCode = ErrCode::new(ET, MP);
        let err_code: ErrCode = ET | MP;
        assert_eq!(EC, err_code);
        let api_error: ApiError = ET + MP;
        assert_eq!(EC.to_api_error().code(), api_error.code());
        assert_eq!(
            "The operation was cancelled. ErrCode(100000102), M00(module 0)/M01(module 01)/M02(module 012)",
            EC.to_string()
        );
        let api_error: ApiError = EC.to_api_error();
        assert_eq!("The operation was cancelled. Code(100000102)", api_error.to_string());
        let err_resp: ErrorResponse<()> = ErrorResponse::from_error(api_error);
        assert_eq!("The operation was cancelled. Code(100000102)", err_resp.to_string());
    }
}