From b71bbcffaad8245f562bf4eb098e7c038759fa7e Mon Sep 17 00:00:00 2001 From: qdequele Date: Thu, 16 Jan 2020 16:58:57 +0100 Subject: [PATCH] simplify error handling --- Cargo.lock | 1 + meilisearch-http/Cargo.toml | 5 +- meilisearch-http/src/error.rs | 41 ++++++++ meilisearch-http/src/helpers/tide.rs | 5 +- meilisearch-http/src/routes/document.rs | 62 +++++-------- meilisearch-http/src/routes/health.rs | 30 ++---- meilisearch-http/src/routes/index.rs | 108 ++++++---------------- meilisearch-http/src/routes/key.rs | 52 +++-------- meilisearch-http/src/routes/search.rs | 13 +-- meilisearch-http/src/routes/setting.rs | 12 +-- meilisearch-http/src/routes/stats.rs | 52 +++-------- meilisearch-http/src/routes/stop_words.rs | 30 ++---- meilisearch-http/src/routes/synonym.rs | 27 +++--- 13 files changed, 158 insertions(+), 280 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2c96a6a61..3a1ac833c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1016,6 +1016,7 @@ dependencies = [ "chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-channel 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "fst 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "heed 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "http 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)", "http-service 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/meilisearch-http/Cargo.toml b/meilisearch-http/Cargo.toml index 7655ac209..61c34b5e7 100644 --- a/meilisearch-http/Cargo.toml +++ b/meilisearch-http/Cargo.toml @@ -14,10 +14,12 @@ name = "meilisearch" path = "src/main.rs" [dependencies] +async-std = { version = "1.0.1", features = ["unstable", "attributes"] } bincode = "1.2.0" chrono = { version = "0.4.9", features = ["serde"] } crossbeam-channel = "0.4.0" env_logger = "0.7.1" +fst = { version = "0.3.5", default-features = false } heed = "0.6.1" http = "0.1.19" indexmap = { version = "1.3.0", features = ["serde-1"] } @@ -34,11 +36,10 @@ serde_qs = "0.5.1" siphasher = "0.3.1" structopt = "0.3.3" sysinfo = "0.9.5" +tide = "0.5.1" ureq = { version = "0.11.2", features = ["tls"], default-features = false } walkdir = "2.2.9" whoami = "0.6" -tide = "0.5.1" -async-std = { version = "1.0.1", features = ["unstable", "attributes"] } [dev-dependencies] diff --git a/meilisearch-http/src/error.rs b/meilisearch-http/src/error.rs index b0f7a1272..220519231 100644 --- a/meilisearch-http/src/error.rs +++ b/meilisearch-http/src/error.rs @@ -6,6 +6,8 @@ use serde::{Deserialize, Serialize}; use tide::IntoResponse; use tide::Response; +use crate::helpers::meilisearch::Error as SearchError; + pub type SResult = Result; pub enum ResponseError { @@ -122,3 +124,42 @@ fn error(message: String, status: StatusCode) -> Response { let message = ErrorMessage { message }; tide::Response::new(status.as_u16()).body_json(&message).unwrap() } + +impl From for ResponseError { + fn from(err: meilisearch_core::Error) -> ResponseError { + ResponseError::internal(err) + } +} + +impl From for ResponseError { + fn from(err: heed::Error) -> ResponseError { + ResponseError::internal(err) + } +} + +impl From for ResponseError { + fn from(err: fst::Error) -> ResponseError { + ResponseError::internal(err) + } +} + +impl From for ResponseError { + fn from(err: SearchError) -> ResponseError { + ResponseError::internal(err) + } +} + + +pub trait IntoInternalError { + fn into_internal_error(self) -> SResult; +} + +/// Must be used only +impl IntoInternalError for Option { + fn into_internal_error(self) -> SResult { + match self { + Some(value) => Ok(value), + None => Err(ResponseError::internal("Heed ")) + } + } +} diff --git a/meilisearch-http/src/helpers/tide.rs b/meilisearch-http/src/helpers/tide.rs index 23eda399f..b12d05152 100644 --- a/meilisearch-http/src/helpers/tide.rs +++ b/meilisearch-http/src/helpers/tide.rs @@ -30,14 +30,13 @@ impl RequestExt for Request { let request_index: Option = None; //self.param::("index").ok(); let db = &self.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; let token_key = format!("{}{}", TOKEN_PREFIX_KEY, user_api_key); let token_config = db .common_store() - .get::<_, Str, SerdeBincode>(&reader, &token_key) - .map_err(ResponseError::internal)? + .get::<_, Str, SerdeBincode>(&reader, &token_key)? .ok_or(ResponseError::invalid_token(format!( "Api key does not exist: {}", user_api_key diff --git a/meilisearch-http/src/routes/document.rs b/meilisearch-http/src/routes/document.rs index fb9eaf681..b6cf005da 100644 --- a/meilisearch-http/src/routes/document.rs +++ b/meilisearch-http/src/routes/document.rs @@ -21,11 +21,10 @@ pub async fn get_document(ctx: Request) -> SResult { let document_id = meilisearch_core::serde::compute_document_id(identifier.clone()); let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; let response = index - .document::>(&reader, None, document_id) - .map_err(ResponseError::internal)? + .document::>(&reader, None, document_id)? .ok_or(ResponseError::document_not_found(&identifier))?; if response.is_empty() { @@ -47,17 +46,13 @@ pub async fn delete_document(ctx: Request) -> SResult { let index = ctx.index()?; let identifier = ctx.identifier()?; let document_id = meilisearch_core::serde::compute_document_id(identifier.clone()); - let db = &ctx.state().db; - let mut update_writer = db.update_write_txn().map_err(ResponseError::internal)?; - + let mut update_writer = db.update_write_txn()?; let mut documents_deletion = index.documents_deletion(); documents_deletion.delete_document_by_id(document_id); - let update_id = documents_deletion - .finalize(&mut update_writer) - .map_err(ResponseError::internal)?; + let update_id = documents_deletion.finalize(&mut update_writer)?; - update_writer.commit().map_err(ResponseError::internal)?; + update_writer.commit()?; let response_body = IndexUpdateResponse { update_id }; Ok(tide::Response::new(202).body_json(&response_body).unwrap()) @@ -81,13 +76,11 @@ pub async fn get_all_documents(ctx: Request) -> SResult { let limit = query.limit.unwrap_or(20); let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; - let documents_ids: Result, _> = - match index.documents_fields_counts.documents_ids(&reader) { - Ok(documents_ids) => documents_ids.skip(offset).take(limit).collect(), - Err(e) => return Err(ResponseError::internal(e)), - }; + let documents_ids: Result, _> = index.documents_fields_counts + .documents_ids(&reader)? + .skip(offset).take(limit).collect(); let documents_ids = match documents_ids { Ok(documents_ids) => documents_ids, @@ -139,13 +132,9 @@ async fn update_multiple_documents(mut ctx: Request, is_partial: bool) -> let query: UpdateDocumentsQuery = ctx.query().unwrap_or_default(); let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; - let mut update_writer = db.update_write_txn().map_err(ResponseError::internal)?; - - let current_schema = index - .main - .schema(&reader) - .map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; + let mut update_writer = db.update_write_txn()?; + let current_schema = index.main.schema(&reader)?; if current_schema.is_none() { let id = match query.identifier { Some(id) => id, @@ -160,9 +149,7 @@ async fn update_multiple_documents(mut ctx: Request, is_partial: bool) -> attribute_identifier: Some(id), ..Settings::default() }; - index - .settings_update(&mut update_writer, settings.into()) - .map_err(ResponseError::internal)?; + index.settings_update(&mut update_writer, settings.into())?; } let mut document_addition = if is_partial { @@ -175,11 +162,8 @@ async fn update_multiple_documents(mut ctx: Request, is_partial: bool) -> document_addition.update_document(document); } - let update_id = document_addition - .finalize(&mut update_writer) - .map_err(ResponseError::internal)?; - - update_writer.commit().map_err(ResponseError::internal)?; + let update_id = document_addition.finalize(&mut update_writer)?; + update_writer.commit()?; let response_body = IndexUpdateResponse { update_id }; Ok(tide::Response::new(202).body_json(&response_body).unwrap()) @@ -200,7 +184,7 @@ pub async fn delete_multiple_documents(mut ctx: Request) -> SResult) -> SResult) -> SResult { let index = ctx.index()?; let db = &ctx.state().db; - let mut writer = db.update_write_txn().map_err(ResponseError::internal)?; + let mut writer = db.update_write_txn()?; - let update_id = index - .clear_all(&mut writer) - .map_err(ResponseError::internal)?; - writer.commit().map_err(ResponseError::internal)?; + let update_id = index.clear_all(&mut writer)?; + writer.commit()?; let response_body = IndexUpdateResponse { update_id }; Ok(tide::Response::new(202).body_json(&response_body).unwrap()) diff --git a/meilisearch-http/src/routes/health.rs b/meilisearch-http/src/routes/health.rs index dae4fa17f..7b361c30f 100644 --- a/meilisearch-http/src/routes/health.rs +++ b/meilisearch-http/src/routes/health.rs @@ -11,7 +11,7 @@ const UNHEALTHY_KEY: &str = "_is_unhealthy"; pub async fn get_health(ctx: Request) -> SResult { let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; let common_store = ctx.state().db.common_store(); @@ -24,38 +24,22 @@ pub async fn get_health(ctx: Request) -> SResult { pub async fn set_healthy(ctx: Request) -> SResult { ctx.is_allowed(Admin)?; - let db = &ctx.state().db; - let mut writer = db.main_write_txn().map_err(ResponseError::internal)?; - + let mut writer = db.main_write_txn()?; let common_store = ctx.state().db.common_store(); - match common_store.delete::<_, Str>(&mut writer, UNHEALTHY_KEY) { - Ok(_) => (), - Err(e) => return Err(ResponseError::internal(e)), - } - - if let Err(e) = writer.commit() { - return Err(ResponseError::internal(e)); - } + common_store.delete::<_, Str>(&mut writer, UNHEALTHY_KEY)?; + writer.commit()?; Ok(tide::Response::new(200)) } pub async fn set_unhealthy(ctx: Request) -> SResult { ctx.is_allowed(Admin)?; - let db = &ctx.state().db; - let mut writer = db.main_write_txn().map_err(ResponseError::internal)?; - + let mut writer = db.main_write_txn()?; let common_store = ctx.state().db.common_store(); - - if let Err(e) = common_store.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &()) { - return Err(ResponseError::internal(e)); - } - - if let Err(e) = writer.commit() { - return Err(ResponseError::internal(e)); - } + common_store.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &())?; + writer.commit()?; Ok(tide::Response::new(200)) } diff --git a/meilisearch-http/src/routes/index.rs b/meilisearch-http/src/routes/index.rs index 537496054..000c122de 100644 --- a/meilisearch-http/src/routes/index.rs +++ b/meilisearch-http/src/routes/index.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; use serde_json::json; use tide::{Request, Response}; -use crate::error::{ResponseError, SResult}; +use crate::error::{ResponseError, SResult, IntoInternalError}; use crate::helpers::tide::RequestExt; use crate::models::token::ACL::*; use crate::Data; @@ -26,7 +26,7 @@ pub async fn list_indexes(ctx: Request) -> SResult { let indexes_uids = ctx.state().db.indexes_uids(); let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; let mut response_body = Vec::new(); @@ -35,21 +35,9 @@ pub async fn list_indexes(ctx: Request) -> SResult { match index { Some(index) => { - let name = index - .main - .name(&reader) - .map_err(ResponseError::internal)? - .ok_or(ResponseError::internal("'name' not found"))?; - let created_at = index - .main - .created_at(&reader) - .map_err(ResponseError::internal)? - .ok_or(ResponseError::internal("'created_at' date not found"))?; - let updated_at = index - .main - .updated_at(&reader) - .map_err(ResponseError::internal)? - .ok_or(ResponseError::internal("'updated_at' date not found"))?; + let name = index.main.name(&reader)?.into_internal_error()?; + let created_at = index.main.created_at(&reader)?.into_internal_error()?; + let updated_at = index.main.updated_at(&reader)?.into_internal_error()?; let index_response = IndexResponse { name, @@ -84,24 +72,12 @@ pub async fn get_index(ctx: Request) -> SResult { let index = ctx.index()?; let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; let uid = ctx.url_param("index")?; - let name = index - .main - .name(&reader) - .map_err(ResponseError::internal)? - .ok_or(ResponseError::internal("'name' not found"))?; - let created_at = index - .main - .created_at(&reader) - .map_err(ResponseError::internal)? - .ok_or(ResponseError::internal("'created_at' date not found"))?; - let updated_at = index - .main - .updated_at(&reader) - .map_err(ResponseError::internal)? - .ok_or(ResponseError::internal("'updated_at' date not found"))?; + let name = index.main.name(&reader)?.into_internal_error()?; + let created_at = index.main.created_at(&reader)?.into_internal_error()?; + let updated_at = index.main.updated_at(&reader)?.into_internal_error()?; let response_body = IndexResponse { name, @@ -158,24 +134,13 @@ pub async fn create_index(mut ctx: Request) -> SResult { Err(e) => return Err(ResponseError::create_index(e)), }; - let mut writer = db.main_write_txn().map_err(ResponseError::internal)?; - + let mut writer = db.main_write_txn()?; let name = body.name.unwrap_or(uid.clone()); + created_index.main.put_name(&mut writer, &name)?; + let created_at = created_index.main.created_at(&writer)?.into_internal_error()?; + let updated_at = created_index.main.updated_at(&writer)?.into_internal_error()?; - created_index.main - .put_name(&mut writer, &name) - .map_err(ResponseError::internal)?; - - let created_at = created_index.main - .created_at(&writer) - .map_err(ResponseError::internal)? - .unwrap_or(Utc::now()); - let updated_at = created_index.main - .updated_at(&writer) - .map_err(ResponseError::internal)? - .unwrap_or(Utc::now()); - - writer.commit().map_err(ResponseError::internal)?; + writer.commit()?; let response_body = IndexCreateResponse { name: name, @@ -214,31 +179,17 @@ pub async fn update_index(mut ctx: Request) -> SResult { let index = ctx.index()?; let db = &ctx.state().db; - let mut writer = db.main_write_txn().map_err(ResponseError::internal)?; + let mut writer = db.main_write_txn()?; - index - .main - .put_name(&mut writer, &body.name) - .map_err(ResponseError::internal)?; + index.main.put_name(&mut writer, &body.name)?; - index - .main - .put_updated_at(&mut writer) - .map_err(ResponseError::internal)?; + index.main.put_updated_at(&mut writer)?; - writer.commit().map_err(ResponseError::internal)?; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; + writer.commit()?; + let reader = db.main_read_txn()?; - let created_at = index - .main - .created_at(&reader) - .map_err(ResponseError::internal)? - .ok_or(ResponseError::internal("'created_at' date not found"))?; - let updated_at = index - .main - .updated_at(&reader) - .map_err(ResponseError::internal)? - .ok_or(ResponseError::internal("'updated_at' date not found"))?; + let created_at = index.main.created_at(&reader)?.into_internal_error()?; + let updated_at = index.main.updated_at(&reader)?.into_internal_error()?; let response_body = UpdateIndexResponse { name: body.name, @@ -254,16 +205,14 @@ pub async fn get_update_status(ctx: Request) -> SResult { ctx.is_allowed(IndexesRead)?; let db = &ctx.state().db; - let reader = db.update_read_txn().map_err(ResponseError::internal)?; + let reader = db.update_read_txn()?; let update_id = ctx .param::("update_id") .map_err(|e| ResponseError::bad_parameter("update_id", e))?; let index = ctx.index()?; - let status = index - .update_status(&reader, update_id) - .map_err(ResponseError::internal)?; + let status = index.update_status(&reader, update_id)?; let response = match status { Some(status) => tide::Response::new(200).body_json(&status).unwrap(), @@ -275,15 +224,10 @@ pub async fn get_update_status(ctx: Request) -> SResult { pub async fn get_all_updates_status(ctx: Request) -> SResult { ctx.is_allowed(IndexesRead)?; - let db = &ctx.state().db; - let reader = db.update_read_txn().map_err(ResponseError::internal)?; - + let reader = db.update_read_txn()?; let index = ctx.index()?; - let response = index - .all_updates_status(&reader) - .map_err(ResponseError::internal)?; - + let response = index.all_updates_status(&reader)?; Ok(tide::Response::new(200).body_json(&response).unwrap()) } @@ -291,7 +235,7 @@ pub async fn delete_index(ctx: Request) -> SResult { ctx.is_allowed(IndexesWrite)?; let _ = ctx.index()?; let index_uid = ctx.url_param("index")?; - ctx.state().db.delete_index(&index_uid).map_err(ResponseError::internal)?; + ctx.state().db.delete_index(&index_uid)?; Ok(tide::Response::new(204)) } diff --git a/meilisearch-http/src/routes/key.rs b/meilisearch-http/src/routes/key.rs index 693674bd1..fea422696 100644 --- a/meilisearch-http/src/routes/key.rs +++ b/meilisearch-http/src/routes/key.rs @@ -24,18 +24,17 @@ pub async fn list(ctx: Request) -> SResult { ctx.is_allowed(Admin)?; let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; let common_store = db.common_store(); let mut response: Vec = Vec::new(); let iter = common_store - .prefix_iter::<_, Str, SerdeBincode>(&reader, TOKEN_PREFIX_KEY) - .map_err(ResponseError::internal)?; + .prefix_iter::<_, Str, SerdeBincode>(&reader, TOKEN_PREFIX_KEY)?; for result in iter { - let (_, token) = result.map_err(ResponseError::internal)?; + let (_, token) = result?; response.push(token); } @@ -47,14 +46,13 @@ pub async fn get(ctx: Request) -> SResult { let request_key = ctx.url_param("key")?; let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; let token_key = format!("{}{}", TOKEN_PREFIX_KEY, request_key); let token_config = db .common_store() - .get::<_, Str, SerdeBincode>(&reader, &token_key) - .map_err(ResponseError::internal)? + .get::<_, Str, SerdeBincode>(&reader, &token_key)? .ok_or(ResponseError::not_found(format!( "token key: {}", token_key @@ -93,14 +91,11 @@ pub async fn create(mut ctx: Request) -> SResult { }; let db = &ctx.state().db; - let mut writer = db.main_write_txn().map_err(ResponseError::internal)?; + let mut writer = db.main_write_txn()?; - db.common_store() - .put::<_, Str, SerdeBincode>(&mut writer, &token_key, &token_definition) - .map_err(ResponseError::internal)?; - - writer.commit().map_err(ResponseError::internal)?; + db.common_store().put::<_, Str, SerdeBincode>(&mut writer, &token_key, &token_definition)?; + writer.commit()?; Ok(tide::Response::new(201).body_json(&token_definition).unwrap()) } @@ -121,15 +116,14 @@ pub async fn update(mut ctx: Request) -> SResult { let data: UpdatedRequest = ctx.body_json().await.map_err(ResponseError::bad_request)?; let db = &ctx.state().db; - let mut writer = db.main_write_txn().map_err(ResponseError::internal)?; + let mut writer = db.main_write_txn()?; let common_store = db.common_store(); let token_key = format!("{}{}", TOKEN_PREFIX_KEY, request_key); let mut token_config = common_store - .get::<_, Str, SerdeBincode>(&writer, &token_key) - .map_err(ResponseError::internal)? + .get::<_, Str, SerdeBincode>(&writer, &token_key)? .ok_or(ResponseError::not_found(format!( "token key: {}", token_key @@ -139,30 +133,22 @@ pub async fn update(mut ctx: Request) -> SResult { if let Some(description) = data.description { token_config.description = description; } - if let Some(acl) = data.acl { token_config.acl = acl; } - if let Some(indexes) = data.indexes { token_config.indexes = indexes; } - if let Some(expires_at) = data.expires_at { token_config.expires_at = expires_at; } - if let Some(revoked) = data.revoked { token_config.revoked = revoked; } token_config.updated_at = Utc::now(); - - common_store - .put::<_, Str, SerdeBincode>(&mut writer, &token_key, &token_config) - .map_err(ResponseError::internal)?; - - writer.commit().map_err(ResponseError::internal)?; + common_store.put::<_, Str, SerdeBincode>(&mut writer, &token_key, &token_config)?; + writer.commit()?; Ok(tide::Response::new(200).body_json(&token_config).unwrap()) } @@ -170,19 +156,11 @@ pub async fn update(mut ctx: Request) -> SResult { pub async fn delete(ctx: Request) -> SResult { ctx.is_allowed(Admin)?; let request_key = ctx.url_param("key")?; - let db = &ctx.state().db; - let mut writer = db.main_write_txn().map_err(ResponseError::internal)?; - + let mut writer = db.main_write_txn()?; let common_store = db.common_store(); - let token_key = format!("{}{}", TOKEN_PREFIX_KEY, request_key); - - common_store - .delete::<_, Str>(&mut writer, &token_key) - .map_err(ResponseError::internal)?; - - writer.commit().map_err(ResponseError::internal)?; - + common_store.delete::<_, Str>(&mut writer, &token_key)?; + writer.commit()?; Ok(tide::Response::new(204)) } diff --git a/meilisearch-http/src/routes/search.rs b/meilisearch-http/src/routes/search.rs index ebf0e5974..d52497f77 100644 --- a/meilisearch-http/src/routes/search.rs +++ b/meilisearch-http/src/routes/search.rs @@ -32,12 +32,9 @@ pub async fn search_with_url_query(ctx: Request) -> SResult { let index = ctx.index()?; let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; - let schema = index - .main - .schema(&reader) - .map_err(ResponseError::internal)? + let schema = index.main.schema(&reader)? .ok_or(ResponseError::open_index("No Schema found"))?; let query: SearchQuery = ctx.query() @@ -201,10 +198,8 @@ pub async fn search_multi_index(mut ctx: Request) -> SResult { } } - let reader = db.main_read_txn().map_err(ResponseError::internal)?; - let response = search_builder - .search(&reader) - .map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; + let response = search_builder.search(&reader)?; Ok((index_uid, response)) }) .collect(); diff --git a/meilisearch-http/src/routes/setting.rs b/meilisearch-http/src/routes/setting.rs index 79a2b799d..42ccc688d 100644 --- a/meilisearch-http/src/routes/setting.rs +++ b/meilisearch-http/src/routes/setting.rs @@ -29,7 +29,7 @@ pub async fn get(ctx: Request) -> SResult { let index = ctx.index()?; let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; let settings = match index.main.customs(&reader).unwrap() { Some(bytes) => bincode::deserialize(bytes).unwrap(), @@ -64,8 +64,8 @@ pub async fn update(mut ctx: Request) -> SResult { let index = ctx.index()?; let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; - let mut writer = db.update_write_txn().map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; + let mut writer = db.update_write_txn()?; let mut current_settings = match index.main.customs(&reader).unwrap() { Some(bytes) => bincode::deserialize(bytes).unwrap(), @@ -82,11 +82,9 @@ pub async fn update(mut ctx: Request) -> SResult { let bytes = bincode::serialize(¤t_settings).unwrap(); - let update_id = index - .customs_update(&mut writer, bytes) - .map_err(ResponseError::internal)?; + let update_id = index.customs_update(&mut writer, bytes)?; - writer.commit().map_err(ResponseError::internal)?; + writer.commit()?; let response_body = IndexUpdateResponse { update_id }; Ok(tide::Response::new(202).body_json(&response_body).unwrap()) diff --git a/meilisearch-http/src/routes/stats.rs b/meilisearch-http/src/routes/stats.rs index f0eac878a..715217031 100644 --- a/meilisearch-http/src/routes/stats.rs +++ b/meilisearch-http/src/routes/stats.rs @@ -8,7 +8,7 @@ use sysinfo::{NetworkExt, Pid, ProcessExt, ProcessorExt, System, SystemExt}; use tide::{Request, Response}; use walkdir::WalkDir; -use crate::error::{ResponseError, SResult}; +use crate::error::{SResult, IntoInternalError}; use crate::helpers::tide::RequestExt; use crate::models::token::ACL::*; use crate::Data; @@ -25,27 +25,12 @@ pub async fn index_stat(ctx: Request) -> SResult { ctx.is_allowed(Admin)?; let index_uid = ctx.url_param("index")?; let index = ctx.index()?; - let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; - let update_reader = db.update_read_txn().map_err(ResponseError::internal)?; - - let number_of_documents = index - .main - .number_of_documents(&reader) - .map_err(ResponseError::internal)?; - - let fields_frequency = index - .main - .fields_frequency(&reader) - .map_err(ResponseError::internal)? - .unwrap_or_default(); - - let is_indexing = ctx - .state() - .is_indexing(&update_reader, &index_uid) - .map_err(ResponseError::internal)? - .ok_or(ResponseError::internal("'is_indexing' date not found"))?; + let reader = db.main_read_txn()?; + let update_reader = db.update_read_txn()?; + let number_of_documents = index.main.number_of_documents(&reader)?; + let fields_frequency = index.main.fields_frequency(&reader)?.unwrap_or_default(); + let is_indexing = ctx.state().is_indexing(&update_reader, &index_uid)?.into_internal_error()?; let response = IndexStatsResponse { number_of_documents, @@ -69,8 +54,8 @@ pub async fn get_stats(ctx: Request) -> SResult { let mut index_list = HashMap::new(); let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; - let update_reader = db.update_read_txn().map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; + let update_reader = db.update_read_txn()?; let indexes_set = ctx.state().db.indexes_uids(); for index_uid in indexes_set { @@ -78,22 +63,14 @@ pub async fn get_stats(ctx: Request) -> SResult { match index { Some(index) => { - let number_of_documents = index - .main - .number_of_documents(&reader) - .map_err(ResponseError::internal)?; + let number_of_documents = index.main.number_of_documents(&reader)?; - let fields_frequency = index - .main - .fields_frequency(&reader) - .map_err(ResponseError::internal)? - .unwrap_or_default(); + let fields_frequency = index.main.fields_frequency(&reader)?.unwrap_or_default(); let is_indexing = ctx .state() - .is_indexing(&update_reader, &index_uid) - .map_err(ResponseError::internal)? - .ok_or(ResponseError::internal("'is_indexing' date not found"))?; + .is_indexing(&update_reader, &index_uid)? + .into_internal_error()?; let response = IndexStatsResponse { number_of_documents, @@ -116,10 +93,7 @@ pub async fn get_stats(ctx: Request) -> SResult { .filter(|metadata| metadata.is_file()) .fold(0, |acc, m| acc + m.len()); - let last_update = ctx - .state() - .last_update(&reader) - .map_err(ResponseError::internal)?; + let last_update = ctx.state().last_update(&reader)?; let response = StatsResult { database_size, diff --git a/meilisearch-http/src/routes/stop_words.rs b/meilisearch-http/src/routes/stop_words.rs index 8cc4f6ce1..fc21d61ef 100644 --- a/meilisearch-http/src/routes/stop_words.rs +++ b/meilisearch-http/src/routes/stop_words.rs @@ -12,20 +12,10 @@ use crate::Data; pub async fn get(ctx: Request) -> SResult { ctx.is_allowed(SettingsRead)?; let index = ctx.index()?; - let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; - - let stop_words_fst = index - .main - .stop_words_fst(&reader) - .map_err(ResponseError::internal)?; - - let stop_words = stop_words_fst - .unwrap_or_default() - .stream() - .into_strs() - .map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; + let stop_words_fst = index.main.stop_words_fst(&reader)?; + let stop_words = stop_words_fst.unwrap_or_default().stream().into_strs()?; Ok(tide::Response::new(200).body_json(&stop_words).unwrap()) } @@ -37,17 +27,16 @@ pub async fn update(mut ctx: Request) -> SResult { let data: BTreeSet = ctx.body_json().await.map_err(ResponseError::bad_request)?; let db = &ctx.state().db; - let mut writer = db.update_write_txn().map_err(ResponseError::internal)?; + let mut writer = db.update_write_txn()?; let settings = SettingsUpdate { stop_words: UpdateState::Update(data), .. SettingsUpdate::default() }; - let update_id = index.settings_update(&mut writer, settings) - .map_err(ResponseError::internal)?; + let update_id = index.settings_update(&mut writer, settings)?; - writer.commit().map_err(ResponseError::internal)?; + writer.commit()?; let response_body = IndexUpdateResponse { update_id }; Ok(tide::Response::new(202).body_json(&response_body).unwrap()) @@ -58,17 +47,16 @@ pub async fn delete(ctx: Request) -> SResult { let index = ctx.index()?; let db = &ctx.state().db; - let mut writer = db.update_write_txn().map_err(ResponseError::internal)?; + let mut writer = db.update_write_txn()?; let settings = SettingsUpdate { stop_words: UpdateState::Clear, .. SettingsUpdate::default() }; - let update_id = index.settings_update(&mut writer, settings) - .map_err(ResponseError::internal)?; + let update_id = index.settings_update(&mut writer, settings)?; - writer.commit().map_err(ResponseError::internal)?; + writer.commit()?; let response_body = IndexUpdateResponse { update_id }; Ok(tide::Response::new(202).body_json(&response_body).unwrap()) diff --git a/meilisearch-http/src/routes/synonym.rs b/meilisearch-http/src/routes/synonym.rs index 9ee0f5b99..a287a4962 100644 --- a/meilisearch-http/src/routes/synonym.rs +++ b/meilisearch-http/src/routes/synonym.rs @@ -15,27 +15,24 @@ pub async fn get(ctx: Request) -> SResult { let index = ctx.index()?; let db = &ctx.state().db; - let reader = db.main_read_txn().map_err(ResponseError::internal)?; + let reader = db.main_read_txn()?; let synonyms_fst = index .main - .synonyms_fst(&reader) - .map_err(ResponseError::internal)?; + .synonyms_fst(&reader)?; let synonyms_fst = synonyms_fst.unwrap_or_default(); - let synonyms_list = synonyms_fst.stream().into_strs().map_err(ResponseError::internal)?; + let synonyms_list = synonyms_fst.stream().into_strs()?; let mut response = IndexMap::new(); let index_synonyms = &index.synonyms; for synonym in synonyms_list { - let alternative_list = index_synonyms - .synonyms(&reader, synonym.as_bytes()) - .map_err(ResponseError::internal)?; + let alternative_list = index_synonyms.synonyms(&reader, synonym.as_bytes())?; if let Some(list) = alternative_list { - let list = list.stream().into_strs().map_err(ResponseError::internal)?; + let list = list.stream().into_strs()?; response.insert(synonym, list); } } @@ -51,17 +48,16 @@ pub async fn update(mut ctx: Request) -> SResult { let index = ctx.index()?; let db = &ctx.state().db; - let mut writer = db.update_write_txn().map_err(ResponseError::internal)?; + let mut writer = db.update_write_txn()?; let settings = SettingsUpdate { synonyms: UpdateState::Update(data), .. SettingsUpdate::default() }; - let update_id = index.settings_update(&mut writer, settings) - .map_err(ResponseError::internal)?; + let update_id = index.settings_update(&mut writer, settings)?; - writer.commit().map_err(ResponseError::internal)?; + writer.commit()?; let response_body = IndexUpdateResponse { update_id }; Ok(tide::Response::new(202).body_json(&response_body).unwrap()) @@ -74,17 +70,16 @@ pub async fn delete(ctx: Request) -> SResult { let index = ctx.index()?; let db = &ctx.state().db; - let mut writer = db.update_write_txn().map_err(ResponseError::internal)?; + let mut writer = db.update_write_txn()?; let settings = SettingsUpdate { synonyms: UpdateState::Clear, .. SettingsUpdate::default() }; - let update_id = index.settings_update(&mut writer, settings) - .map_err(ResponseError::internal)?; + let update_id = index.settings_update(&mut writer, settings)?; - writer.commit().map_err(ResponseError::internal)?; + writer.commit()?; let response_body = IndexUpdateResponse { update_id }; Ok(tide::Response::new(202).body_json(&response_body).unwrap())