2021-06-10 17:31:08 +02:00
|
|
|
use std::error::Error as StdError;
|
|
|
|
use std::{fmt, io};
|
2021-06-09 17:05:46 +02:00
|
|
|
|
2021-06-10 15:55:22 +02:00
|
|
|
use heed::{MdbError, Error as HeedError};
|
2021-06-10 17:31:08 +02:00
|
|
|
use serde_json::{Map, Value};
|
|
|
|
|
|
|
|
use crate::{DocumentId, FieldId};
|
2021-06-10 15:55:22 +02:00
|
|
|
|
2021-06-10 17:31:08 +02:00
|
|
|
pub type Object = Map<String, Value>;
|
2021-06-09 17:05:46 +02:00
|
|
|
|
2021-06-10 17:31:08 +02:00
|
|
|
#[derive(Debug)]
|
2021-06-09 17:05:46 +02:00
|
|
|
pub enum Error {
|
|
|
|
InternalError(InternalError),
|
|
|
|
IoError(io::Error),
|
|
|
|
UserError(UserError),
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:31:08 +02:00
|
|
|
#[derive(Debug)]
|
2021-06-09 17:05:46 +02:00
|
|
|
pub enum InternalError {
|
2021-06-10 17:31:08 +02:00
|
|
|
DatabaseMissingEntry { db_name: &'static str, key: Option<&'static str> },
|
2021-06-09 17:05:46 +02:00
|
|
|
FieldIdMapMissingEntry(FieldIdMapMissingEntry),
|
2021-06-10 17:31:08 +02:00
|
|
|
IndexingMergingKeys { process: &'static str },
|
2021-06-10 15:55:22 +02:00
|
|
|
SerializationError(SerializationError),
|
|
|
|
StoreError(MdbError),
|
|
|
|
InvalidDatabaseTyping,
|
|
|
|
DatabaseClosing,
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:31:08 +02:00
|
|
|
#[derive(Debug)]
|
2021-06-10 15:55:22 +02:00
|
|
|
pub enum SerializationError {
|
|
|
|
Decoding { db_name: Option<&'static str> },
|
|
|
|
Encoding { db_name: Option<&'static str> },
|
|
|
|
InvalidNumberSerialization,
|
2021-06-09 17:05:46 +02:00
|
|
|
}
|
|
|
|
|
2021-06-10 17:31:08 +02:00
|
|
|
#[derive(Debug)]
|
2021-06-09 17:05:46 +02:00
|
|
|
pub enum FieldIdMapMissingEntry {
|
2021-06-10 17:31:08 +02:00
|
|
|
FieldId { field_id: FieldId, from_db_name: &'static str },
|
|
|
|
FieldName { field_name: String, from_db_name: &'static str },
|
2021-06-09 17:05:46 +02:00
|
|
|
}
|
|
|
|
|
2021-06-10 17:31:08 +02:00
|
|
|
#[derive(Debug)]
|
2021-06-09 17:05:46 +02:00
|
|
|
pub enum UserError {
|
2021-06-10 15:55:22 +02:00
|
|
|
AttributeLimitReached,
|
|
|
|
DocumentLimitReached,
|
|
|
|
InvalidCriterionName { name: String },
|
2021-06-10 17:31:08 +02:00
|
|
|
InvalidDocumentId { document_id: Value },
|
2021-06-10 15:55:22 +02:00
|
|
|
MissingDocumentId { document: Object },
|
|
|
|
MissingPrimaryKey,
|
|
|
|
DatabaseSizeReached,
|
|
|
|
NoSpaceLeftOnDevice,
|
|
|
|
InvalidStoreFile,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(error: io::Error) -> Error {
|
|
|
|
// TODO must be improved and more precise
|
|
|
|
Error::IoError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<HeedError> for Error {
|
|
|
|
fn from(error: HeedError) -> Error {
|
|
|
|
use self::Error::*;
|
|
|
|
use self::InternalError::*;
|
|
|
|
use self::SerializationError::*;
|
|
|
|
use self::UserError::*;
|
2021-06-09 17:05:46 +02:00
|
|
|
|
2021-06-10 15:55:22 +02:00
|
|
|
match error {
|
|
|
|
HeedError::Io(error) => Error::from(error),
|
|
|
|
HeedError::Mdb(MdbError::MapFull) => UserError(DatabaseSizeReached),
|
|
|
|
HeedError::Mdb(MdbError::Invalid) => UserError(InvalidStoreFile),
|
|
|
|
HeedError::Mdb(error) => InternalError(StoreError(error)),
|
|
|
|
HeedError::Encoding => InternalError(SerializationError(Encoding { db_name: None })),
|
|
|
|
HeedError::Decoding => InternalError(SerializationError(Decoding { db_name: None })),
|
|
|
|
HeedError::InvalidDatabaseTyping => InternalError(InvalidDatabaseTyping),
|
|
|
|
HeedError::DatabaseClosing => InternalError(DatabaseClosing),
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 17:05:46 +02:00
|
|
|
}
|
2021-06-10 17:31:08 +02:00
|
|
|
|
|
|
|
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 {}
|