change ResponseError to Error

This commit is contained in:
mpostma 2020-05-19 18:20:29 +02:00
parent 4c2af8e515
commit e2db197b3f
11 changed files with 203 additions and 201 deletions

View File

@ -7,7 +7,7 @@ use serde_json::json;
use actix_web::error::JsonPayloadError; use actix_web::error::JsonPayloadError;
#[derive(Debug)] #[derive(Debug)]
pub enum ResponseError { pub enum Error {
BadParameter(String, String), BadParameter(String, String),
BadRequest(String), BadRequest(String),
CreateIndex(String), CreateIndex(String),
@ -63,69 +63,69 @@ impl fmt::Display for FacetCountError {
} }
} }
impl ResponseError { impl Error {
pub fn internal(err: impl fmt::Display) -> ResponseError { pub fn internal(err: impl fmt::Display) -> Error {
ResponseError::Internal(err.to_string()) Error::Internal(err.to_string())
} }
pub fn bad_request(err: impl fmt::Display) -> ResponseError { pub fn bad_request(err: impl fmt::Display) -> Error {
ResponseError::BadRequest(err.to_string()) Error::BadRequest(err.to_string())
} }
pub fn missing_authorization_header() -> ResponseError { pub fn missing_authorization_header() -> Error {
ResponseError::MissingAuthorizationHeader Error::MissingAuthorizationHeader
} }
pub fn invalid_token(err: impl fmt::Display) -> ResponseError { pub fn invalid_token(err: impl fmt::Display) -> Error {
ResponseError::InvalidToken(err.to_string()) Error::InvalidToken(err.to_string())
} }
pub fn not_found(err: impl fmt::Display) -> ResponseError { pub fn not_found(err: impl fmt::Display) -> Error {
ResponseError::NotFound(err.to_string()) Error::NotFound(err.to_string())
} }
pub fn index_not_found(err: impl fmt::Display) -> ResponseError { pub fn index_not_found(err: impl fmt::Display) -> Error {
ResponseError::IndexNotFound(err.to_string()) Error::IndexNotFound(err.to_string())
} }
pub fn document_not_found(err: impl fmt::Display) -> ResponseError { pub fn document_not_found(err: impl fmt::Display) -> Error {
ResponseError::DocumentNotFound(err.to_string()) Error::DocumentNotFound(err.to_string())
} }
pub fn missing_header(err: impl fmt::Display) -> ResponseError { pub fn missing_header(err: impl fmt::Display) -> Error {
ResponseError::MissingHeader(err.to_string()) Error::MissingHeader(err.to_string())
} }
pub fn bad_parameter(param: impl fmt::Display, err: impl fmt::Display) -> ResponseError { pub fn bad_parameter(param: impl fmt::Display, err: impl fmt::Display) -> Error {
ResponseError::BadParameter(param.to_string(), err.to_string()) Error::BadParameter(param.to_string(), err.to_string())
} }
pub fn open_index(err: impl fmt::Display) -> ResponseError { pub fn open_index(err: impl fmt::Display) -> Error {
ResponseError::OpenIndex(err.to_string()) Error::OpenIndex(err.to_string())
} }
pub fn create_index(err: impl fmt::Display) -> ResponseError { pub fn create_index(err: impl fmt::Display) -> Error {
ResponseError::CreateIndex(err.to_string()) Error::CreateIndex(err.to_string())
} }
pub fn invalid_index_uid() -> ResponseError { pub fn invalid_index_uid() -> Error {
ResponseError::InvalidIndexUid Error::InvalidIndexUid
} }
pub fn maintenance() -> ResponseError { pub fn maintenance() -> Error {
ResponseError::Maintenance Error::Maintenance
} }
pub fn retrieve_document(doc_id: u32, err: impl fmt::Display) -> ResponseError { pub fn retrieve_document(doc_id: u64, err: impl fmt::Display) -> Error {
ResponseError::RetrieveDocument(doc_id, err.to_string()) Error::RetrieveDocument(doc_id, err.to_string())
} }
pub fn search_documents(err: impl fmt::Display) -> ResponseError { pub fn search_documents(err: impl fmt::Display) -> Error {
ResponseError::SearchDocuments(err.to_string()) Error::SearchDocuments(err.to_string())
} }
} }
impl fmt::Display for ResponseError { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Self::BadParameter(param, err) => write!(f, "Url parameter {} error: {}", param, err), Self::BadParameter(param, err) => write!(f, "Url parameter {} error: {}", param, err),
@ -152,7 +152,7 @@ impl fmt::Display for ResponseError {
} }
} }
impl aweb::error::ResponseError for ResponseError { impl aweb::error::ResponseError for Error {
fn error_response(&self) -> aweb::HttpResponse { fn error_response(&self) -> aweb::HttpResponse {
ResponseBuilder::new(self.status_code()).json(json!({ ResponseBuilder::new(self.status_code()).json(json!({
"message": self.to_string(), "message": self.to_string(),
@ -185,26 +185,26 @@ impl aweb::error::ResponseError for ResponseError {
} }
} }
impl From<meilisearch_core::HeedError> for ResponseError { impl From<meilisearch_core::HeedError> for Error {
fn from(err: meilisearch_core::HeedError) -> ResponseError { fn from(err: meilisearch_core::HeedError) -> Error {
ResponseError::Internal(err.to_string()) Error::Internal(err.to_string())
} }
} }
impl From<meilisearch_core::FstError> for ResponseError { impl From<meilisearch_core::FstError> for Error {
fn from(err: meilisearch_core::FstError) -> ResponseError { fn from(err: meilisearch_core::FstError) -> Error {
ResponseError::Internal(err.to_string()) Error::Internal(err.to_string())
} }
} }
impl From<meilisearch_core::FacetError> for ResponseError { impl From<meilisearch_core::FacetError> for Error {
fn from(error: meilisearch_core::FacetError) -> ResponseError { fn from(error: meilisearch_core::FacetError) -> Error {
ResponseError::FacetExpression(error.to_string()) Error::FacetExpression(error.to_string())
} }
} }
impl From<meilisearch_core::Error> for ResponseError { impl From<meilisearch_core::Error> for Error {
fn from(err: meilisearch_core::Error) -> ResponseError { fn from(err: meilisearch_core::Error) -> Error {
use meilisearch_core::pest_error::LineColLocation::*; use meilisearch_core::pest_error::LineColLocation::*;
match err { match err {
meilisearch_core::Error::FilterParseError(e) => { meilisearch_core::Error::FilterParseError(e) => {
@ -214,43 +214,43 @@ impl From<meilisearch_core::Error> for ResponseError {
}; };
let message = format!("parsing error on line {} at column {}: {}", line, column, e.variant.message()); let message = format!("parsing error on line {} at column {}: {}", line, column, e.variant.message());
ResponseError::FilterParsing(message) Error::FilterParsing(message)
}, },
meilisearch_core::Error::FacetError(e) => ResponseError::FacetExpression(e.to_string()), meilisearch_core::Error::FacetError(e) => Error::FacetExpression(e.to_string()),
_ => ResponseError::Internal(err.to_string()), _ => Error::Internal(err.to_string()),
} }
} }
} }
impl From<meilisearch_schema::Error> for ResponseError { impl From<meilisearch_schema::Error> for Error {
fn from(err: meilisearch_schema::Error) -> ResponseError { fn from(err: meilisearch_schema::Error) -> Error {
ResponseError::Internal(err.to_string()) Error::Internal(err.to_string())
} }
} }
impl From<actix_http::Error> for ResponseError { impl From<actix_http::Error> for Error {
fn from(err: actix_http::Error) -> ResponseError { fn from(err: actix_http::Error) -> Error {
ResponseError::Internal(err.to_string()) Error::Internal(err.to_string())
} }
} }
impl From<FacetCountError> for ResponseError { impl From<FacetCountError> for Error {
fn from(other: FacetCountError) -> ResponseError { fn from(other: FacetCountError) -> Error {
ResponseError::FacetCount(other.to_string()) Error::FacetCount(other.to_string())
} }
} }
impl From<JsonPayloadError> for ResponseError { impl From<JsonPayloadError> for Error {
fn from(err: JsonPayloadError) -> ResponseError { fn from(err: JsonPayloadError) -> Error {
match err { match err {
JsonPayloadError::Deserialize(err) => ResponseError::BadRequest(format!("Invalid JSON: {}", err)), JsonPayloadError::Deserialize(err) => Error::BadRequest(format!("Invalid JSON: {}", err)),
JsonPayloadError::Overflow => ResponseError::PayloadTooLarge, JsonPayloadError::Overflow => Error::PayloadTooLarge,
JsonPayloadError::ContentType => ResponseError::UnsupportedMediaType, JsonPayloadError::ContentType => Error::UnsupportedMediaType,
JsonPayloadError::Payload(err) => ResponseError::BadRequest(format!("Problem while decoding the request: {}", err)), JsonPayloadError::Payload(err) => Error::BadRequest(format!("Problem while decoding the request: {}", err)),
} }
} }
} }
pub fn json_error_handler(err: JsonPayloadError) -> ResponseError { pub fn json_error_handler(err: JsonPayloadError) -> Error {
err.into() err.into()
} }

View File

@ -4,10 +4,10 @@ use std::rc::Rc;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use actix_service::{Service, Transform}; use actix_service::{Service, Transform};
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error}; use actix_web::{dev::ServiceRequest, dev::ServiceResponse};
use futures::future::{err, ok, Future, Ready}; use futures::future::{err, ok, Future, Ready};
use crate::error::ResponseError; use crate::error::Error;
use crate::Data; use crate::Data;
#[derive(Clone)] #[derive(Clone)]
@ -19,13 +19,13 @@ pub enum Authentication {
impl<S: 'static, B> Transform<S> for Authentication impl<S: 'static, B> Transform<S> for Authentication
where where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>, S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error>,
S::Future: 'static, S::Future: 'static,
B: 'static, B: 'static,
{ {
type Request = ServiceRequest; type Request = ServiceRequest;
type Response = ServiceResponse<B>; type Response = ServiceResponse<B>;
type Error = Error; type Error = actix_web::Error;
type InitError = (); type InitError = ();
type Transform = LoggingMiddleware<S>; type Transform = LoggingMiddleware<S>;
type Future = Ready<Result<Self::Transform, Self::InitError>>; type Future = Ready<Result<Self::Transform, Self::InitError>>;
@ -45,13 +45,13 @@ pub struct LoggingMiddleware<S> {
impl<S, B> Service for LoggingMiddleware<S> impl<S, B> Service for LoggingMiddleware<S>
where where
S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static, S: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = actix_web::Error> + 'static,
S::Future: 'static, S::Future: 'static,
B: 'static, B: 'static,
{ {
type Request = ServiceRequest; type Request = ServiceRequest;
type Response = ServiceResponse<B>; type Response = ServiceResponse<B>;
type Error = Error; type Error = actix_web::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>; type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> { fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
@ -71,10 +71,10 @@ where
let auth_header = match req.headers().get("X-Meili-API-Key") { let auth_header = match req.headers().get("X-Meili-API-Key") {
Some(auth) => match auth.to_str() { Some(auth) => match auth.to_str() {
Ok(auth) => auth, Ok(auth) => auth,
Err(_) => return Box::pin(err(ResponseError::MissingAuthorizationHeader.into())), Err(_) => return Box::pin(err(Error::MissingAuthorizationHeader.into())),
}, },
None => { None => {
return Box::pin(err(ResponseError::MissingAuthorizationHeader.into())); return Box::pin(err(Error::MissingAuthorizationHeader.into()));
} }
}; };
@ -95,7 +95,7 @@ where
Box::pin(svc.call(req)) Box::pin(svc.call(req))
} else { } else {
Box::pin(err( Box::pin(err(
ResponseError::InvalidToken(auth_header.to_string()).into() Error::InvalidToken(auth_header.to_string()).into()
)) ))
} }
} }

View File

@ -17,7 +17,7 @@ use serde_json::Value;
use siphasher::sip::SipHasher; use siphasher::sip::SipHasher;
use slice_group_by::GroupBy; use slice_group_by::GroupBy;
use crate::error::ResponseError; use crate::error::Error;
pub trait IndexSearchExt { pub trait IndexSearchExt {
fn new_search(&self, query: String) -> SearchBuilder; fn new_search(&self, query: String) -> SearchBuilder;
@ -107,12 +107,12 @@ impl<'a> SearchBuilder<'a> {
self self
} }
pub fn search(self, reader: &heed::RoTxn<MainT>) -> Result<SearchResult, ResponseError> { pub fn search(self, reader: &heed::RoTxn<MainT>) -> Result<SearchResult, Error> {
let schema = self let schema = self
.index .index
.main .main
.schema(reader)? .schema(reader)?
.ok_or(ResponseError::internal("missing schema"))?; .ok_or(Error::internal("missing schema"))?;
let ranked_map = self.index.main.ranked_map(reader)?.unwrap_or_default(); let ranked_map = self.index.main.ranked_map(reader)?.unwrap_or_default();
@ -159,7 +159,7 @@ impl<'a> SearchBuilder<'a> {
let start = Instant::now(); let start = Instant::now();
let result = query_builder.query(reader, &self.query, self.offset..(self.offset + self.limit)); let result = query_builder.query(reader, &self.query, self.offset..(self.offset + self.limit));
let search_result = result.map_err(ResponseError::search_documents)?; let search_result = result.map_err(Error::search_documents)?;
let time_ms = start.elapsed().as_millis() as usize; let time_ms = start.elapsed().as_millis() as usize;
let mut all_attributes: HashSet<&str> = HashSet::new(); let mut all_attributes: HashSet<&str> = HashSet::new();
@ -194,8 +194,8 @@ impl<'a> SearchBuilder<'a> {
let mut document: IndexMap<String, Value> = self let mut document: IndexMap<String, Value> = self
.index .index
.document(reader, Some(&all_attributes), doc.id) .document(reader, Some(&all_attributes), doc.id)
.map_err(|e| ResponseError::retrieve_document(doc.id.0, e))? .map_err(|e| Error::retrieve_document(doc.id.0, e))?
.ok_or(ResponseError::internal( .ok_or(Error::internal(
"Impossible to retrieve the document; Corrupted data", "Impossible to retrieve the document; Corrupted data",
))?; ))?;
@ -260,7 +260,7 @@ impl<'a> SearchBuilder<'a> {
reader: &heed::RoTxn<MainT>, reader: &heed::RoTxn<MainT>,
ranked_map: &'a RankedMap, ranked_map: &'a RankedMap,
schema: &Schema, schema: &Schema,
) -> Result<Option<Criteria<'a>>, ResponseError> { ) -> Result<Option<Criteria<'a>>, Error> {
let ranking_rules = self.index.main.ranking_rules(reader)?; let ranking_rules = self.index.main.ranking_rules(reader)?;
if let Some(ranking_rules) = ranking_rules { if let Some(ranking_rules) = ranking_rules {

View File

@ -7,7 +7,7 @@ use meilisearch_core::update;
use serde::Deserialize; use serde::Deserialize;
use serde_json::Value; use serde_json::Value;
use crate::error::ResponseError; use crate::error::Error;
use crate::helpers::Authentication; use crate::helpers::Authentication;
use crate::routes::{IndexParam, IndexUpdateResponse}; use crate::routes::{IndexParam, IndexUpdateResponse};
use crate::Data; use crate::Data;
@ -37,11 +37,11 @@ pub fn services(cfg: &mut web::ServiceConfig) {
async fn get_document( async fn get_document(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<DocumentParam>, path: web::Path<DocumentParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let internal_id = index.main let internal_id = index.main
@ -49,8 +49,8 @@ async fn get_document(
.ok_or(ResponseError::document_not_found(&path.document_id))?; .ok_or(ResponseError::document_not_found(&path.document_id))?;
let response: Document = index let response: Document = index
.document(&reader, None, internal_id)? .document(&reader, None, document_id)?
.ok_or(ResponseError::document_not_found(&path.document_id))?; .ok_or(Error::document_not_found(&path.document_id))?;
Ok(HttpResponse::Ok().json(response)) Ok(HttpResponse::Ok().json(response))
} }
@ -62,11 +62,13 @@ async fn get_document(
async fn delete_document( async fn delete_document(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<DocumentParam>, path: web::Path<DocumentParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let document_id = meilisearch_core::serde::compute_document_id(&path.document_id);
let mut update_writer = data.db.update_write_txn()?; let mut update_writer = data.db.update_write_txn()?;
@ -93,11 +95,11 @@ async fn get_all_documents(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
params: web::Query<BrowseQuery>, params: web::Query<BrowseQuery>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let offset = params.offset.unwrap_or(0); let offset = params.offset.unwrap_or(0);
let limit = params.limit.unwrap_or(20); let limit = params.limit.unwrap_or(20);
@ -151,18 +153,18 @@ async fn update_multiple_documents(
params: web::Query<UpdateDocumentsQuery>, params: web::Query<UpdateDocumentsQuery>,
body: web::Json<Vec<Document>>, body: web::Json<Vec<Document>>,
is_partial: bool, is_partial: bool,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let mut schema = index let mut schema = index
.main .main
.schema(&reader)? .schema(&reader)?
.ok_or(ResponseError::internal("Impossible to retrieve the schema"))?; .ok_or(Error::internal("Impossible to retrieve the schema"))?;
if schema.primary_key().is_none() { if schema.primary_key().is_none() {
let id = match &params.primary_key { let id = match &params.primary_key {
@ -170,14 +172,14 @@ async fn update_multiple_documents(
None => body None => body
.first() .first()
.and_then(find_primary_key) .and_then(find_primary_key)
.ok_or(ResponseError::bad_request("Could not infer a primary key"))?, .ok_or(Error::bad_request("Could not infer a primary key"))?,
}; };
let mut writer = data.db.main_write_txn()?; let mut writer = data.db.main_write_txn()?;
schema schema
.set_primary_key(&id) .set_primary_key(&id)
.map_err(ResponseError::bad_request)?; .map_err(Error::bad_request)?;
index.main.put_schema(&mut writer, &schema)?; index.main.put_schema(&mut writer, &schema)?;
writer.commit()?; writer.commit()?;
} }
@ -205,7 +207,7 @@ async fn add_documents(
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
params: web::Query<UpdateDocumentsQuery>, params: web::Query<UpdateDocumentsQuery>,
body: web::Json<Vec<Document>>, body: web::Json<Vec<Document>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
update_multiple_documents(data, path, params, body, false).await update_multiple_documents(data, path, params, body, false).await
} }
@ -215,7 +217,7 @@ async fn update_documents(
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
params: web::Query<UpdateDocumentsQuery>, params: web::Query<UpdateDocumentsQuery>,
body: web::Json<Vec<Document>>, body: web::Json<Vec<Document>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
update_multiple_documents(data, path, params, body, true).await update_multiple_documents(data, path, params, body, true).await
} }
@ -227,11 +229,11 @@ async fn delete_documents(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<Vec<Value>>, body: web::Json<Vec<Value>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let mut writer = data.db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
@ -253,11 +255,11 @@ async fn delete_documents(
async fn clear_all_documents( async fn clear_all_documents(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let mut writer = data.db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;

View File

@ -3,7 +3,7 @@ use actix_web_macros::{get, put};
use heed::types::{Str, Unit}; use heed::types::{Str, Unit};
use serde::Deserialize; use serde::Deserialize;
use crate::error::ResponseError; use crate::error::Error;
use crate::helpers::Authentication; use crate::helpers::Authentication;
use crate::Data; use crate::Data;
@ -14,19 +14,19 @@ pub fn services(cfg: &mut web::ServiceConfig) {
} }
#[get("/health", wrap = "Authentication::Private")] #[get("/health", wrap = "Authentication::Private")]
async fn get_health(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> { async fn get_health(data: web::Data<Data>) -> Result<HttpResponse, Error> {
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let common_store = data.db.common_store(); let common_store = data.db.common_store();
if let Ok(Some(_)) = common_store.get::<_, Str, Unit>(&reader, UNHEALTHY_KEY) { if let Ok(Some(_)) = common_store.get::<_, Str, Unit>(&reader, UNHEALTHY_KEY) {
return Err(ResponseError::Maintenance); return Err(Error::Maintenance);
} }
Ok(HttpResponse::Ok().finish()) Ok(HttpResponse::Ok().finish())
} }
async fn set_healthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> { async fn set_healthy(data: web::Data<Data>) -> Result<HttpResponse, Error> {
let mut writer = data.db.main_write_txn()?; let mut writer = data.db.main_write_txn()?;
let common_store = data.db.common_store(); let common_store = data.db.common_store();
common_store.delete::<_, Str>(&mut writer, UNHEALTHY_KEY)?; common_store.delete::<_, Str>(&mut writer, UNHEALTHY_KEY)?;
@ -35,7 +35,7 @@ async fn set_healthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseErro
Ok(HttpResponse::Ok().finish()) Ok(HttpResponse::Ok().finish())
} }
async fn set_unhealthy(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> { async fn set_unhealthy(data: web::Data<Data>) -> Result<HttpResponse, Error> {
let mut writer = data.db.main_write_txn()?; let mut writer = data.db.main_write_txn()?;
let common_store = data.db.common_store(); let common_store = data.db.common_store();
common_store.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &())?; common_store.put::<_, Str, Unit>(&mut writer, UNHEALTHY_KEY, &())?;
@ -53,7 +53,7 @@ struct HealthBody {
async fn change_healthyness( async fn change_healthyness(
data: web::Data<Data>, data: web::Data<Data>,
body: web::Json<HealthBody>, body: web::Json<HealthBody>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
if body.health { if body.health {
set_healthy(data).await set_healthy(data).await
} else { } else {

View File

@ -5,7 +5,7 @@ use log::error;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::error::ResponseError; use crate::error::Error;
use crate::helpers::Authentication; use crate::helpers::Authentication;
use crate::routes::IndexParam; use crate::routes::IndexParam;
use crate::Data; use crate::Data;
@ -40,7 +40,7 @@ struct IndexResponse {
} }
#[get("/indexes", wrap = "Authentication::Private")] #[get("/indexes", wrap = "Authentication::Private")]
async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> { async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, Error> {
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let mut response = Vec::new(); let mut response = Vec::new();
@ -50,19 +50,19 @@ async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, ResponseErr
match index { match index {
Some(index) => { Some(index) => {
let name = index.main.name(&reader)?.ok_or(ResponseError::internal( let name = index.main.name(&reader)?.ok_or(Error::internal(
"Impossible to get the name of an index", "Impossible to get the name of an index",
))?; ))?;
let created_at = index let created_at = index
.main .main
.created_at(&reader)? .created_at(&reader)?
.ok_or(ResponseError::internal( .ok_or(Error::internal(
"Impossible to get the create date of an index", "Impossible to get the create date of an index",
))?; ))?;
let updated_at = index let updated_at = index
.main .main
.updated_at(&reader)? .updated_at(&reader)?
.ok_or(ResponseError::internal( .ok_or(Error::internal(
"Impossible to get the last update date of an index", "Impossible to get the last update date of an index",
))?; ))?;
@ -97,27 +97,27 @@ async fn list_indexes(data: web::Data<Data>) -> Result<HttpResponse, ResponseErr
async fn get_index( async fn get_index(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let name = index.main.name(&reader)?.ok_or(ResponseError::internal( let name = index.main.name(&reader)?.ok_or(Error::internal(
"Impossible to get the name of an index", "Impossible to get the name of an index",
))?; ))?;
let created_at = index let created_at = index
.main .main
.created_at(&reader)? .created_at(&reader)?
.ok_or(ResponseError::internal( .ok_or(Error::internal(
"Impossible to get the create date of an index", "Impossible to get the create date of an index",
))?; ))?;
let updated_at = index let updated_at = index
.main .main
.updated_at(&reader)? .updated_at(&reader)?
.ok_or(ResponseError::internal( .ok_or(Error::internal(
"Impossible to get the last update date of an index", "Impossible to get the last update date of an index",
))?; ))?;
@ -150,9 +150,9 @@ struct IndexCreateRequest {
async fn create_index( async fn create_index(
data: web::Data<Data>, data: web::Data<Data>,
body: web::Json<IndexCreateRequest>, body: web::Json<IndexCreateRequest>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
if let (None, None) = (body.name.clone(), body.uid.clone()) { if let (None, None) = (body.name.clone(), body.uid.clone()) {
return Err(ResponseError::bad_request( return Err(Error::bad_request(
"Index creation must have an uid", "Index creation must have an uid",
)); ));
} }
@ -165,7 +165,7 @@ async fn create_index(
{ {
uid.to_owned() uid.to_owned()
} else { } else {
return Err(ResponseError::InvalidIndexUid); return Err(Error::InvalidIndexUid);
} }
} }
None => loop { None => loop {
@ -179,7 +179,7 @@ async fn create_index(
let created_index = data let created_index = data
.db .db
.create_index(&uid) .create_index(&uid)
.map_err(ResponseError::create_index)?; .map_err(Error::create_index)?;
let mut writer = data.db.main_write_txn()?; let mut writer = data.db.main_write_txn()?;
@ -189,18 +189,18 @@ async fn create_index(
let created_at = created_index let created_at = created_index
.main .main
.created_at(&writer)? .created_at(&writer)?
.ok_or(ResponseError::internal("Impossible to read created at"))?; .ok_or(Error::internal("Impossible to read created at"))?;
let updated_at = created_index let updated_at = created_index
.main .main
.updated_at(&writer)? .updated_at(&writer)?
.ok_or(ResponseError::internal("Impossible to read updated at"))?; .ok_or(Error::internal("Impossible to read updated at"))?;
if let Some(id) = body.primary_key.clone() { if let Some(id) = body.primary_key.clone() {
if let Some(mut schema) = created_index.main.schema(&writer)? { if let Some(mut schema) = created_index.main.schema(&writer)? {
schema schema
.set_primary_key(&id) .set_primary_key(&id)
.map_err(ResponseError::bad_request)?; .map_err(Error::bad_request)?;
created_index.main.put_schema(&mut writer, &schema)?; created_index.main.put_schema(&mut writer, &schema)?;
} }
} }
@ -238,11 +238,11 @@ async fn update_index(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<IndexCreateRequest>, body: web::Json<IndexCreateRequest>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let mut writer = data.db.main_write_txn()?; let mut writer = data.db.main_write_txn()?;
@ -254,7 +254,7 @@ async fn update_index(
if let Some(mut schema) = index.main.schema(&writer)? { if let Some(mut schema) = index.main.schema(&writer)? {
match schema.primary_key() { match schema.primary_key() {
Some(_) => { Some(_) => {
return Err(ResponseError::bad_request( return Err(Error::bad_request(
"The primary key cannot be updated", "The primary key cannot be updated",
)); ));
} }
@ -271,19 +271,19 @@ async fn update_index(
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let name = index.main.name(&reader)?.ok_or(ResponseError::internal( let name = index.main.name(&reader)?.ok_or(Error::internal(
"Impossible to get the name of an index", "Impossible to get the name of an index",
))?; ))?;
let created_at = index let created_at = index
.main .main
.created_at(&reader)? .created_at(&reader)?
.ok_or(ResponseError::internal( .ok_or(Error::internal(
"Impossible to get the create date of an index", "Impossible to get the create date of an index",
))?; ))?;
let updated_at = index let updated_at = index
.main .main
.updated_at(&reader)? .updated_at(&reader)?
.ok_or(ResponseError::internal( .ok_or(Error::internal(
"Impossible to get the last update date of an index", "Impossible to get the last update date of an index",
))?; ))?;
@ -308,7 +308,7 @@ async fn update_index(
async fn delete_index( async fn delete_index(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
data.db.delete_index(&path.index_uid)?; data.db.delete_index(&path.index_uid)?;
Ok(HttpResponse::NoContent().finish()) Ok(HttpResponse::NoContent().finish())
@ -327,11 +327,11 @@ struct UpdateParam {
async fn get_update_status( async fn get_update_status(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<UpdateParam>, path: web::Path<UpdateParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.update_read_txn()?; let reader = data.db.update_read_txn()?;
@ -339,7 +339,7 @@ async fn get_update_status(
match status { match status {
Some(status) => Ok(HttpResponse::Ok().json(status)), Some(status) => Ok(HttpResponse::Ok().json(status)),
None => Err(ResponseError::NotFound(format!( None => Err(Error::NotFound(format!(
"Update {} not found", "Update {} not found",
path.update_id path.update_id
))), ))),
@ -350,11 +350,11 @@ async fn get_update_status(
async fn get_all_updates_status( async fn get_all_updates_status(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.update_read_txn()?; let reader = data.db.update_read_txn()?;

View File

@ -7,7 +7,7 @@ use actix_web_macros::get;
use serde::Deserialize; use serde::Deserialize;
use serde_json::Value; use serde_json::Value;
use crate::error::{ResponseError, FacetCountError}; use crate::error::{Error, FacetCountError};
use crate::helpers::meilisearch::IndexSearchExt; use crate::helpers::meilisearch::IndexSearchExt;
use crate::helpers::Authentication; use crate::helpers::Authentication;
use crate::routes::IndexParam; use crate::routes::IndexParam;
@ -41,18 +41,18 @@ async fn search_with_url_query(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
params: web::Query<SearchQuery>, params: web::Query<SearchQuery>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let schema = index let schema = index
.main .main
.schema(&reader)? .schema(&reader)?
.ok_or(ResponseError::internal("Impossible to retrieve the schema"))?; .ok_or(Error::internal("Impossible to retrieve the schema"))?;
let mut search_builder = index.new_search(params.q.clone()); let mut search_builder = index.new_search(params.q.clone());
@ -90,7 +90,7 @@ async fn search_with_url_query(
if let Some(ref facet_filters) = params.facet_filters { if let Some(ref facet_filters) = params.facet_filters {
match index.main.attributes_for_faceting(&reader)? { match index.main.attributes_for_faceting(&reader)? {
Some(ref attrs) => { search_builder.add_facet_filters(FacetFilter::from_str(facet_filters, &schema, attrs)?); }, Some(ref attrs) => { search_builder.add_facet_filters(FacetFilter::from_str(facet_filters, &schema, attrs)?); },
None => return Err(ResponseError::FacetExpression("can't filter on facets, as no facet is set".to_string())) None => return Err(Error::FacetExpression("can't filter on facets, as no facet is set".to_string()))
} }
} }

View File

@ -3,7 +3,7 @@ use actix_web_macros::{delete, get, post};
use meilisearch_core::settings::{Settings, SettingsUpdate, UpdateState, DEFAULT_RANKING_RULES}; use meilisearch_core::settings::{Settings, SettingsUpdate, UpdateState, DEFAULT_RANKING_RULES};
use std::collections::{BTreeMap, BTreeSet, HashSet}; use std::collections::{BTreeMap, BTreeSet, HashSet};
use crate::error::ResponseError; use crate::error::Error;
use crate::helpers::Authentication; use crate::helpers::Authentication;
use crate::routes::{IndexParam, IndexUpdateResponse}; use crate::routes::{IndexParam, IndexUpdateResponse};
use crate::Data; use crate::Data;
@ -33,17 +33,17 @@ async fn update_all(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<Settings>, body: web::Json<Settings>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let mut writer = data.db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = body let settings = body
.into_inner() .into_inner()
.into_update() .into_update()
.map_err(ResponseError::bad_request)?; .map_err(Error::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
@ -54,11 +54,11 @@ async fn update_all(
async fn get_all( async fn get_all(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
@ -134,11 +134,11 @@ async fn get_all(
async fn delete_all( async fn delete_all(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let mut writer = data.db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
@ -166,11 +166,11 @@ async fn delete_all(
async fn get_rules( async fn get_rules(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let ranking_rules = index let ranking_rules = index
@ -192,11 +192,11 @@ async fn update_rules(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<Option<Vec<String>>>, body: web::Json<Option<Vec<String>>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let settings = Settings { let settings = Settings {
ranking_rules: Some(body.into_inner()), ranking_rules: Some(body.into_inner()),
@ -204,7 +204,7 @@ async fn update_rules(
}; };
let mut writer = data.db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = settings.into_update().map_err(ResponseError::bad_request)?; let settings = settings.into_update().map_err(Error::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
@ -218,11 +218,11 @@ async fn update_rules(
async fn delete_rules( async fn delete_rules(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let mut writer = data.db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
@ -244,11 +244,11 @@ async fn delete_rules(
async fn get_distinct( async fn get_distinct(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let distinct_attribute = index.main.distinct_attribute(&reader)?; let distinct_attribute = index.main.distinct_attribute(&reader)?;
@ -263,11 +263,11 @@ async fn update_distinct(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<Option<String>>, body: web::Json<Option<String>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let settings = Settings { let settings = Settings {
distinct_attribute: Some(body.into_inner()), distinct_attribute: Some(body.into_inner()),
@ -275,7 +275,7 @@ async fn update_distinct(
}; };
let mut writer = data.db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = settings.into_update().map_err(ResponseError::bad_request)?; let settings = settings.into_update().map_err(Error::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
@ -289,11 +289,11 @@ async fn update_distinct(
async fn delete_distinct( async fn delete_distinct(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let mut writer = data.db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
@ -315,11 +315,11 @@ async fn delete_distinct(
async fn get_searchable( async fn get_searchable(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
let searchable_attributes: Option<Vec<String>> = let searchable_attributes: Option<Vec<String>> =
@ -336,11 +336,11 @@ async fn update_searchable(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<Option<Vec<String>>>, body: web::Json<Option<Vec<String>>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let settings = Settings { let settings = Settings {
searchable_attributes: Some(body.into_inner()), searchable_attributes: Some(body.into_inner()),
@ -348,7 +348,7 @@ async fn update_searchable(
}; };
let mut writer = data.db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = settings.into_update().map_err(ResponseError::bad_request)?; let settings = settings.into_update().map_err(Error::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
@ -362,11 +362,11 @@ async fn update_searchable(
async fn delete_searchable( async fn delete_searchable(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
searchable_attributes: UpdateState::Clear, searchable_attributes: UpdateState::Clear,
@ -387,11 +387,11 @@ async fn delete_searchable(
async fn get_displayed( async fn get_displayed(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
@ -410,11 +410,11 @@ async fn update_displayed(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<Option<HashSet<String>>>, body: web::Json<Option<HashSet<String>>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let settings = Settings { let settings = Settings {
displayed_attributes: Some(body.into_inner()), displayed_attributes: Some(body.into_inner()),
@ -422,7 +422,7 @@ async fn update_displayed(
}; };
let mut writer = data.db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = settings.into_update().map_err(ResponseError::bad_request)?; let settings = settings.into_update().map_err(Error::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;
@ -436,11 +436,11 @@ async fn update_displayed(
async fn delete_displayed( async fn delete_displayed(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
displayed_attributes: UpdateState::Clear, displayed_attributes: UpdateState::Clear,
@ -461,11 +461,11 @@ async fn delete_displayed(
async fn get_accept_new_fields( async fn get_accept_new_fields(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let schema = index.main.schema(&reader)?; let schema = index.main.schema(&reader)?;
@ -483,11 +483,11 @@ async fn update_accept_new_fields(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<Option<bool>>, body: web::Json<Option<bool>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let settings = Settings { let settings = Settings {
accept_new_fields: Some(body.into_inner()), accept_new_fields: Some(body.into_inner()),
@ -495,7 +495,7 @@ async fn update_accept_new_fields(
}; };
let mut writer = data.db.update_write_txn()?; let mut writer = data.db.update_write_txn()?;
let settings = settings.into_update().map_err(ResponseError::bad_request)?; let settings = settings.into_update().map_err(Error::bad_request)?;
let update_id = index.settings_update(&mut writer, settings)?; let update_id = index.settings_update(&mut writer, settings)?;
writer.commit()?; writer.commit()?;

View File

@ -10,7 +10,7 @@ use serde::Serialize;
use sysinfo::{NetworkExt, ProcessExt, ProcessorExt, System, SystemExt}; use sysinfo::{NetworkExt, ProcessExt, ProcessorExt, System, SystemExt};
use walkdir::WalkDir; use walkdir::WalkDir;
use crate::error::ResponseError; use crate::error::Error;
use crate::helpers::Authentication; use crate::helpers::Authentication;
use crate::routes::IndexParam; use crate::routes::IndexParam;
use crate::Data; use crate::Data;
@ -35,11 +35,11 @@ struct IndexStatsResponse {
async fn index_stats( async fn index_stats(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
@ -51,7 +51,7 @@ async fn index_stats(
let is_indexing = let is_indexing =
data.is_indexing(&update_reader, &path.index_uid)? data.is_indexing(&update_reader, &path.index_uid)?
.ok_or(ResponseError::internal( .ok_or(Error::internal(
"Impossible to know if the database is indexing", "Impossible to know if the database is indexing",
))?; ))?;
@ -71,7 +71,7 @@ struct StatsResult {
} }
#[get("/stats", wrap = "Authentication::Private")] #[get("/stats", wrap = "Authentication::Private")]
async fn get_stats(data: web::Data<Data>) -> Result<HttpResponse, ResponseError> { async fn get_stats(data: web::Data<Data>) -> Result<HttpResponse, Error> {
let mut index_list = HashMap::new(); let mut index_list = HashMap::new();
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
@ -87,7 +87,7 @@ async fn get_stats(data: web::Data<Data>) -> Result<HttpResponse, ResponseError>
let fields_distribution = index.main.fields_distribution(&reader)?.unwrap_or_default(); let fields_distribution = index.main.fields_distribution(&reader)?.unwrap_or_default();
let is_indexing = data.is_indexing(&update_reader, &index_uid)?.ok_or( let is_indexing = data.is_indexing(&update_reader, &index_uid)?.ok_or(
ResponseError::internal("Impossible to know if the database is indexing"), Error::internal("Impossible to know if the database is indexing"),
)?; )?;
let response = IndexStatsResponse { let response = IndexStatsResponse {

View File

@ -3,7 +3,7 @@ use actix_web_macros::{delete, get, post};
use meilisearch_core::settings::{SettingsUpdate, UpdateState}; use meilisearch_core::settings::{SettingsUpdate, UpdateState};
use std::collections::BTreeSet; use std::collections::BTreeSet;
use crate::error::ResponseError; use crate::error::Error;
use crate::helpers::Authentication; use crate::helpers::Authentication;
use crate::routes::{IndexParam, IndexUpdateResponse}; use crate::routes::{IndexParam, IndexUpdateResponse};
use crate::Data; use crate::Data;
@ -19,11 +19,11 @@ pub fn services(cfg: &mut web::ServiceConfig) {
async fn get( async fn get(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
let stop_words_fst = index.main.stop_words_fst(&reader)?; let stop_words_fst = index.main.stop_words_fst(&reader)?;
let stop_words = stop_words_fst.stream().into_strs()?; let stop_words = stop_words_fst.stream().into_strs()?;
@ -39,11 +39,11 @@ async fn update(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<BTreeSet<String>>, body: web::Json<BTreeSet<String>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
stop_words: UpdateState::Update(body.into_inner()), stop_words: UpdateState::Update(body.into_inner()),
@ -64,11 +64,11 @@ async fn update(
async fn delete( async fn delete(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
stop_words: UpdateState::Clear, stop_words: UpdateState::Clear,

View File

@ -5,7 +5,7 @@ use actix_web_macros::{delete, get, post};
use indexmap::IndexMap; use indexmap::IndexMap;
use meilisearch_core::settings::{SettingsUpdate, UpdateState}; use meilisearch_core::settings::{SettingsUpdate, UpdateState};
use crate::error::ResponseError; use crate::error::Error;
use crate::helpers::Authentication; use crate::helpers::Authentication;
use crate::routes::{IndexParam, IndexUpdateResponse}; use crate::routes::{IndexParam, IndexUpdateResponse};
use crate::Data; use crate::Data;
@ -21,11 +21,11 @@ pub fn services(cfg: &mut web::ServiceConfig) {
async fn get( async fn get(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let reader = data.db.main_read_txn()?; let reader = data.db.main_read_txn()?;
@ -51,11 +51,11 @@ async fn update(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
body: web::Json<BTreeMap<String, Vec<String>>>, body: web::Json<BTreeMap<String, Vec<String>>>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
synonyms: UpdateState::Update(body.into_inner()), synonyms: UpdateState::Update(body.into_inner()),
@ -76,11 +76,11 @@ async fn update(
async fn delete( async fn delete(
data: web::Data<Data>, data: web::Data<Data>,
path: web::Path<IndexParam>, path: web::Path<IndexParam>,
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, Error> {
let index = data let index = data
.db .db
.open_index(&path.index_uid) .open_index(&path.index_uid)
.ok_or(ResponseError::index_not_found(&path.index_uid))?; .ok_or(Error::index_not_found(&path.index_uid))?;
let settings = SettingsUpdate { let settings = SettingsUpdate {
synonyms: UpdateState::Clear, synonyms: UpdateState::Clear,