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
use mycelium_base::utils::errors::{execution_err, MappedErrors};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
/// ErrorCode is a struct that represents an error code.
///
/// It is used to represent errors that occur in the system. Error should be
/// internal or external. Internal errors are errors that are not expected to
/// occur in the system. External errors are errors that are not expected to
/// occur in the system.
#[derive(Clone, Debug, Deserialize, Serialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ErrorCode {
/// The prefix of the error.
pub prefix: String,
/// The code of the error.
pub error_number: i32,
/// A compiled string of the prefix and code.
pub code: Option<String>,
/// The message of the error.
pub message: String,
/// The details of the error.
pub details: Option<String>,
/// Whether the error is internal or external.
pub is_internal: bool,
/// Whether the error is native of mycelium or not.
pub is_native: bool,
}
impl ErrorCode {
// ? -----------------------------------------------------------------------
// ? PUBLIC STRUCTURAL METHODS (CONSTRUCTORS)
// ? -----------------------------------------------------------------------
/// Creates a new ErrorCode with the given code and message.
pub fn new(
prefix: String,
error_number: i32,
message: String,
is_internal: bool,
is_native: bool,
) -> Result<Self, MappedErrors> {
ErrorCode::validate_prefix(&prefix)?;
Ok(Self {
prefix,
error_number,
code: None,
message,
details: None,
is_internal,
is_native,
})
}
/// Creates a new internal ErrorCode with the given code and message.
pub fn new_internal_error(
prefix: String,
error_number: i32,
message: String,
is_native: bool,
) -> Result<Self, MappedErrors> {
ErrorCode::validate_prefix(&prefix)?;
Ok(Self {
prefix,
error_number,
code: None,
message,
details: None,
is_internal: true,
is_native,
})
}
/// Creates a new external ErrorCode with the given code and message.
pub fn new_external_error(
prefix: String,
code: i32,
message: String,
is_native: bool,
) -> Result<Self, MappedErrors> {
ErrorCode::validate_prefix(&prefix)?;
Ok(Self {
prefix,
error_number: code,
code: None,
message,
details: None,
is_internal: false,
is_native,
})
}
// ? -----------------------------------------------------------------------
// ? PUBLIC INSTANCE METHODS
// ? -----------------------------------------------------------------------
/// Creates a new ErrorCode with the given code, message, and details.
pub fn with_details(mut self, details: String) -> Self {
self.details = Some(details);
self
}
pub fn with_code(mut self) -> Self {
self.code = Some(format!("{}{:05}", self.prefix, self.error_number));
self
}
// ? -----------------------------------------------------------------------
// ? PRIVATE STATIC METHODS
// ? -----------------------------------------------------------------------
/// Validates the given prefix.
fn validate_prefix(prefix: &str) -> Result<(), MappedErrors> {
let (min_prefix_size, max_prefix_size) =
Self::get_min_and_max_prefix_sizes();
if prefix.len() < min_prefix_size {
return execution_err(format!(
"Prefix must be longest than {min_prefix_size}."
))
.as_error();
}
if prefix.len() > max_prefix_size {
return execution_err(format!(
"Prefix must be shortest than {max_prefix_size}."
))
.as_error();
}
if !prefix
.chars()
.all(|c| c.is_ascii_alphabetic() && c.is_uppercase())
{
return execution_err("Prefix must be all alphabetic.".to_string())
.as_error();
}
Ok(())
}
/// Gets the minimum and maximum prefix sizes.
pub(self) fn get_min_and_max_prefix_sizes() -> (usize, usize) {
(2, 4)
}
}
// * ---------------------------------------------------------------------------
// * TESTS
// * ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_internal_error() {
let error_code = ErrorCode::new_internal_error(
"TEST".to_string(),
1,
"Test error.".to_string(),
false,
)
.unwrap();
assert_eq!(error_code.prefix, "TEST");
assert_eq!(error_code.error_number, 1);
assert_eq!(error_code.message, "Test error.");
assert_eq!(error_code.details, None);
assert_eq!(error_code.is_internal, true);
}
#[test]
fn test_new_external_error() {
let error_code = ErrorCode::new_external_error(
"TEST".to_string(),
1,
"Test error.".to_string(),
false,
)
.unwrap();
assert_eq!(error_code.prefix, "TEST");
assert_eq!(error_code.error_number, 1);
assert_eq!(error_code.message, "Test error.");
assert_eq!(error_code.details, None);
assert_eq!(error_code.is_internal, false);
}
#[test]
fn test_with_details() {
let error_code = ErrorCode::new_internal_error(
"TEST".to_string(),
1,
"Test error.".to_string(),
false,
)
.unwrap()
.with_details("Test details.".to_string());
assert_eq!(error_code.prefix, "TEST");
assert_eq!(error_code.error_number, 1);
assert_eq!(error_code.message, "Test error.");
assert_eq!(error_code.details, Some("Test details.".to_string()));
assert_eq!(error_code.is_internal, true);
}
#[test]
fn test_validate_prefix() {
assert!(ErrorCode::validate_prefix("TEST").is_ok());
assert!(ErrorCode::validate_prefix("test").is_err());
assert!(ErrorCode::validate_prefix("TE").is_ok());
assert!(ErrorCode::validate_prefix("T").is_err());
assert!(ErrorCode::validate_prefix("TEST1").is_err());
}
#[test]
fn test_with_full_code_works() {
for i in 0..31 {
let error_code = ErrorCode::new_internal_error(
"TEST".to_string(),
i,
"Test error.".to_string(),
false,
)
.unwrap()
.with_code();
assert_eq!(error_code.code, Some(format!("TEST{:05}", i)));
}
}
}