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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
use crate::domain::dtos::error_code::ErrorCode;
use enum_iterator::{all, Sequence};
use mycelium_base::utils::errors::{execution_err, MappedErrors};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
fmt::{Display, Formatter, Result as FmtResult},
};
/// Here the mycelium native error codes are defined
///
/// This is a list of all the error codes that are used in the system.
#[derive(
Debug, PartialEq, Sequence, Serialize, Deserialize, Hash, Eq, Clone, Copy,
)]
pub enum NativeErrorCodes {
///
/// code: "MYC00001",
/// message: "Database Client Unavailable error",
/// details: "Database Client error. Could not fetch client.",
/// is_internal: true,
/// is_native: true
///
MYC00001 = 1,
///
/// code: "MYC00002",
/// message: "User already registered in Mycelium",
/// details: "When a manager account try to register a new account and the account owner already exists this error should be returned.",
/// is_internal: false,
/// is_native: true
///
MYC00002,
///
/// code: "MYC00003",
/// message: "Account already registered in Mycelium",
/// details: "When a manager account try to register a new account and the account already exists this error should be returned.",
/// is_internal: false,
/// is_native: true
///
MYC00003,
///
/// code: "MYC00004",
/// message: "Could not check profile verbose status",
/// details: "This error should be dispatched when use-cases could not access the account verbose-status during validations.",
/// is_internal: false,
/// is_native: true
///
MYC00004,
///
/// code: "MYC00005",
/// message: "Action restricted to active users",
/// details: "Indicates that the desired action should only be performed by active users.",
/// is_internal: true,
/// is_native: true
///
MYC00005,
///
/// code: "MYC00006",
/// message: "Action restricted to manager users",
/// details: "Indicates that the action requires manager privileges.",
/// is_internal: true,
/// is_native: true
///
MYC00006,
///
/// code: "MYC00007",
/// message: "Updating action failed",
/// details: "Action dispatched when an update action was preceded by unknown error.",
/// is_internal: true,
/// is_native: true
///
MYC00007,
///
/// code: "MYC00008",
/// message: "Token not found or expired",
/// details: "Indicates that the token was not found or has expired.",
/// is_internal: false,
/// is_native: true
///
MYC00008,
///
/// code: "MYC00009",
/// message: "User not found",
/// details: "Indicates that the user was not found.",
/// is_internal: false,
/// is_native: true
///
MYC00009,
///
/// code: "MYC00010",
/// message: "Unable to notify user",
/// details: "Indicates that the system was unable to notify the user, but the action was successful.",
/// is_internal: false,
/// is_native: true
///
MYC00010,
///
/// code: "MYC00011",
/// message: "New Password is the same as the old one",
/// details: "Indicates that the new password is the same as the old one.",
/// is_internal: false,
/// is_native: true
///
MYC00011,
///
/// code: "MYC00012",
/// message: "Unable to validate password",
/// details: "Indicates that the system was unable to validate the password.",
/// is_internal: false,
/// is_native: true
///
MYC00012,
///
/// code: "MYC00013",
/// message: "Unauthorized action",
/// details: "Indicates that the action is unauthorized.",
/// is_internal: false,
/// is_native: true
///
MYC00013,
///
/// code: "MYC00014",
/// message: "Tenant name already exists",
/// details: "Indicates that the tenant name already exists.",
/// is_internal: false,
/// is_native: true
///
MYC00014,
///
/// code: "MYC00015",
/// message: "Tenant owner already exists",
/// details: "Indicates that the tenant owner already exists.",
/// is_internal: false,
/// is_native: true
///
MYC00015,
///
/// code: "MYC00016",
/// message: "Tenant owner not found",
/// details: "Indicates that the tenant owner was not found.",
/// is_internal: false,
/// is_native: true
///
MYC00016,
///
/// code: "MYC00017",
/// message: "Guest already exists",
/// details: "Indicates that the guest already exists.",
/// is_internal: false,
/// is_native: true
///
MYC00017,
///
/// code: "MYC00018",
/// message: "Invalid user operation",
/// details: "Indicates that the user operation is invalid.",
/// is_internal: false,
/// is_native: true
///
MYC00018,
///
/// code: "MYC00019",
/// message: "Insufficient privileges",
/// details: "Indicates that the user has insufficient privileges.",
/// is_internal: false,
/// is_native: true
///
MYC00019,
///
/// code: "MYC00020",
/// message: "Possible security issue",
/// details: "The informed scope is not valid.",
/// is_internal: false,
/// is_native: true
///
MYC00020,
///
/// code: "MYC00021",
/// message: "Totp Already Enabled",
/// details: "Indicates that the user already has TOTP enabled.",
/// is_internal: false,
/// is_native: true
///
MYC00021,
///
/// code: "MYC00022",
/// message: "Totp Disabled",
/// details: "Indicates that the user does not have TOTP enabled.",
/// is_internal: false,
/// is_native: true
///
MYC00022,
///
/// code: "MYC00023",
/// message: "Totp Token invalid",
/// details: "Indicates that the TOTP token is invalid.",
/// is_internal: false,
/// is_native: true
///
MYC00023,
}
impl NativeErrorCodes {
/// Get parts of a single error code enumerator element
///
/// This method will return a tuple with the prefix and the code of the
/// enumerator element.
pub fn parts(&self) -> (String, i32) {
let pattern = Self::get_self_validation_pattern();
let error_item = &self.to_string();
if !pattern.is_match(error_item) {
panic!("Invalid native error code enum format.");
}
let capture = pattern.captures(error_item).unwrap();
(capture[1].to_string(), capture[2].parse::<i32>().unwrap())
}
/// Get the error code as a str.
pub fn as_str(&self) -> &'static str {
match self {
Self::MYC00001 => "MYC00001",
Self::MYC00002 => "MYC00002",
Self::MYC00003 => "MYC00003",
Self::MYC00004 => "MYC00004",
Self::MYC00005 => "MYC00005",
Self::MYC00006 => "MYC00006",
Self::MYC00007 => "MYC00007",
Self::MYC00008 => "MYC00008",
Self::MYC00009 => "MYC00009",
Self::MYC00010 => "MYC00010",
Self::MYC00011 => "MYC00011",
Self::MYC00012 => "MYC00012",
Self::MYC00013 => "MYC00013",
Self::MYC00014 => "MYC00014",
Self::MYC00015 => "MYC00015",
Self::MYC00016 => "MYC00016",
Self::MYC00017 => "MYC00017",
Self::MYC00018 => "MYC00018",
Self::MYC00019 => "MYC00019",
Self::MYC00020 => "MYC00020",
Self::MYC00021 => "MYC00021",
Self::MYC00022 => "MYC00022",
Self::MYC00023 => "MYC00023",
}
}
pub fn iter() -> impl Iterator<Item = NativeErrorCodes> {
all::<NativeErrorCodes>()
}
pub fn as_doc(&self) -> Result<ErrorCode, MappedErrors> {
match self {
Self::MYC00001 => Ok(ErrorCode::new_internal_error(
"MYC".to_string(),
1,
"Database Client Unavailable error".to_string(),
true,
)?.with_details("Database Client error. Could not fetch client.".to_string())),
Self::MYC00002 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
2,
"User already registered in Mycelium".to_string(),
true,
)?.with_details("When a manager account try to register a new account and the account owner already exists this error should be returned.".to_string())),
Self::MYC00003 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
3,
"Account already registered in Mycelium".to_string(),
true,
)?.with_details("When a manager account try to register a new account and the account already exists this error should be returned.".to_string())),
Self::MYC00004 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
4,
"Could not check profile verbose status".to_string(),
true,
)?.with_details("This error should be dispatched when use-cases could not access the account verbose-status during validations.".to_string())),
Self::MYC00005 => Ok(ErrorCode::new_internal_error(
"MYC".to_string(),
5,
"Action restricted to active users".to_string(),
true,
)?.with_details("Indicates that the desired action should only be performed by active users.".to_string())),
Self::MYC00006 => Ok(ErrorCode::new_internal_error(
"MYC".to_string(),
6,
"Action restricted to manager users".to_string(),
true,
)?.with_details("Indicates that the action requires manager privileges.".to_string())),
Self::MYC00007 => Ok(ErrorCode::new_internal_error(
"MYC".to_string(),
7,
"Updating action failed".to_string(),
true,
)?.with_details("Action dispatched when an update action was preceded by unknown error.".to_string())),
Self::MYC00008 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
8,
"Token not found or expired".to_string(),
true,
)?.with_details("Indicates that the token was not found or has expired.".to_string())),
Self::MYC00009 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
9,
"User not found".to_string(),
true,
)?.with_details("Indicates that the user was not found.".to_string())),
Self::MYC00010 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
10,
"Unable to notify user".to_string(),
true,
)?.with_details("Indicates that the system was unable to notify the user, but the action was successful.".to_string())),
Self::MYC00011 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
11,
"New Password is the same as the old one".to_string(),
true,
)?.with_details("Indicates that the new password is the same as the old one.".to_string())),
Self::MYC00012 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
12,
"Unable to validate password".to_string(),
true,
)?.with_details("Indicates that the system was unable to validate the password.".to_string())),
Self::MYC00013 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
13,
"Unauthorized action".to_string(),
true,
)?.with_details("Indicates that the action is unauthorized.".to_string())),
Self::MYC00014 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
14,
"Tenant name already exists".to_string(),
true,
)?.with_details("Indicates that the tenant name already exists.".to_string())),
Self::MYC00015 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
15,
"Tenant owner already exists".to_string(),
true,
)?.with_details("Indicates that the tenant owner already exists.".to_string())),
Self::MYC00016 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
16,
"Tenant owner not found".to_string(),
true,
)?.with_details("Indicates that the tenant owner was not found.".to_string())),
Self::MYC00017 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
17,
"Guest already exists".to_string(),
true,
)?.with_details("Indicates that the guest already exists.".to_string())),
Self::MYC00018 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
18,
"Invalid user operation".to_string(),
true,
)?.with_details("Indicates that the user operation is invalid.".to_string())),
Self::MYC00019 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
19,
"Insufficient privileges".to_string(),
true,
)?.with_details("Indicates that the user has insufficient privileges.".to_string())),
Self::MYC00020 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
20,
"Possible security issue".to_string(),
true,
)?.with_details("The informed scope is not valid.".to_string())),
Self::MYC00021 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
21,
"Totp Already Enabled".to_string(),
true,
)?.with_details("Indicates that the user already has TOTP enabled.".to_string())),
Self::MYC00022 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
22,
"Totp Disabled".to_string(),
true,
)?.with_details("Indicates that the user does not have TOTP enabled.".to_string())),
Self::MYC00023 => Ok(ErrorCode::new_external_error(
"MYC".to_string(),
23,
"Totp Token invalid".to_string(),
true,
)?.with_details("Indicates that the TOTP token is invalid.".to_string())),
}
}
/// Get the error code options.
///
/// This method will check if all entries in the enum are in the correct.
/// Case yes, it will return a hashmap with the native enums codes as keys
/// and the error code as values.
///
/// This method should be used during the initialization of the application.
///
pub fn to_error_codes(
) -> Result<HashMap<NativeErrorCodes, ErrorCode>, MappedErrors> {
Self::self_validate()?;
let mut error_code_sources =
HashMap::<NativeErrorCodes, ErrorCode>::new();
for item in Self::iter() {
let mut error_code = item.as_doc()?;
if let Some(details) = error_code.details.to_owned() {
error_code = error_code.with_details(details.to_string());
}
error_code_sources.insert(item.to_owned(), error_code.to_owned());
}
Ok(error_code_sources)
}
/// Create a vector with all the error code options.
fn to_vec() -> Vec<Self> {
all::<NativeErrorCodes>().collect::<Vec<_>>()
}
/// Validate the format of the enum
///
/// This method will panic if the enum format is invalid. This is done to
/// ensure that the enum is always in the correct format. This is a private
/// method that is only used internally during tests.
fn self_validate() -> Result<Regex, MappedErrors> {
let pattern = Self::get_self_validation_pattern();
let error_codes = Self::to_vec();
for error_code in error_codes {
let error_item = &error_code.to_string();
if !pattern.is_match(error_item) {
return execution_err(
"Invalid native error code enum format.".to_string(),
)
.as_error();
}
}
Ok(pattern)
}
/// Get the regex pattern for the enum format validation.
fn get_self_validation_pattern() -> Regex {
Regex::new(r"^([A-Z]{2,4})([0-9]+)$").unwrap()
}
}
impl Display for NativeErrorCodes {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "{:?}", self)
}
}
// * ---------------------------------------------------------------------------
// * TESTS
// * ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_validate_native_error_code_enum_format() {
let pattern = NativeErrorCodes::get_self_validation_pattern();
let error_codes = NativeErrorCodes::to_vec();
for error_code in error_codes {
let error_item = &error_code.to_string();
assert!(pattern.is_match(error_item));
}
}
#[test]
fn should_fail_to_validate_native_error_code_enum_format() {
let pattern = NativeErrorCodes::get_self_validation_pattern();
let error_codes = NativeErrorCodes::to_vec();
for error_code in error_codes {
let error_item = &error_code.to_string();
assert!(pattern.is_match(error_item));
}
}
#[test]
fn should_get_native_error_code_parts() {
let error_code = NativeErrorCodes::MYC00001;
let (prefix, code) = error_code.parts();
assert_eq!(prefix, "MYC");
assert_eq!(code, 1);
}
#[test]
fn should_fail_to_get_native_error_code_parts() {
let error_code = NativeErrorCodes::MYC00001;
let (prefix, code) = error_code.parts();
assert_eq!(prefix, "MYC");
assert_ne!(code, 2);
}
#[test]
fn test_self_validate() {
let pattern = NativeErrorCodes::self_validate().unwrap();
let error_codes = NativeErrorCodes::to_vec();
for error_code in error_codes {
let error_item = &error_code.to_string();
assert!(pattern.is_match(error_item));
}
}
}