Fix Index name parsing error message to fit the specification

This commit is contained in:
ManyTheFish 2022-11-08 14:37:34 +01:00
parent 3999f74f78
commit 8bb260bf3e
7 changed files with 32 additions and 15 deletions

View File

@ -352,7 +352,7 @@ async fn error_add_api_key_invalid_parameters_indexes() {
assert_eq!(400, code, "{:?}", &response); assert_eq!(400, code, "{:?}", &response);
let expected_response = json!({ let expected_response = json!({
"message": r#"`{"name":"products"}` is not a valid index uid. It should be an array of string representing index names."#, "message": r#"`indexes` field value `{"name":"products"}` is invalid. It should be an array of string representing index names."#,
"code": "invalid_api_key_indexes", "code": "invalid_api_key_indexes",
"type": "invalid_request", "type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#invalid_api_key_indexes" "link": "https://docs.meilisearch.com/errors#invalid_api_key_indexes"
@ -377,7 +377,7 @@ async fn error_add_api_key_invalid_index_uids() {
let (response, code) = server.add_api_key(content).await; let (response, code) = server.add_api_key(content).await;
let expected_response = json!({ let expected_response = json!({
"message": r#"`["invalid index # / \\name with spaces"]` is not a valid index uid. It should be an array of string representing index names."#, "message": r#"`invalid index # / \name with spaces` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_)."#,
"code": "invalid_api_key_indexes", "code": "invalid_api_key_indexes",
"type": "invalid_request", "type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#invalid_api_key_indexes" "link": "https://docs.meilisearch.com/errors#invalid_api_key_indexes"

View File

@ -636,7 +636,7 @@ async fn error_document_add_create_index_bad_uid() {
let (response, code) = index.add_documents(json!([{"id": 1}]), None).await; let (response, code) = index.add_documents(json!([{"id": 1}]), None).await;
let expected_response = json!({ let expected_response = json!({
"message": "invalid index uid `883 fj!`, the uid must be an integer or a string containing only alphanumeric characters a-z A-Z 0-9, hyphens - and underscores _.", "message": "`883 fj!` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_).",
"code": "invalid_index_uid", "code": "invalid_index_uid",
"type": "invalid_request", "type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#invalid_index_uid" "link": "https://docs.meilisearch.com/errors#invalid_index_uid"

View File

@ -10,7 +10,7 @@ async fn error_document_update_create_index_bad_uid() {
let (response, code) = index.update_documents(json!([{"id": 1}]), None).await; let (response, code) = index.update_documents(json!([{"id": 1}]), None).await;
let expected_response = json!({ let expected_response = json!({
"message": "invalid index uid `883 fj!`, the uid must be an integer or a string containing only alphanumeric characters a-z A-Z 0-9, hyphens - and underscores _.", "message": "`883 fj!` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_).",
"code": "invalid_index_uid", "code": "invalid_index_uid",
"type": "invalid_request", "type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#invalid_index_uid" "link": "https://docs.meilisearch.com/errors#invalid_index_uid"

View File

@ -189,7 +189,7 @@ async fn error_create_with_invalid_index_uid() {
let (response, code) = index.create(None).await; let (response, code) = index.create(None).await;
let expected_response = json!({ let expected_response = json!({
"message": "invalid index uid `test test#!`, the uid must be an integer or a string containing only alphanumeric characters a-z A-Z 0-9, hyphens - and underscores _.", "message": "`test test#!` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_).",
"code": "invalid_index_uid", "code": "invalid_index_uid",
"type": "invalid_request", "type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#invalid_index_uid" "link": "https://docs.meilisearch.com/errors#invalid_index_uid"

View File

@ -182,7 +182,7 @@ async fn error_update_setting_unexisting_index_invalid_uid() {
assert_eq!(code, 400); assert_eq!(code, 400);
let expected = json!({ let expected = json!({
"message": "invalid index uid `test##! `, the uid must be an integer or a string containing only alphanumeric characters a-z A-Z 0-9, hyphens - and underscores _.", "message": "`test##! ` is not a valid index uid. Index uid can be an integer or a string containing only alphanumeric characters, hyphens (-) and underscores (_).",
"code": "invalid_index_uid", "code": "invalid_index_uid",
"type": "invalid_request", "type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#invalid_index_uid"}); "link": "https://docs.meilisearch.com/errors#invalid_index_uid"});

View File

@ -75,9 +75,9 @@ impl fmt::Display for IndexUidFormatError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( write!(
f, f,
"invalid index uid `{}`, the uid must be an integer \ "`{}` is not a valid index uid. Index uid can be an \
or a string containing only alphanumeric characters \ integer or a string containing only alphanumeric \
a-z A-Z 0-9, hyphens - and underscores _.", characters, hyphens (-) and underscores (_).",
self.invalid_uid, self.invalid_uid,
) )
} }

View File

@ -1,4 +1,5 @@
use std::hash::Hash; use std::hash::Hash;
use std::str::FromStr;
use enum_iterator::Sequence; use enum_iterator::Sequence;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -9,7 +10,7 @@ use time::{Date, OffsetDateTime, PrimitiveDateTime};
use uuid::Uuid; use uuid::Uuid;
use crate::error::{Code, ErrorCode}; use crate::error::{Code, ErrorCode};
use crate::index_uid::IndexUid; use crate::index_uid::{IndexUid, IndexUidFormatError};
use crate::star_or::StarOr; use crate::star_or::StarOr;
type Result<T> = std::result::Result<T, Error>; type Result<T> = std::result::Result<T, Error>;
@ -64,7 +65,15 @@ impl Key {
let indexes = value let indexes = value
.get("indexes") .get("indexes")
.map(|ind| { .map(|ind| {
from_value(ind.clone()).map_err(|_| Error::InvalidApiKeyIndexes(ind.clone())) from_value::<Vec<String>>(ind.clone())
// If it's not a vec of string, return an API key parsing error.
.map_err(|_| Error::InvalidApiKeyIndexes(ind.clone()))
.and_then(|ind| {
ind.into_iter()
// If it's not a valid Index uid, return an Index Uid parsing error.
.map(|i| StarOr::<IndexUid>::from_str(&i).map_err(Error::from))
.collect()
})
}) })
.ok_or(Error::MissingParameter("indexes"))??; .ok_or(Error::MissingParameter("indexes"))??;
@ -339,10 +348,10 @@ pub enum Error {
MissingParameter(&'static str), MissingParameter(&'static str),
#[error("`actions` field value `{0}` is invalid. It should be an array of string representing action names.")] #[error("`actions` field value `{0}` is invalid. It should be an array of string representing action names.")]
InvalidApiKeyActions(Value), InvalidApiKeyActions(Value),
#[error( #[error("`indexes` field value `{0}` is invalid. It should be an array of string representing index names.")]
"`{0}` is not a valid index uid. It should be an array of string representing index names."
)]
InvalidApiKeyIndexes(Value), InvalidApiKeyIndexes(Value),
#[error("{0}")]
InvalidApiKeyIndexUid(IndexUidFormatError),
#[error("`expiresAt` field value `{0}` is invalid. It should follow the RFC 3339 format to represents a date or datetime in the future or specified as a null value. e.g. 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'.")] #[error("`expiresAt` field value `{0}` is invalid. It should follow the RFC 3339 format to represents a date or datetime in the future or specified as a null value. e.g. 'YYYY-MM-DD' or 'YYYY-MM-DD HH:MM:SS'.")]
InvalidApiKeyExpiresAt(Value), InvalidApiKeyExpiresAt(Value),
#[error("`description` field value `{0}` is invalid. It should be a string or specified as a null value.")] #[error("`description` field value `{0}` is invalid. It should be a string or specified as a null value.")]
@ -357,12 +366,20 @@ pub enum Error {
ImmutableField(String), ImmutableField(String),
} }
impl From<IndexUidFormatError> for Error {
fn from(e: IndexUidFormatError) -> Self {
Self::InvalidApiKeyIndexUid(e)
}
}
impl ErrorCode for Error { impl ErrorCode for Error {
fn error_code(&self) -> Code { fn error_code(&self) -> Code {
match self { match self {
Self::MissingParameter(_) => Code::MissingParameter, Self::MissingParameter(_) => Code::MissingParameter,
Self::InvalidApiKeyActions(_) => Code::InvalidApiKeyActions, Self::InvalidApiKeyActions(_) => Code::InvalidApiKeyActions,
Self::InvalidApiKeyIndexes(_) => Code::InvalidApiKeyIndexes, Self::InvalidApiKeyIndexes(_) | Self::InvalidApiKeyIndexUid(_) => {
Code::InvalidApiKeyIndexes
}
Self::InvalidApiKeyExpiresAt(_) => Code::InvalidApiKeyExpiresAt, Self::InvalidApiKeyExpiresAt(_) => Code::InvalidApiKeyExpiresAt,
Self::InvalidApiKeyDescription(_) => Code::InvalidApiKeyDescription, Self::InvalidApiKeyDescription(_) => Code::InvalidApiKeyDescription,
Self::InvalidApiKeyName(_) => Code::InvalidApiKeyName, Self::InvalidApiKeyName(_) => Code::InvalidApiKeyName,