Implement ErrorCode on the milli::Error type

This commit is contained in:
Kerollmops 2022-10-04 10:38:17 +02:00 committed by Clément Renault
parent d76634a36c
commit 91e13c2824
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
6 changed files with 45 additions and 10 deletions

1
Cargo.lock generated
View File

@ -2483,6 +2483,7 @@ name = "meilisearch-types"
version = "0.29.1" version = "0.29.1"
dependencies = [ dependencies = [
"actix-web", "actix-web",
"milli 0.33.0",
"proptest", "proptest",
"proptest-derive", "proptest-derive",
"serde", "serde",

View File

@ -13,7 +13,6 @@ pub mod metrics;
pub mod route_metrics; pub mod route_metrics;
use std::sync::{atomic::AtomicBool, Arc}; use std::sync::{atomic::AtomicBool, Arc};
use std::time::Duration;
use crate::error::MeilisearchHttpError; use crate::error::MeilisearchHttpError;
use actix_web::error::JsonPayloadError; use actix_web::error::JsonPayloadError;
@ -21,7 +20,6 @@ use actix_web::web::Data;
use analytics::Analytics; use analytics::Analytics;
use error::PayloadError; use error::PayloadError;
use http::header::CONTENT_TYPE; use http::header::CONTENT_TYPE;
use index_scheduler::milli::update::IndexerConfig;
pub use option::Opt; pub use option::Opt;
use actix_web::{web, HttpRequest}; use actix_web::{web, HttpRequest};
@ -29,7 +27,6 @@ use actix_web::{web, HttpRequest};
use extractors::payload::PayloadConfig; use extractors::payload::PayloadConfig;
use index_scheduler::IndexScheduler; use index_scheduler::IndexScheduler;
use meilisearch_auth::AuthController; use meilisearch_auth::AuthController;
use sysinfo::{RefreshKind, System, SystemExt};
pub static AUTOBATCHING_ENABLED: AtomicBool = AtomicBool::new(false); pub static AUTOBATCHING_ENABLED: AtomicBool = AtomicBool::new(false);

View File

@ -1,9 +1,5 @@
use std::convert::TryFrom;
use std::sync::Arc;
use actix_web::web::Data; use actix_web::web::Data;
use actix_web::{web, HttpRequest, HttpResponse}; use actix_web::{web, HttpRequest, HttpResponse};
use index::Index;
use index_scheduler::milli::FieldDistribution; use index_scheduler::milli::FieldDistribution;
use index_scheduler::{IndexScheduler, KindWithContent, Query, Status}; use index_scheduler::{IndexScheduler, KindWithContent, Query, Status};
use log::debug; use log::debug;
@ -74,7 +70,6 @@ pub async fn list_indexes(
) -> Result<HttpResponse, ResponseError> { ) -> Result<HttpResponse, ResponseError> {
let search_rules = &index_scheduler.filters().search_rules; let search_rules = &index_scheduler.filters().search_rules;
let indexes: Vec<_> = index_scheduler.indexes()?; let indexes: Vec<_> = index_scheduler.indexes()?;
let nb_indexes = indexes.len();
let indexes = indexes let indexes = indexes
.iter() .iter()
.filter(|index| search_rules.is_index_authorized(&index.name)) .filter(|index| search_rules.is_index_authorized(&index.name))

View File

@ -16,7 +16,7 @@ use meilisearch_types::star_or::StarOr;
use crate::analytics::Analytics; use crate::analytics::Analytics;
use crate::extractors::authentication::{policies::*, GuardedData}; use crate::extractors::authentication::{policies::*, GuardedData};
use self::indexes::{IndexStats, IndexView}; use self::indexes::IndexStats;
mod api_key; mod api_key;
mod dump; mod dump;

View File

@ -6,11 +6,12 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = { version = "4.2.1", default-features = false } actix-web = { version = "4.2.1", default-features = false }
tokio = "1.0" milli = { git = "https://github.com/meilisearch/milli.git", tag = "v0.33.0" }
proptest = { version = "1.0.0", optional = true } proptest = { version = "1.0.0", optional = true }
proptest-derive = { version = "0.3.0", optional = true } proptest-derive = { version = "0.3.0", optional = true }
serde = { version = "1.0.145", features = ["derive"] } serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.85" serde_json = "1.0.85"
tokio = "1.0"
[features] [features]
test-traits = ["proptest", "proptest-derive"] test-traits = ["proptest", "proptest-derive"]

View File

@ -351,6 +351,47 @@ impl ErrorCode for JoinError {
} }
} }
impl ErrorCode for milli::Error {
fn error_code(&self) -> Code {
use milli::{Error, UserError};
match self {
Error::InternalError(_) => Code::Internal,
Error::IoError(_) => Code::Internal,
Error::UserError(ref error) => {
match error {
// TODO: wait for spec for new error codes.
UserError::SerdeJson(_)
| UserError::InvalidLmdbOpenOptions
| UserError::DocumentLimitReached
| UserError::AccessingSoftDeletedDocument { .. }
| UserError::UnknownInternalDocumentId { .. } => Code::Internal,
UserError::InvalidStoreFile => Code::InvalidStore,
UserError::NoSpaceLeftOnDevice => Code::NoSpaceLeftOnDevice,
UserError::MaxDatabaseSizeReached => Code::DatabaseSizeLimitReached,
UserError::AttributeLimitReached => Code::MaxFieldsLimitExceeded,
UserError::InvalidFilter(_) => Code::Filter,
UserError::MissingDocumentId { .. } => Code::MissingDocumentId,
UserError::InvalidDocumentId { .. } | UserError::TooManyDocumentIds { .. } => {
Code::InvalidDocumentId
}
UserError::MissingPrimaryKey => Code::MissingPrimaryKey,
UserError::PrimaryKeyCannotBeChanged(_) => Code::PrimaryKeyAlreadyPresent,
UserError::SortRankingRuleMissing => Code::Sort,
UserError::InvalidFacetsDistribution { .. } => Code::BadRequest,
UserError::InvalidSortableAttribute { .. } => Code::Sort,
UserError::CriterionError(_) => Code::InvalidRankingRule,
UserError::InvalidGeoField { .. } => Code::InvalidGeoField,
UserError::SortError(_) => Code::Sort,
UserError::InvalidMinTypoWordLenSetting(_, _) => {
Code::InvalidMinWordLengthForTypo
}
}
}
}
}
}
#[cfg(feature = "test-traits")] #[cfg(feature = "test-traits")]
mod strategy { mod strategy {
use proptest::strategy::Strategy; use proptest::strategy::Strategy;