From 702041b7e19d4af4daef712811514267bfc2cdf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Wed, 26 Apr 2023 18:09:24 +0200 Subject: [PATCH] Improve the returned errors from the facet-search route --- meilisearch-types/src/error.rs | 4 ++++ meilisearch/src/routes/indexes/facet_search.rs | 4 ++-- milli/src/error.rs | 10 ++++++++++ milli/src/search/mod.rs | 3 +-- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/meilisearch-types/src/error.rs b/meilisearch-types/src/error.rs index 09c8b7b8c..1a87b2707 100644 --- a/meilisearch-types/src/error.rs +++ b/meilisearch-types/src/error.rs @@ -242,11 +242,14 @@ InvalidSearchMatchingStrategy , InvalidRequest , BAD_REQUEST ; InvalidSearchOffset , InvalidRequest , BAD_REQUEST ; InvalidSearchPage , InvalidRequest , BAD_REQUEST ; InvalidSearchQ , InvalidRequest , BAD_REQUEST ; +InvalidFacetSearchQuery , InvalidRequest , BAD_REQUEST ; +InvalidFacetSearchName , InvalidRequest , BAD_REQUEST ; InvalidSearchVector , InvalidRequest , BAD_REQUEST ; InvalidSearchShowMatchesPosition , InvalidRequest , BAD_REQUEST ; InvalidSearchShowRankingScore , InvalidRequest , BAD_REQUEST ; InvalidSearchShowRankingScoreDetails , InvalidRequest , BAD_REQUEST ; InvalidSearchSort , InvalidRequest , BAD_REQUEST ; +InvalidSearchFacet , InvalidRequest , BAD_REQUEST ; InvalidSettingsDisplayedAttributes , InvalidRequest , BAD_REQUEST ; InvalidSettingsDistinctAttribute , InvalidRequest , BAD_REQUEST ; InvalidSettingsFaceting , InvalidRequest , BAD_REQUEST ; @@ -340,6 +343,7 @@ impl ErrorCode for milli::Error { UserError::InvalidSearchableAttribute { .. } => { Code::InvalidAttributesToSearchOn } + UserError::InvalidSearchFacet { .. } => Code::InvalidSearchFacet, UserError::CriterionError(_) => Code::InvalidSettingsRankingRules, UserError::InvalidGeoField { .. } => Code::InvalidDocumentGeoField, UserError::InvalidVectorDimensions { .. } => Code::InvalidVectorDimensions, diff --git a/meilisearch/src/routes/indexes/facet_search.rs b/meilisearch/src/routes/indexes/facet_search.rs index 3f97e2858..3633057e8 100644 --- a/meilisearch/src/routes/indexes/facet_search.rs +++ b/meilisearch/src/routes/indexes/facet_search.rs @@ -28,9 +28,9 @@ pub fn configure(cfg: &mut web::ServiceConfig) { #[derive(Debug, Clone, Default, PartialEq, Eq, deserr::Deserr)] #[deserr(error = DeserrJsonError, rename_all = camelCase, deny_unknown_fields)] pub struct FacetSearchQuery { - #[deserr(default, error = DeserrJsonError)] + #[deserr(default, error = DeserrJsonError)] pub facet_query: Option, - #[deserr(default, error = DeserrJsonError)] + #[deserr(error = DeserrJsonError)] pub facet_name: String, #[deserr(default, error = DeserrJsonError)] pub q: Option, diff --git a/milli/src/error.rs b/milli/src/error.rs index 61597faf3..81be9a215 100644 --- a/milli/src/error.rs +++ b/milli/src/error.rs @@ -128,6 +128,16 @@ only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and undersco } )] InvalidSortableAttribute { field: String, valid_fields: BTreeSet }, + #[error("Attribute `{}` is not filterable. {}", + .field, + match .valid_fields.is_empty() { + true => "This index does not have configured filterable attributes.".to_string(), + false => format!("Available filterable attributes are: `{}`.", + valid_fields.iter().map(AsRef::as_ref).collect::>().join(", ") + ), + } + )] + InvalidSearchFacet { field: String, valid_fields: BTreeSet }, #[error("Attribute `{}` is not searchable. Available searchable attributes are: `{}{}`.", .field, .valid_fields.iter().map(AsRef::as_ref).collect::>().join(", "), diff --git a/milli/src/search/mod.rs b/milli/src/search/mod.rs index 211e6a0e8..87a46229d 100644 --- a/milli/src/search/mod.rs +++ b/milli/src/search/mod.rs @@ -263,8 +263,7 @@ impl<'a> SearchForFacetValue<'a> { let filterable_fields = index.filterable_fields(rtxn)?; if !filterable_fields.contains(&self.facet) { - // TODO create a new type of error - return Err(UserError::InvalidSortableAttribute { + return Err(UserError::InvalidSearchFacet { field: self.facet.clone(), valid_fields: filterable_fields.into_iter().collect(), })?;