mycelium_base/utils/errors/
factories.rs1use super::base::{ErrorType, MappedErrors};
2
3pub fn creation_err<T: ToString>(msg: T) -> MappedErrors {
5 MappedErrors::default(msg.to_string())
6 .with_error_type(ErrorType::CreationError)
7}
8
9pub fn updating_err<T: ToString>(msg: T) -> MappedErrors {
11 MappedErrors::default(msg.to_string())
12 .with_error_type(ErrorType::UpdatingError)
13}
14
15pub fn updating_many_err<T: ToString>(msg: T) -> MappedErrors {
17 MappedErrors::default(msg.to_string())
18 .with_error_type(ErrorType::UpdatingManyError)
19}
20
21pub fn fetching_err<T: ToString>(msg: T) -> MappedErrors {
23 MappedErrors::default(msg.to_string())
24 .with_error_type(ErrorType::FetchingError)
25}
26
27pub fn deletion_err<T: ToString>(msg: T) -> MappedErrors {
29 MappedErrors::default(msg.to_string())
30 .with_error_type(ErrorType::DeletionError)
31}
32
33pub fn use_case_err<T: ToString>(msg: T) -> MappedErrors {
35 MappedErrors::default(msg.to_string())
36 .with_error_type(ErrorType::UseCaseError)
37}
38
39pub fn execution_err<T: ToString>(msg: T) -> MappedErrors {
41 MappedErrors::default(msg.to_string())
42 .with_error_type(ErrorType::ExecutionError)
43}
44
45pub fn invalid_repo_err<T: ToString>(msg: T) -> MappedErrors {
47 MappedErrors::default(msg.to_string())
48 .with_error_type(ErrorType::InvalidRepositoryError)
49}
50
51pub fn invalid_arg_err<T: ToString>(msg: T) -> MappedErrors {
53 MappedErrors::default(msg.to_string())
54 .with_error_type(ErrorType::InvalidArgumentError)
55}
56
57pub fn dto_err<T: ToString>(msg: T) -> MappedErrors {
59 MappedErrors::default(msg.to_string())
60 .with_error_type(ErrorType::DataTransferLayerError)
61}
62
63pub fn general_err(msg: String, error_type: String) -> MappedErrors {
65 MappedErrors::default(msg.to_string())
66 .with_error_type(ErrorType::GeneralError(error_type))
67}
68
69#[cfg(test)]
74mod test {
75 use super::*;
76 use crate::utils::errors::{base::ErrorType, ErrorCodes};
77
78 #[test]
79 fn test_default_factories() {
80 assert_eq!(
81 creation_err("create").error_type(),
82 ErrorType::CreationError
83 );
84
85 assert_eq!(
86 updating_err("update".to_string()).error_type(),
87 ErrorType::UpdatingError
88 );
89
90 assert_eq!(
91 fetching_err("fetch".to_string()).error_type(),
92 ErrorType::FetchingError
93 );
94
95 assert_eq!(
96 deletion_err("delete".to_string()).error_type(),
97 ErrorType::DeletionError
98 );
99
100 assert_eq!(
101 use_case_err("use_case".to_string()).error_type(),
102 ErrorType::UseCaseError
103 );
104
105 assert_eq!(
106 execution_err("execution".to_string()).error_type(),
107 ErrorType::ExecutionError
108 );
109
110 assert_eq!(
111 invalid_repo_err("invalid_repo".to_string()).error_type(),
112 ErrorType::InvalidRepositoryError
113 );
114
115 assert_eq!(
116 invalid_arg_err("invalid_arg".to_string()).error_type(),
117 ErrorType::InvalidArgumentError
118 );
119 }
120
121 #[test]
122 fn test_creation_error_factory() {
123 fn result_function() -> Result<String, MappedErrors> {
124 creation_err("create".to_string())
125 .with_code("ID001")
126 .with_code("ID002")
127 .as_error()
128 }
129
130 let result = result_function().unwrap_err();
131
132 assert!(
133 result.code()
134 == ErrorCodes::Codes(vec![
135 "ID001".to_string(),
136 "ID002".to_string()
137 ])
138 );
139 }
140}