Add a new error message when the valid_fields is empty

> "Attribute `{}` is not sortable. This index doesn't have configured sortable attributes."
> "Attribute `{}` is not sortable. Available sortable attributes are: `{}`."

coexist in the error handling
This commit is contained in:
Bruno Casali 2022-03-04 16:34:03 -03:00
parent df518d8b0b
commit 66c6d5e1ef

View File

@ -256,11 +256,20 @@ only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and undersco
Self::InvalidSortableAttribute { field, valid_fields } => {
let valid_names =
valid_fields.iter().map(AsRef::as_ref).collect::<Vec<_>>().join(", ");
write!(
f,
"Attribute `{}` is not sortable. Available sortable attributes are: `{}`.",
field, valid_names
)
if valid_names.is_empty() {
write!(
f,
"Attribute `{}` is not sortable. This index does not have configured sortable attributes.",
field
)
} else {
write!(
f,
"Attribute `{}` is not sortable. Available sortable attributes are: `{}`.",
field, valid_names
)
}
}
Self::SortRankingRuleMissing => f.write_str(
"The sort ranking rule must be specified in the \
@ -320,3 +329,19 @@ impl fmt::Display for SerializationError {
}
impl StdError for SerializationError {}
#[test]
fn conditionally_lookup_for_error_message() {
let prefix = "Attribute `name` is not sortable.";
let messages = vec![
(BTreeSet::new(), "This index does not have configured sortable attributes."),
(BTreeSet::from(["age".to_string()]), "Available sortable attributes are: `age`."),
];
for (list, suffix) in messages {
let err =
UserError::InvalidSortableAttribute { field: "name".to_string(), valid_fields: list };
assert_eq!(err.to_string(), format!("{} {}", prefix, suffix));
}
}