Move index swap error handling from meilisearch-http to index-scheduler

And make index_not_found error asynchronous, since we can't know
whether the index will exist by the time the index swap task is
processed.

Improve the index-swap test to verify that future tasks are not swapped
and to test the new error messages that were introduced.
This commit is contained in:
Loïc Lecrenier 2022-10-27 09:41:32 +02:00
parent b44cc62320
commit 78ffa00f98
11 changed files with 305 additions and 63 deletions

View file

@ -24,20 +24,6 @@ pub enum MeilisearchHttpError {
MissingPayload(PayloadType),
#[error("The provided payload reached the size limit.")]
PayloadTooLarge,
#[error(
"Indexes {} not found.",
.0.iter().map(|s| format!("`{}`", s)).collect::<Vec<_>>().join(", ")
)]
IndexesNotFound(Vec<String>),
#[error(
"Indexes must be declared only once during a swap. `{0}` was specified several times."
)]
SwapDuplicateIndexFound(String),
#[error(
"Indexes must be declared only once during a swap. {} were specified several times.",
.0.iter().map(|s| format!("`{}`", s)).collect::<Vec<_>>().join(", ")
)]
SwapDuplicateIndexesFound(Vec<String>),
#[error("Two indexes must be given for each swap. The list `{:?}` contains {} indexes.",
.0, .0.len()
)]
@ -71,9 +57,6 @@ impl ErrorCode for MeilisearchHttpError {
MeilisearchHttpError::DocumentNotFound(_) => Code::DocumentNotFound,
MeilisearchHttpError::InvalidExpression(_, _) => Code::Filter,
MeilisearchHttpError::PayloadTooLarge => Code::PayloadTooLarge,
MeilisearchHttpError::IndexesNotFound(_) => Code::IndexNotFound,
MeilisearchHttpError::SwapDuplicateIndexFound(_) => Code::DuplicateIndexFound,
MeilisearchHttpError::SwapDuplicateIndexesFound(_) => Code::DuplicateIndexFound,
MeilisearchHttpError::SwapIndexPayloadWrongLength(_) => Code::BadRequest,
MeilisearchHttpError::IndexUid(e) => e.error_code(),
MeilisearchHttpError::SerdeJson(_) => Code::Internal,

View file

@ -1,5 +1,3 @@
use std::collections::BTreeSet;
use actix_web::web::Data;
use actix_web::{web, HttpResponse};
use index_scheduler::IndexScheduler;
@ -29,9 +27,6 @@ pub async fn swap_indexes(
let search_rules = &index_scheduler.filters().search_rules;
let mut swaps = vec![];
let mut indexes_set = BTreeSet::<String>::default();
let mut unauthorized_indexes = BTreeSet::new();
let mut duplicate_indexes = BTreeSet::new();
for SwapIndexesPayload { indexes } in params.into_inner().into_iter() {
let (lhs, rhs) = match indexes.as_slice() {
[lhs, rhs] => (lhs, rhs),
@ -39,34 +34,10 @@ pub async fn swap_indexes(
return Err(MeilisearchHttpError::SwapIndexPayloadWrongLength(indexes).into());
}
};
if !search_rules.is_index_authorized(lhs) {
unauthorized_indexes.insert(lhs.clone());
if !search_rules.is_index_authorized(lhs) || !search_rules.is_index_authorized(rhs) {
return Err(AuthenticationError::InvalidToken.into());
}
if !search_rules.is_index_authorized(rhs) {
unauthorized_indexes.insert(rhs.clone());
}
swaps.push(IndexSwap { indexes: (lhs.clone(), rhs.clone()) });
let is_unique_index_lhs = indexes_set.insert(lhs.clone());
if !is_unique_index_lhs {
duplicate_indexes.insert(lhs.clone());
}
let is_unique_index_rhs = indexes_set.insert(rhs.clone());
if !is_unique_index_rhs {
duplicate_indexes.insert(rhs.clone());
}
}
if !duplicate_indexes.is_empty() {
let duplicate_indexes: Vec<_> = duplicate_indexes.into_iter().collect();
if let [index] = duplicate_indexes.as_slice() {
return Err(MeilisearchHttpError::SwapDuplicateIndexFound(index.clone()).into());
} else {
return Err(MeilisearchHttpError::SwapDuplicateIndexesFound(duplicate_indexes).into());
}
}
if !unauthorized_indexes.is_empty() {
return Err(AuthenticationError::InvalidToken.into());
}
let task = KindWithContent::IndexSwap { swaps };

View file

@ -705,7 +705,7 @@ async fn test_summarized_index_swap() {
]
},
"error": {
"message": "Index `doggos` not found.",
"message": "Indexes `cattos`, `doggos` not found.",
"code": "index_not_found",
"type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#index_not_found"