From 0d3fb5ee0ddf620dff9036012bc42d1f7fc9619d Mon Sep 17 00:00:00 2001 From: marin postma Date: Tue, 15 Jun 2021 17:55:27 +0200 Subject: [PATCH] factorize internal error macro --- meilisearch-http/src/error.rs | 12 +++++ meilisearch-http/src/index/error.rs | 20 +++----- .../src/index_controller/dump_actor/error.rs | 5 +- .../src/index_controller/error.rs | 7 +-- meilisearch-http/src/index_controller/mod.rs | 2 +- .../index_controller/update_actor/error.rs | 12 ----- .../index_controller/uuid_resolver/actor.rs | 2 +- .../index_controller/uuid_resolver/error.rs | 34 +++++++++++++ .../src/index_controller/uuid_resolver/mod.rs | 49 +------------------ .../index_controller/uuid_resolver/store.rs | 2 +- meilisearch-http/src/lib.rs | 1 + 11 files changed, 62 insertions(+), 84 deletions(-) create mode 100644 meilisearch-http/src/index_controller/uuid_resolver/error.rs diff --git a/meilisearch-http/src/error.rs b/meilisearch-http/src/error.rs index a853fd8cf..7468f0630 100644 --- a/meilisearch-http/src/error.rs +++ b/meilisearch-http/src/error.rs @@ -127,3 +127,15 @@ where let error = PayloadError(err); 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)) + } + } + )* + } +} diff --git a/meilisearch-http/src/index/error.rs b/meilisearch-http/src/index/error.rs index 25d017547..32f13a143 100644 --- a/meilisearch-http/src/index/error.rs +++ b/meilisearch-http/src/index/error.rs @@ -15,19 +15,13 @@ pub enum IndexError { Facet(#[from] FacetError), } -macro_rules! internal_error { - ($($other:path), *) => { - $( - impl From<$other> for IndexError { - fn from(other: $other) -> Self { - Self::Internal(Box::new(other)) - } - } - )* - } -} - -internal_error!(std::io::Error, heed::Error, fst::Error, serde_json::Error); +internal_error!( + IndexError: + std::io::Error, + heed::Error, + fst::Error, + serde_json::Error +); impl ErrorCode for IndexError { fn error_code(&self) -> Code { diff --git a/meilisearch-http/src/index_controller/dump_actor/error.rs b/meilisearch-http/src/index_controller/dump_actor/error.rs index ff5147e89..778b6b25c 100644 --- a/meilisearch-http/src/index_controller/dump_actor/error.rs +++ b/meilisearch-http/src/index_controller/dump_actor/error.rs @@ -1,8 +1,7 @@ use meilisearch_error::{Code, ErrorCode}; -use crate::index_controller::{ - update_actor::error::UpdateActorError, uuid_resolver::UuidResolverError, -}; +use crate::index_controller::update_actor::error::UpdateActorError; +use crate::index_controller::uuid_resolver::error::UuidResolverError; pub type Result = std::result::Result; diff --git a/meilisearch-http/src/index_controller/error.rs b/meilisearch-http/src/index_controller/error.rs index 23992ba80..0862c299e 100644 --- a/meilisearch-http/src/index_controller/error.rs +++ b/meilisearch-http/src/index_controller/error.rs @@ -1,19 +1,15 @@ -use std::error::Error; - use meilisearch_error::Code; use meilisearch_error::ErrorCode; use super::dump_actor::error::DumpActorError; use super::index_actor::error::IndexActorError; use super::update_actor::error::UpdateActorError; -use super::uuid_resolver::UuidResolverError; +use super::uuid_resolver::error::UuidResolverError; pub type Result = std::result::Result; #[derive(Debug, thiserror::Error)] pub enum IndexControllerError { - #[error("Internal error: {0}")] - Internal(Box), #[error("Missing index uid")] MissingUid, #[error("index resolution error: {0}")] @@ -29,7 +25,6 @@ pub enum IndexControllerError { impl ErrorCode for IndexControllerError { fn error_code(&self) -> Code { match self { - IndexControllerError::Internal(_) => Code::Internal, IndexControllerError::MissingUid => Code::InvalidIndexUid, IndexControllerError::Uuid(e) => e.error_code(), IndexControllerError::IndexActor(e) => e.error_code(), diff --git a/meilisearch-http/src/index_controller/mod.rs b/meilisearch-http/src/index_controller/mod.rs index 188e3b434..d82eb3d30 100644 --- a/meilisearch-http/src/index_controller/mod.rs +++ b/meilisearch-http/src/index_controller/mod.rs @@ -19,7 +19,7 @@ use index_actor::IndexActorHandle; use snapshot::{load_snapshot, SnapshotService}; use update_actor::UpdateActorHandle; pub use updates::*; -use uuid_resolver::{UuidResolverError, UuidResolverHandle}; +use uuid_resolver::{error::UuidResolverError, UuidResolverHandle}; use crate::index::{Checked, Document, SearchQuery, SearchResult, Settings}; use crate::option::Opt; diff --git a/meilisearch-http/src/index_controller/update_actor/error.rs b/meilisearch-http/src/index_controller/update_actor/error.rs index af30fb046..003d90054 100644 --- a/meilisearch-http/src/index_controller/update_actor/error.rs +++ b/meilisearch-http/src/index_controller/update_actor/error.rs @@ -20,18 +20,6 @@ pub enum UpdateActorError { FatalUpdateStoreError, } -macro_rules! internal_error { - ($($other:path), *) => { - $( - impl From<$other> for UpdateActorError { - fn from(other: $other) -> Self { - Self::Internal(Box::new(other)) - } - } - )* - } -} - impl From> for UpdateActorError { fn from(_: tokio::sync::mpsc::error::SendError) -> Self { Self::FatalUpdateStoreError diff --git a/meilisearch-http/src/index_controller/uuid_resolver/actor.rs b/meilisearch-http/src/index_controller/uuid_resolver/actor.rs index 74158ce04..2c73aa490 100644 --- a/meilisearch-http/src/index_controller/uuid_resolver/actor.rs +++ b/meilisearch-http/src/index_controller/uuid_resolver/actor.rs @@ -4,7 +4,7 @@ use log::{info, warn}; use tokio::sync::mpsc; use uuid::Uuid; -use super::{Result, UuidResolveMsg, UuidResolverError, UuidStore}; +use super::{Result, UuidResolveMsg, error::UuidResolverError, UuidStore}; pub struct UuidResolverActor { inbox: mpsc::Receiver, diff --git a/meilisearch-http/src/index_controller/uuid_resolver/error.rs b/meilisearch-http/src/index_controller/uuid_resolver/error.rs new file mode 100644 index 000000000..30cde08c1 --- /dev/null +++ b/meilisearch-http/src/index_controller/uuid_resolver/error.rs @@ -0,0 +1,34 @@ +use meilisearch_error::{Code, ErrorCode}; + +pub type Result = std::result::Result; + +#[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), +} + +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, + } + } +} diff --git a/meilisearch-http/src/index_controller/uuid_resolver/mod.rs b/meilisearch-http/src/index_controller/uuid_resolver/mod.rs index 67d77d411..b7e40618e 100644 --- a/meilisearch-http/src/index_controller/uuid_resolver/mod.rs +++ b/meilisearch-http/src/index_controller/uuid_resolver/mod.rs @@ -1,4 +1,5 @@ mod actor; +pub mod error; mod handle_impl; mod message; pub mod store; @@ -6,14 +7,12 @@ pub mod store; use std::collections::HashSet; use std::path::PathBuf; -use meilisearch_error::Code; -use meilisearch_error::ErrorCode; -use thiserror::Error; use uuid::Uuid; use actor::UuidResolverActor; use message::UuidResolveMsg; use store::UuidStore; +use error::Result; #[cfg(test)] use mockall::automock; @@ -23,7 +22,6 @@ pub use store::HeedUuidStore; const UUID_STORE_SIZE: usize = 1_073_741_824; //1GiB -pub type Result = std::result::Result; #[async_trait::async_trait] #[cfg_attr(test, automock)] @@ -36,46 +34,3 @@ pub trait UuidResolverHandle { async fn get_size(&self) -> Result; async fn dump(&self, path: PathBuf) -> Result>; } - -#[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), -} - -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, - } - } -} diff --git a/meilisearch-http/src/index_controller/uuid_resolver/store.rs b/meilisearch-http/src/index_controller/uuid_resolver/store.rs index 88d707797..9107ce908 100644 --- a/meilisearch-http/src/index_controller/uuid_resolver/store.rs +++ b/meilisearch-http/src/index_controller/uuid_resolver/store.rs @@ -8,7 +8,7 @@ use heed::{CompactionOption, Database, Env, EnvOpenOptions}; use serde::{Deserialize, Serialize}; use uuid::Uuid; -use super::{Result, UuidResolverError, UUID_STORE_SIZE}; +use super::{Result, error::UuidResolverError, UUID_STORE_SIZE}; use crate::helpers::EnvSizer; #[derive(Serialize, Deserialize)] diff --git a/meilisearch-http/src/lib.rs b/meilisearch-http/src/lib.rs index a815a3659..25c29ac11 100644 --- a/meilisearch-http/src/lib.rs +++ b/meilisearch-http/src/lib.rs @@ -1,4 +1,5 @@ pub mod data; +#[macro_use] pub mod error; pub mod helpers; mod index;