mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-03 11:57:07 +02:00
Merge #3401
3401: improve the error messages for the immutable fields r=dureuill a=irevoire Fix https://github.com/meilisearch/meilisearch/issues/3400 Co-authored-by: Tamo <tamo@meilisearch.com>
This commit is contained in:
commit
8fb685f5aa
4 changed files with 43 additions and 33 deletions
|
@ -9,7 +9,7 @@ We try to:
|
|||
use deserr::{ErrorKind, IntoValue, ValueKind, ValuePointerRef};
|
||||
|
||||
use super::{DeserrJsonError, DeserrQueryParamError};
|
||||
use crate::error::ErrorCode;
|
||||
use crate::error::{Code, ErrorCode};
|
||||
|
||||
/// Return a description of the given location in a Json, preceded by the given article.
|
||||
/// e.g. `at .key1[8].key2`. If the location is the origin, the given article will not be
|
||||
|
@ -179,6 +179,19 @@ impl<C: Default + ErrorCode> deserr::DeserializeError for DeserrJsonError<C> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn immutable_field_error(field: &str, accepted: &[&str], code: Code) -> DeserrJsonError {
|
||||
let msg = format!(
|
||||
"Immutable field `{field}`: expected one of {}",
|
||||
accepted
|
||||
.iter()
|
||||
.map(|accepted| format!("`{}`", accepted))
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
);
|
||||
|
||||
DeserrJsonError::new(msg, code)
|
||||
}
|
||||
|
||||
/// Return a description of the given location in query parameters, preceded by the
|
||||
/// given article. e.g. `at key5[2]`. If the location is the origin, the given article
|
||||
/// will not be included in the description.
|
||||
|
|
|
@ -11,6 +11,7 @@ use time::macros::{format_description, time};
|
|||
use time::{Date, OffsetDateTime, PrimitiveDateTime};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::deserr::error_messages::immutable_field_error;
|
||||
use crate::deserr::DeserrJsonError;
|
||||
use crate::error::deserr_codes::*;
|
||||
use crate::error::{unwrap_any, Code, ParseOffsetDateTimeError};
|
||||
|
@ -57,22 +58,19 @@ fn deny_immutable_fields_api_key(
|
|||
accepted: &[&str],
|
||||
location: ValuePointerRef,
|
||||
) -> DeserrJsonError {
|
||||
let mut error = unwrap_any(DeserrJsonError::<BadRequest>::error::<Infallible>(
|
||||
None,
|
||||
deserr::ErrorKind::UnknownKey { key: field, accepted },
|
||||
location,
|
||||
));
|
||||
|
||||
error.code = match field {
|
||||
"uid" => Code::ImmutableApiKeyUid,
|
||||
"actions" => Code::ImmutableApiKeyActions,
|
||||
"indexes" => Code::ImmutableApiKeyIndexes,
|
||||
"expiresAt" => Code::ImmutableApiKeyExpiresAt,
|
||||
"createdAt" => Code::ImmutableApiKeyCreatedAt,
|
||||
"updatedAt" => Code::ImmutableApiKeyUpdatedAt,
|
||||
_ => Code::BadRequest,
|
||||
};
|
||||
error
|
||||
match field {
|
||||
"uid" => immutable_field_error(field, accepted, Code::ImmutableApiKeyUid),
|
||||
"actions" => immutable_field_error(field, accepted, Code::ImmutableApiKeyActions),
|
||||
"indexes" => immutable_field_error(field, accepted, Code::ImmutableApiKeyIndexes),
|
||||
"expiresAt" => immutable_field_error(field, accepted, Code::ImmutableApiKeyExpiresAt),
|
||||
"createdAt" => immutable_field_error(field, accepted, Code::ImmutableApiKeyCreatedAt),
|
||||
"updatedAt" => immutable_field_error(field, accepted, Code::ImmutableApiKeyUpdatedAt),
|
||||
_ => unwrap_any(DeserrJsonError::<BadRequest>::error::<Infallible>(
|
||||
None,
|
||||
deserr::ErrorKind::UnknownKey { key: field, accepted },
|
||||
location,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, DeserializeFromValue)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue