2021-09-24 11:53:11 +02:00
|
|
|
use std::fmt;
|
|
|
|
|
2022-06-06 12:38:46 +02:00
|
|
|
use meilisearch_types::error::{Code, ErrorCode};
|
|
|
|
use meilisearch_types::index_uid::IndexUidFormatError;
|
|
|
|
use meilisearch_types::internal_error;
|
2021-09-24 11:53:11 +02:00
|
|
|
use tokio::sync::mpsc::error::SendError as MpscSendError;
|
|
|
|
use tokio::sync::oneshot::error::RecvError as OneshotRecvError;
|
2021-10-26 19:36:48 +02:00
|
|
|
use uuid::Uuid;
|
2021-09-24 11:53:11 +02:00
|
|
|
|
2022-06-02 11:00:07 +02:00
|
|
|
use crate::{error::MilliError, index::error::IndexError, update_file_store::UpdateFileStoreError};
|
2021-09-24 11:53:11 +02:00
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, IndexResolverError>;
|
|
|
|
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum IndexResolverError {
|
|
|
|
#[error("{0}")]
|
|
|
|
IndexError(#[from] IndexError),
|
2021-10-26 19:36:48 +02:00
|
|
|
#[error("Index `{0}` already exists.")]
|
|
|
|
IndexAlreadyExists(String),
|
|
|
|
#[error("Index `{0}` not found.")]
|
2021-09-24 11:53:11 +02:00
|
|
|
UnexistingIndex(String),
|
|
|
|
#[error("A primary key is already present. It's impossible to update it")]
|
|
|
|
ExistingPrimaryKey,
|
2021-11-03 14:25:49 +01:00
|
|
|
#[error("An internal error has occurred. `{0}`.")]
|
2021-09-24 11:53:11 +02:00
|
|
|
Internal(Box<dyn std::error::Error + Send + Sync + 'static>),
|
2021-11-03 14:25:49 +01:00
|
|
|
#[error("The creation of the `{0}` index has failed due to `Index uuid is already assigned`.")]
|
2021-10-28 15:42:42 +02:00
|
|
|
UuidAlreadyExists(Uuid),
|
2021-09-24 11:53:11 +02:00
|
|
|
#[error("{0}")]
|
|
|
|
Milli(#[from] milli::Error),
|
2022-06-06 12:38:46 +02:00
|
|
|
#[error("{0}")]
|
|
|
|
BadlyFormatted(#[from] IndexUidFormatError),
|
2021-09-24 11:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> From<MpscSendError<T>> for IndexResolverError
|
2021-09-28 22:22:59 +02:00
|
|
|
where
|
|
|
|
T: Send + Sync + 'static + fmt::Debug,
|
2021-09-24 11:53:11 +02:00
|
|
|
{
|
|
|
|
fn from(other: tokio::sync::mpsc::error::SendError<T>) -> Self {
|
|
|
|
Self::Internal(Box::new(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<OneshotRecvError> for IndexResolverError {
|
|
|
|
fn from(other: tokio::sync::oneshot::error::RecvError) -> Self {
|
|
|
|
Self::Internal(Box::new(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal_error!(
|
2022-03-16 18:15:58 +05:30
|
|
|
IndexResolverError: milli::heed::Error,
|
2021-09-24 11:53:11 +02:00
|
|
|
uuid::Error,
|
|
|
|
std::io::Error,
|
|
|
|
tokio::task::JoinError,
|
2022-06-02 11:00:07 +02:00
|
|
|
serde_json::Error,
|
|
|
|
UpdateFileStoreError
|
2021-09-24 11:53:11 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
impl ErrorCode for IndexResolverError {
|
|
|
|
fn error_code(&self) -> Code {
|
|
|
|
match self {
|
|
|
|
IndexResolverError::IndexError(e) => e.error_code(),
|
2021-10-26 19:36:48 +02:00
|
|
|
IndexResolverError::IndexAlreadyExists(_) => Code::IndexAlreadyExists,
|
2021-09-24 11:53:11 +02:00
|
|
|
IndexResolverError::UnexistingIndex(_) => Code::IndexNotFound,
|
|
|
|
IndexResolverError::ExistingPrimaryKey => Code::PrimaryKeyAlreadyPresent,
|
|
|
|
IndexResolverError::Internal(_) => Code::Internal,
|
2021-11-03 14:25:49 +01:00
|
|
|
IndexResolverError::UuidAlreadyExists(_) => Code::CreateIndex,
|
2021-09-24 11:53:11 +02:00
|
|
|
IndexResolverError::Milli(e) => MilliError(e).error_code(),
|
|
|
|
IndexResolverError::BadlyFormatted(_) => Code::InvalidIndexUid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|