373: Improve error message for bad sort syntax with geosearch r=Kerollmops a=irevoire

`@Kerollmops` This should be the last PR for the geosearch and error handling, sorry for doing it in so many steps 😬 

Co-authored-by: Tamo <tamo@meilisearch.com>
This commit is contained in:
bors[bot] 2021-09-28 12:39:32 +00:00 committed by GitHub
commit 3a12f5887e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -141,6 +141,7 @@ impl FromStr for AscDesc {
#[derive(Debug)] #[derive(Debug)]
pub enum SortError { pub enum SortError {
BadGeoPointUsage { name: String },
InvalidName { name: String }, InvalidName { name: String },
ReservedName { name: String }, ReservedName { name: String },
ReservedNameForSettings { name: String }, ReservedNameForSettings { name: String },
@ -151,6 +152,9 @@ impl From<AscDescError> for SortError {
fn from(error: AscDescError) -> Self { fn from(error: AscDescError) -> Self {
match error { match error {
AscDescError::InvalidSyntax { name } => SortError::InvalidName { name }, AscDescError::InvalidSyntax { name } => SortError::InvalidName { name },
AscDescError::ReservedKeyword { name } if name.starts_with("_geoPoint") => {
SortError::BadGeoPointUsage { name }
}
AscDescError::ReservedKeyword { name } if &name == "_geo" => { AscDescError::ReservedKeyword { name } if &name == "_geo" => {
SortError::ReservedNameForSettings { name: "_geoPoint".to_string() } SortError::ReservedNameForSettings { name: "_geoPoint".to_string() }
} }
@ -165,6 +169,14 @@ impl From<AscDescError> for SortError {
impl fmt::Display for SortError { impl fmt::Display for SortError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
Self::BadGeoPointUsage { name } => {
write!(
f,
"invalid syntax for the `_geoPoint` parameter: `{}`. \
Usage: `_geoPoint(latitude, longitude):asc`",
name
)
}
Self::InvalidName { name } => { Self::InvalidName { name } => {
write!(f, "invalid syntax for the sort parameter {}", name) write!(f, "invalid syntax for the sort parameter {}", name)
} }