mycelium_base/utils/errors/
factories.rs

1use super::base::{ErrorType, MappedErrors};
2
3/// A factory for creation errors
4pub fn creation_err<T: ToString>(msg: T) -> MappedErrors {
5    MappedErrors::default(msg.to_string())
6        .with_error_type(ErrorType::CreationError)
7}
8
9/// A factory for updating errors
10pub fn updating_err<T: ToString>(msg: T) -> MappedErrors {
11    MappedErrors::default(msg.to_string())
12        .with_error_type(ErrorType::UpdatingError)
13}
14
15/// A factory for updating many errors
16pub fn updating_many_err<T: ToString>(msg: T) -> MappedErrors {
17    MappedErrors::default(msg.to_string())
18        .with_error_type(ErrorType::UpdatingManyError)
19}
20
21/// A factory for fetching errors
22pub fn fetching_err<T: ToString>(msg: T) -> MappedErrors {
23    MappedErrors::default(msg.to_string())
24        .with_error_type(ErrorType::FetchingError)
25}
26
27/// A factory for deletion errors
28pub fn deletion_err<T: ToString>(msg: T) -> MappedErrors {
29    MappedErrors::default(msg.to_string())
30        .with_error_type(ErrorType::DeletionError)
31}
32
33/// A factory for use case errors
34pub fn use_case_err<T: ToString>(msg: T) -> MappedErrors {
35    MappedErrors::default(msg.to_string())
36        .with_error_type(ErrorType::UseCaseError)
37}
38
39/// A factory for execution errors
40pub fn execution_err<T: ToString>(msg: T) -> MappedErrors {
41    MappedErrors::default(msg.to_string())
42        .with_error_type(ErrorType::ExecutionError)
43}
44
45/// A factory for invalid repository errors
46pub fn invalid_repo_err<T: ToString>(msg: T) -> MappedErrors {
47    MappedErrors::default(msg.to_string())
48        .with_error_type(ErrorType::InvalidRepositoryError)
49}
50
51/// A factory for invalid argument errors
52pub fn invalid_arg_err<T: ToString>(msg: T) -> MappedErrors {
53    MappedErrors::default(msg.to_string())
54        .with_error_type(ErrorType::InvalidArgumentError)
55}
56
57/// A factory for data transfer objects error
58pub fn dto_err<T: ToString>(msg: T) -> MappedErrors {
59    MappedErrors::default(msg.to_string())
60        .with_error_type(ErrorType::DataTransferLayerError)
61}
62
63/// A factory for general errors
64pub 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// * ---------------------------------------------------------------------------
70// * TESTS
71// * ---------------------------------------------------------------------------
72
73#[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}