move the error types to meilisearch-http

This commit is contained in:
Irevoire 2022-10-24 17:51:30 +02:00 committed by Clément Renault
parent 2808be9d45
commit 3ea489421e
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
2 changed files with 29 additions and 65 deletions

View File

@ -24,6 +24,20 @@ 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(transparent)]
IndexUid(#[from] IndexUidFormatError),
#[error(transparent)]
@ -53,6 +67,9 @@ 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::IndexUid(e) => e.error_code(),
MeilisearchHttpError::SerdeJson(_) => Code::Internal,
MeilisearchHttpError::HeedError(_) => Code::Internal,

View File

@ -7,7 +7,7 @@ use meilisearch_types::error::ResponseError;
use meilisearch_types::tasks::KindWithContent;
use serde::Deserialize;
use self::errors::{DuplicateSwappedIndexError, IndexesNotFoundError};
use crate::error::MeilisearchHttpError;
use crate::extractors::authentication::policies::*;
use crate::extractors::authentication::GuardedData;
use crate::extractors::sequential_extractor::SeqHandler;
@ -53,13 +53,20 @@ pub async fn swap_indexes(
}
}
if !duplicate_indexes.is_empty() {
return Err(DuplicateSwappedIndexError {
indexes: duplicate_indexes.into_iter().collect(),
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());
}
.into());
}
if !unknown_indexes.is_empty() {
return Err(IndexesNotFoundError { indexes: unknown_indexes.into_iter().collect() }.into());
let unknown_indexes: Vec<_> = unknown_indexes.into_iter().collect();
if let [index] = unknown_indexes.as_slice() {
return Err(index_scheduler::Error::IndexNotFound(index.clone()).into());
} else {
return Err(MeilisearchHttpError::IndexesNotFound(unknown_indexes).into());
}
}
let task = KindWithContent::IndexSwap { swaps };
@ -69,63 +76,3 @@ pub async fn swap_indexes(
Ok(HttpResponse::Accepted().json(task_view))
}
pub mod errors {
use std::fmt::Display;
use meilisearch_types::error::{Code, ErrorCode};
#[derive(Debug)]
pub struct IndexesNotFoundError {
pub indexes: Vec<String>,
}
impl Display for IndexesNotFoundError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.indexes.len() == 1 {
write!(f, "Index `{}` not found,", self.indexes[0])?;
} else {
write!(f, "Indexes `{}`", self.indexes[0])?;
for index in self.indexes.iter().skip(1) {
write!(f, ", `{}`", index)?;
}
write!(f, "not found.")?;
}
Ok(())
}
}
impl std::error::Error for IndexesNotFoundError {}
impl ErrorCode for IndexesNotFoundError {
fn error_code(&self) -> Code {
Code::IndexNotFound
}
}
#[derive(Debug)]
pub struct DuplicateSwappedIndexError {
pub indexes: Vec<String>,
}
impl Display for DuplicateSwappedIndexError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.indexes.len() == 1 {
write!(f, "Indexes must be declared only once during a swap. `{}` was specified several times.", self.indexes[0])?;
} else {
write!(
f,
"Indexes must be declared only once during a swap. `{}`",
self.indexes[0]
)?;
for index in self.indexes.iter().skip(1) {
write!(f, ", `{}`", index)?;
}
write!(f, "were specified several times.")?;
}
Ok(())
}
}
impl std::error::Error for DuplicateSwappedIndexError {}
impl ErrorCode for DuplicateSwappedIndexError {
fn error_code(&self) -> Code {
Code::DuplicateIndexFound
}
}
}