mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-30 09:04:59 +08:00
Implement the Display trait on the Error type
This commit is contained in:
parent
44c353fafd
commit
456541e921
@ -1,60 +1,50 @@
|
|||||||
use std::io;
|
use std::error::Error as StdError;
|
||||||
|
use std::{fmt, io};
|
||||||
|
|
||||||
|
use heed::{MdbError, Error as HeedError};
|
||||||
|
use serde_json::{Map, Value};
|
||||||
|
|
||||||
use crate::{DocumentId, FieldId};
|
use crate::{DocumentId, FieldId};
|
||||||
use heed::{MdbError, Error as HeedError};
|
|
||||||
|
|
||||||
pub type Object = serde_json::Map<String, serde_json::Value>;
|
pub type Object = Map<String, Value>;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
InternalError(InternalError),
|
InternalError(InternalError),
|
||||||
IoError(io::Error),
|
IoError(io::Error),
|
||||||
UserError(UserError),
|
UserError(UserError),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum InternalError {
|
pub enum InternalError {
|
||||||
DatabaseMissingEntry(DatabaseMissingEntry),
|
DatabaseMissingEntry { db_name: &'static str, key: Option<&'static str> },
|
||||||
FieldIdMapMissingEntry(FieldIdMapMissingEntry),
|
FieldIdMapMissingEntry(FieldIdMapMissingEntry),
|
||||||
IndexingMergingKeys(IndexingMergingKeys),
|
IndexingMergingKeys { process: &'static str },
|
||||||
SerializationError(SerializationError),
|
SerializationError(SerializationError),
|
||||||
StoreError(MdbError),
|
StoreError(MdbError),
|
||||||
InvalidDatabaseTyping,
|
InvalidDatabaseTyping,
|
||||||
DatabaseClosing,
|
DatabaseClosing,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum SerializationError {
|
pub enum SerializationError {
|
||||||
Decoding { db_name: Option<&'static str> },
|
Decoding { db_name: Option<&'static str> },
|
||||||
Encoding { db_name: Option<&'static str> },
|
Encoding { db_name: Option<&'static str> },
|
||||||
InvalidNumberSerialization,
|
InvalidNumberSerialization,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum IndexingMergingKeys {
|
#[derive(Debug)]
|
||||||
DocIdWordPosition,
|
|
||||||
Document,
|
|
||||||
MainFstDeserialization,
|
|
||||||
WordLevelPositionDocids,
|
|
||||||
WordPrefixLevelPositionDocids,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum FieldIdMapMissingEntry {
|
pub enum FieldIdMapMissingEntry {
|
||||||
DisplayedFieldId { field_id: FieldId },
|
FieldId { field_id: FieldId, from_db_name: &'static str },
|
||||||
DisplayedFieldName { field_name: String },
|
FieldName { field_name: String, from_db_name: &'static str },
|
||||||
FacetedFieldName { field_name: String },
|
|
||||||
FilterableFieldName { field_name: String },
|
|
||||||
SearchableFieldName { field_name: String },
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum DatabaseMissingEntry {
|
|
||||||
DocumentId { internal_id: DocumentId },
|
|
||||||
FacetValuesDocids,
|
|
||||||
IndexCreationTime,
|
|
||||||
IndexUpdateTime,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum UserError {
|
pub enum UserError {
|
||||||
AttributeLimitReached,
|
AttributeLimitReached,
|
||||||
DocumentLimitReached,
|
DocumentLimitReached,
|
||||||
InvalidCriterionName { name: String },
|
InvalidCriterionName { name: String },
|
||||||
InvalidDocumentId { document_id: DocumentId },
|
InvalidDocumentId { document_id: Value },
|
||||||
MissingDocumentId { document: Object },
|
MissingDocumentId { document: Object },
|
||||||
MissingPrimaryKey,
|
MissingPrimaryKey,
|
||||||
DatabaseSizeReached,
|
DatabaseSizeReached,
|
||||||
@ -88,3 +78,93 @@ impl From<HeedError> for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::InternalError(error) => write!(f, "internal: {}", error),
|
||||||
|
Self::IoError(error) => error.fmt(f),
|
||||||
|
Self::UserError(error) => error.fmt(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StdError for Error {}
|
||||||
|
|
||||||
|
impl fmt::Display for InternalError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::DatabaseMissingEntry { db_name, key } => {
|
||||||
|
write!(f, "missing {} in the {} database", key.unwrap_or("key"), db_name)
|
||||||
|
},
|
||||||
|
Self::FieldIdMapMissingEntry(error) => error.fmt(f),
|
||||||
|
Self::IndexingMergingKeys { process } => {
|
||||||
|
write!(f, "invalid merge while processing {}", process)
|
||||||
|
},
|
||||||
|
Self::SerializationError(error) => error.fmt(f),
|
||||||
|
Self::StoreError(error) => error.fmt(f),
|
||||||
|
Self::InvalidDatabaseTyping => HeedError::InvalidDatabaseTyping.fmt(f),
|
||||||
|
Self::DatabaseClosing => HeedError::DatabaseClosing.fmt(f),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StdError for InternalError {}
|
||||||
|
|
||||||
|
impl fmt::Display for UserError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::AttributeLimitReached => f.write_str("maximum number of attributes reached"),
|
||||||
|
Self::DocumentLimitReached => f.write_str("maximum number of documents reached"),
|
||||||
|
Self::InvalidCriterionName { name } => write!(f, "invalid criterion {}", name),
|
||||||
|
Self::InvalidDocumentId { document_id } => {
|
||||||
|
let json = serde_json::to_string(document_id).unwrap();
|
||||||
|
write!(f, "document identifier is invalid {}", json)
|
||||||
|
},
|
||||||
|
Self::MissingDocumentId { document } => {
|
||||||
|
let json = serde_json::to_string(document).unwrap();
|
||||||
|
write!(f, "document doesn't have an identifier {}", json)
|
||||||
|
},
|
||||||
|
Self::MissingPrimaryKey => f.write_str("missing primary key"),
|
||||||
|
Self::DatabaseSizeReached => f.write_str("database size reached"),
|
||||||
|
// TODO where can we find it instead of writing the text ourselves?
|
||||||
|
Self::NoSpaceLeftOnDevice => f.write_str("no space left on device"),
|
||||||
|
Self::InvalidStoreFile => f.write_str("store file is not a valid database file"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StdError for UserError {}
|
||||||
|
|
||||||
|
impl fmt::Display for FieldIdMapMissingEntry {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::FieldId { field_id, from_db_name } => {
|
||||||
|
write!(f, "unknown field id {} coming from {} database", field_id, from_db_name)
|
||||||
|
},
|
||||||
|
Self::FieldName { field_name, from_db_name } => {
|
||||||
|
write!(f, "unknown field name {} coming from {} database", field_name, from_db_name)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StdError for FieldIdMapMissingEntry {}
|
||||||
|
|
||||||
|
impl fmt::Display for SerializationError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::Decoding { db_name: Some(name) } => {
|
||||||
|
write!(f, "decoding from the {} database failed", name)
|
||||||
|
},
|
||||||
|
Self::Decoding { db_name: None } => f.write_str("decoding failed"),
|
||||||
|
Self::Encoding { db_name: Some(name) } => {
|
||||||
|
write!(f, "encoding into the {} database failed", name)
|
||||||
|
},
|
||||||
|
Self::Encoding { db_name: None } => f.write_str("encoding failed"),
|
||||||
|
Self::InvalidNumberSerialization => f.write_str("number is not a valid finite number"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StdError for SerializationError {}
|
||||||
|
Loading…
Reference in New Issue
Block a user