factorize internal error macro

This commit is contained in:
marin postma 2021-06-15 17:55:27 +02:00
parent 02277ec2cf
commit 0d3fb5ee0d
No known key found for this signature in database
GPG Key ID: 6088B7721C3E39F9
11 changed files with 62 additions and 84 deletions

View File

@ -127,3 +127,15 @@ where
let error = PayloadError(err); let error = PayloadError(err);
error.into() error.into()
} }
macro_rules! internal_error {
($target:ty : $($other:path), *) => {
$(
impl From<$other> for $target {
fn from(other: $other) -> Self {
Self::Internal(Box::new(other))
}
}
)*
}
}

View File

@ -15,19 +15,13 @@ pub enum IndexError {
Facet(#[from] FacetError), Facet(#[from] FacetError),
} }
macro_rules! internal_error { internal_error!(
($($other:path), *) => { IndexError:
$( std::io::Error,
impl From<$other> for IndexError { heed::Error,
fn from(other: $other) -> Self { fst::Error,
Self::Internal(Box::new(other)) serde_json::Error
} );
}
)*
}
}
internal_error!(std::io::Error, heed::Error, fst::Error, serde_json::Error);
impl ErrorCode for IndexError { impl ErrorCode for IndexError {
fn error_code(&self) -> Code { fn error_code(&self) -> Code {

View File

@ -1,8 +1,7 @@
use meilisearch_error::{Code, ErrorCode}; use meilisearch_error::{Code, ErrorCode};
use crate::index_controller::{ use crate::index_controller::update_actor::error::UpdateActorError;
update_actor::error::UpdateActorError, uuid_resolver::UuidResolverError, use crate::index_controller::uuid_resolver::error::UuidResolverError;
};
pub type Result<T> = std::result::Result<T, DumpActorError>; pub type Result<T> = std::result::Result<T, DumpActorError>;

View File

@ -1,19 +1,15 @@
use std::error::Error;
use meilisearch_error::Code; use meilisearch_error::Code;
use meilisearch_error::ErrorCode; use meilisearch_error::ErrorCode;
use super::dump_actor::error::DumpActorError; use super::dump_actor::error::DumpActorError;
use super::index_actor::error::IndexActorError; use super::index_actor::error::IndexActorError;
use super::update_actor::error::UpdateActorError; use super::update_actor::error::UpdateActorError;
use super::uuid_resolver::UuidResolverError; use super::uuid_resolver::error::UuidResolverError;
pub type Result<T> = std::result::Result<T, IndexControllerError>; pub type Result<T> = std::result::Result<T, IndexControllerError>;
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum IndexControllerError { pub enum IndexControllerError {
#[error("Internal error: {0}")]
Internal(Box<dyn Error>),
#[error("Missing index uid")] #[error("Missing index uid")]
MissingUid, MissingUid,
#[error("index resolution error: {0}")] #[error("index resolution error: {0}")]
@ -29,7 +25,6 @@ pub enum IndexControllerError {
impl ErrorCode for IndexControllerError { impl ErrorCode for IndexControllerError {
fn error_code(&self) -> Code { fn error_code(&self) -> Code {
match self { match self {
IndexControllerError::Internal(_) => Code::Internal,
IndexControllerError::MissingUid => Code::InvalidIndexUid, IndexControllerError::MissingUid => Code::InvalidIndexUid,
IndexControllerError::Uuid(e) => e.error_code(), IndexControllerError::Uuid(e) => e.error_code(),
IndexControllerError::IndexActor(e) => e.error_code(), IndexControllerError::IndexActor(e) => e.error_code(),

View File

@ -19,7 +19,7 @@ use index_actor::IndexActorHandle;
use snapshot::{load_snapshot, SnapshotService}; use snapshot::{load_snapshot, SnapshotService};
use update_actor::UpdateActorHandle; use update_actor::UpdateActorHandle;
pub use updates::*; pub use updates::*;
use uuid_resolver::{UuidResolverError, UuidResolverHandle}; use uuid_resolver::{error::UuidResolverError, UuidResolverHandle};
use crate::index::{Checked, Document, SearchQuery, SearchResult, Settings}; use crate::index::{Checked, Document, SearchQuery, SearchResult, Settings};
use crate::option::Opt; use crate::option::Opt;

View File

@ -20,18 +20,6 @@ pub enum UpdateActorError {
FatalUpdateStoreError, FatalUpdateStoreError,
} }
macro_rules! internal_error {
($($other:path), *) => {
$(
impl From<$other> for UpdateActorError {
fn from(other: $other) -> Self {
Self::Internal(Box::new(other))
}
}
)*
}
}
impl<T> From<tokio::sync::mpsc::error::SendError<T>> for UpdateActorError { impl<T> From<tokio::sync::mpsc::error::SendError<T>> for UpdateActorError {
fn from(_: tokio::sync::mpsc::error::SendError<T>) -> Self { fn from(_: tokio::sync::mpsc::error::SendError<T>) -> Self {
Self::FatalUpdateStoreError Self::FatalUpdateStoreError

View File

@ -4,7 +4,7 @@ use log::{info, warn};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use uuid::Uuid; use uuid::Uuid;
use super::{Result, UuidResolveMsg, UuidResolverError, UuidStore}; use super::{Result, UuidResolveMsg, error::UuidResolverError, UuidStore};
pub struct UuidResolverActor<S> { pub struct UuidResolverActor<S> {
inbox: mpsc::Receiver<UuidResolveMsg>, inbox: mpsc::Receiver<UuidResolveMsg>,

View File

@ -0,0 +1,34 @@
use meilisearch_error::{Code, ErrorCode};
pub type Result<T> = std::result::Result<T, UuidResolverError>;
#[derive(Debug, thiserror::Error)]
pub enum UuidResolverError {
#[error("Name already exist.")]
NameAlreadyExist,
#[error("Index \"{0}\" doesn't exist.")]
UnexistingIndex(String),
#[error("Badly formatted index uid: {0}")]
BadlyFormatted(String),
#[error("Internal error resolving index uid: {0}")]
Internal(Box<dyn std::error::Error + Sync + Send + 'static>),
}
internal_error!(
UuidResolverError: heed::Error,
uuid::Error,
std::io::Error,
tokio::task::JoinError,
serde_json::Error
);
impl ErrorCode for UuidResolverError {
fn error_code(&self) -> Code {
match self {
UuidResolverError::NameAlreadyExist => Code::IndexAlreadyExists,
UuidResolverError::UnexistingIndex(_) => Code::IndexNotFound,
UuidResolverError::BadlyFormatted(_) => Code::InvalidIndexUid,
UuidResolverError::Internal(_) => Code::Internal,
}
}
}

View File

@ -1,4 +1,5 @@
mod actor; mod actor;
pub mod error;
mod handle_impl; mod handle_impl;
mod message; mod message;
pub mod store; pub mod store;
@ -6,14 +7,12 @@ pub mod store;
use std::collections::HashSet; use std::collections::HashSet;
use std::path::PathBuf; use std::path::PathBuf;
use meilisearch_error::Code;
use meilisearch_error::ErrorCode;
use thiserror::Error;
use uuid::Uuid; use uuid::Uuid;
use actor::UuidResolverActor; use actor::UuidResolverActor;
use message::UuidResolveMsg; use message::UuidResolveMsg;
use store::UuidStore; use store::UuidStore;
use error::Result;
#[cfg(test)] #[cfg(test)]
use mockall::automock; use mockall::automock;
@ -23,7 +22,6 @@ pub use store::HeedUuidStore;
const UUID_STORE_SIZE: usize = 1_073_741_824; //1GiB const UUID_STORE_SIZE: usize = 1_073_741_824; //1GiB
pub type Result<T> = std::result::Result<T, UuidResolverError>;
#[async_trait::async_trait] #[async_trait::async_trait]
#[cfg_attr(test, automock)] #[cfg_attr(test, automock)]
@ -36,46 +34,3 @@ pub trait UuidResolverHandle {
async fn get_size(&self) -> Result<u64>; async fn get_size(&self) -> Result<u64>;
async fn dump(&self, path: PathBuf) -> Result<HashSet<Uuid>>; async fn dump(&self, path: PathBuf) -> Result<HashSet<Uuid>>;
} }
#[derive(Debug, Error)]
pub enum UuidResolverError {
#[error("Name already exist.")]
NameAlreadyExist,
#[error("Index \"{0}\" doesn't exist.")]
UnexistingIndex(String),
#[error("Badly formatted index uid: {0}")]
BadlyFormatted(String),
#[error("Internal error resolving index uid: {0}")]
Internal(Box<dyn std::error::Error + Sync + Send + 'static>),
}
macro_rules! internal_error {
($($other:path), *) => {
$(
impl From<$other> for UuidResolverError {
fn from(other: $other) -> Self {
Self::Internal(Box::new(other))
}
}
)*
}
}
internal_error!(
heed::Error,
uuid::Error,
std::io::Error,
tokio::task::JoinError,
serde_json::Error
);
impl ErrorCode for UuidResolverError {
fn error_code(&self) -> Code {
match self {
UuidResolverError::NameAlreadyExist => Code::IndexAlreadyExists,
UuidResolverError::UnexistingIndex(_) => Code::IndexNotFound,
UuidResolverError::BadlyFormatted(_) => Code::InvalidIndexUid,
UuidResolverError::Internal(_) => Code::Internal,
}
}
}

View File

@ -8,7 +8,7 @@ use heed::{CompactionOption, Database, Env, EnvOpenOptions};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use uuid::Uuid; use uuid::Uuid;
use super::{Result, UuidResolverError, UUID_STORE_SIZE}; use super::{Result, error::UuidResolverError, UUID_STORE_SIZE};
use crate::helpers::EnvSizer; use crate::helpers::EnvSizer;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]

View File

@ -1,4 +1,5 @@
pub mod data; pub mod data;
#[macro_use]
pub mod error; pub mod error;
pub mod helpers; pub mod helpers;
mod index; mod index;