461: Add a new error message when the `valid_fields` is empty r=curquiza a=brunoocasali

I've created a test case to handle the new error formatting behavior, but I'm not sure if:

- this is the right place to add the test?
- this is the best way to test this behavior?

And I'm not sure also regarding the `match` implementation, is this something required? Or maybe just an `if` statement is ok as well?

I left the two messages literally without "reusing the prefix" in the implementation because I think this could help the "searchability" of the error in the future.

# Pull Request

## What does this PR do?
Fixes https://github.com/meilisearch/meilisearch/issues/2140

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue?
- [ ] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Bruno Casali <brunoocasali@gmail.com>
This commit is contained in:
bors[bot] 2022-03-08 09:55:58 +00:00 committed by GitHub
commit a8d28e364d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -256,12 +256,21 @@ only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and undersco
Self::InvalidSortableAttribute { field, valid_fields } => { Self::InvalidSortableAttribute { field, valid_fields } => {
let valid_names = let valid_names =
valid_fields.iter().map(AsRef::as_ref).collect::<Vec<_>>().join(", "); valid_fields.iter().map(AsRef::as_ref).collect::<Vec<_>>().join(", ");
if valid_names.is_empty() {
write!(
f,
"Attribute `{}` is not sortable. This index does not have configured sortable attributes.",
field
)
} else {
write!( write!(
f, f,
"Attribute `{}` is not sortable. Available sortable attributes are: `{}`.", "Attribute `{}` is not sortable. Available sortable attributes are: `{}`.",
field, valid_names field, valid_names
) )
} }
}
Self::SortRankingRuleMissing => f.write_str( Self::SortRankingRuleMissing => f.write_str(
"The sort ranking rule must be specified in the \ "The sort ranking rule must be specified in the \
ranking rules settings to use the sort parameter at search time.", ranking rules settings to use the sort parameter at search time.",
@ -320,3 +329,19 @@ impl fmt::Display for SerializationError {
} }
impl StdError 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));
}
}