surrealcs_kernel/utils/
generic.rsuse nanoservices_utils::errors::{NanoServiceError, NanoServiceErrorStatus};
pub static KEY_ALREADY_EXISTS_ERR: &str = "Key already exists";
pub fn check_key_already_exists(error: &NanoServiceError) -> bool {
error.status == NanoServiceErrorStatus::BadRequest && error.message == KEY_ALREADY_EXISTS_ERR
}
pub fn construct_key_already_exists_error() -> NanoServiceError {
NanoServiceError::new(KEY_ALREADY_EXISTS_ERR.to_string(), NanoServiceErrorStatus::BadRequest)
}
pub static CONDITION_NOT_MET_ERR: &str = "Condition not met";
pub fn check_condition_not_met(error: &NanoServiceError) -> bool {
error.status == NanoServiceErrorStatus::BadRequest && error.message == CONDITION_NOT_MET_ERR
}
pub fn construct_condition_not_met_error() -> NanoServiceError {
NanoServiceError::new(CONDITION_NOT_MET_ERR.to_string(), NanoServiceErrorStatus::BadRequest)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_check_condition_not_met_true() {
let error = NanoServiceError::new(
CONDITION_NOT_MET_ERR.to_string(),
NanoServiceErrorStatus::BadRequest,
);
assert!(check_condition_not_met(&error));
}
#[test]
fn test_check_condition_not_met_false() {
let error = NanoServiceError::new(
"Some other error".to_string(),
NanoServiceErrorStatus::BadRequest,
);
assert!(!check_condition_not_met(&error));
}
#[test]
fn test_construct_condition_not_met_error() {
let error = construct_condition_not_met_error();
assert_eq!(error.status, NanoServiceErrorStatus::BadRequest);
assert_eq!(error.message, CONDITION_NOT_MET_ERR);
}
}