use super::base::{ErrorType, MappedErrors};
pub fn creation_err(msg: String) -> MappedErrors {
MappedErrors::default(msg).with_error_type(ErrorType::CreationError)
}
pub fn updating_err(msg: String) -> MappedErrors {
MappedErrors::default(msg).with_error_type(ErrorType::UpdatingError)
}
pub fn fetching_err(msg: String) -> MappedErrors {
MappedErrors::default(msg).with_error_type(ErrorType::FetchingError)
}
pub fn deletion_err(msg: String) -> MappedErrors {
MappedErrors::default(msg).with_error_type(ErrorType::DeletionError)
}
pub fn use_case_err(msg: String) -> MappedErrors {
MappedErrors::default(msg).with_error_type(ErrorType::UseCaseError)
}
pub fn execution_err(msg: String) -> MappedErrors {
MappedErrors::default(msg).with_error_type(ErrorType::ExecutionError)
}
pub fn invalid_repo_err(msg: String) -> MappedErrors {
MappedErrors::default(msg)
.with_error_type(ErrorType::InvalidRepositoryError)
}
pub fn invalid_arg_err(msg: String) -> MappedErrors {
MappedErrors::default(msg).with_error_type(ErrorType::InvalidArgumentError)
}
#[cfg(test)]
mod test {
use super::*;
use crate::utils::errors::{base::ErrorType, ErrorCode};
#[test]
fn test_default_factories() {
assert_eq!(
creation_err("create".to_string()).error_type(),
ErrorType::CreationError
);
assert_eq!(
updating_err("update".to_string()).error_type(),
ErrorType::UpdatingError
);
assert_eq!(
fetching_err("fetch".to_string()).error_type(),
ErrorType::FetchingError
);
assert_eq!(
deletion_err("delete".to_string()).error_type(),
ErrorType::DeletionError
);
assert_eq!(
use_case_err("use_case".to_string()).error_type(),
ErrorType::UseCaseError
);
assert_eq!(
execution_err("execution".to_string()).error_type(),
ErrorType::ExecutionError
);
assert_eq!(
invalid_repo_err("invalid_repo".to_string()).error_type(),
ErrorType::InvalidRepositoryError
);
assert_eq!(
invalid_arg_err("invalid_arg".to_string()).error_type(),
ErrorType::InvalidArgumentError
);
}
#[test]
fn test_creation_error_factory() {
fn result_function() -> Result<String, MappedErrors> {
creation_err("create".to_string())
.with_code("ID001".to_string())
.as_error()
}
let result = result_function().unwrap_err();
assert!(result.code() == ErrorCode::Code("ID001".to_string()));
}
}