MeiliSearch/meilisearch-lib/src/error.rs

61 lines
2.3 KiB
Rust
Raw Normal View History

use std::error::Error;
use std::fmt;
use meilisearch_error::{Code, ErrorCode};
use milli::UserError;
macro_rules! internal_error {
($target:ty : $($other:path), *) => {
$(
impl From<$other> for $target {
fn from(other: $other) -> Self {
Self::Internal(Box::new(other))
}
}
)*
}
}
#[derive(Debug)]
pub struct MilliError<'a>(pub &'a milli::Error);
impl Error for MilliError<'_> {}
impl fmt::Display for MilliError<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl ErrorCode for MilliError<'_> {
fn error_code(&self) -> Code {
match self.0 {
milli::Error::InternalError(_) => Code::Internal,
milli::Error::IoError(_) => Code::Internal,
milli::Error::UserError(ref error) => {
match error {
// TODO: wait for spec for new error codes.
2021-09-27 15:41:14 +02:00
UserError::SerdeJson(_)
| UserError::MaxDatabaseSizeReached
| UserError::InvalidStoreFile
| UserError::NoSpaceLeftOnDevice
2021-10-26 19:36:48 +02:00
| UserError::DocumentLimitReached
| UserError::UnknownInternalDocumentId { .. } => Code::Internal,
UserError::AttributeLimitReached => Code::MaxFieldsLimitExceeded,
UserError::InvalidFilter(_) => Code::Filter,
UserError::MissingDocumentId { .. } => Code::MissingDocumentId,
2021-10-26 19:36:48 +02:00
UserError::InvalidDocumentId { .. } => Code::InvalidDocumentId,
UserError::MissingPrimaryKey => Code::MissingPrimaryKey,
2021-10-26 19:36:48 +02:00
UserError::PrimaryKeyCannotBeChanged(_) => Code::PrimaryKeyAlreadyPresent,
UserError::SortRankingRuleMissing => Code::Sort,
UserError::InvalidFacetsDistribution { .. } => Code::BadRequest,
2021-09-28 14:49:13 +02:00
UserError::InvalidSortableAttribute { .. } => Code::Sort,
2021-09-27 15:41:14 +02:00
UserError::CriterionError(_) => Code::InvalidRankingRule,
UserError::InvalidGeoField { .. } => Code::InvalidGeoField,
2021-09-28 14:49:13 +02:00
UserError::SortError(_) => Code::Sort,
}
}
}
}
}