backend: impl From<heed::Error> for RuntimeError

This commit is contained in:
hinto.janai 2024-02-13 17:50:04 -05:00
parent 6eb0981829
commit ddce2fa70c
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
3 changed files with 75 additions and 20 deletions

View file

@ -0,0 +1,61 @@
//! Conversion from `heed::Error` -> `cuprate_database::RuntimeError`.
//---------------------------------------------------------------------------------------------------- Import
use std::borrow::Cow;
//---------------------------------------------------------------------------------------------------- Error
impl From<heed::Error> for crate::RuntimeError {
fn from(error: heed::Error) -> Self {
use heed::Error as E1;
use heed::MdbError as E2;
match error {
// I/O errors.
E1::Io(io_error) => Self::Io(io_error),
// LMDB errors.
E1::Mdb(mdb_error) => match mdb_error {
E2::KeyExist => Self::KeyExists,
E2::NotFound => Self::KeyNotFound,
E2::VersionMismatch => Self::VersionMismatch,
E2::MapFull => Self::MapFull,
E2::ReadersFull => Self::ReadersFull,
E2::PageFull => Self::PageFull,
E2::Other(c_int) => {
Self::Unknown(Cow::Owned(format!("heed::Error::Other({c_int})")))
}
E2::DbsFull
| E2::PageNotFound
| E2::Corrupted
| E2::Panic
| E2::Invalid
| E2::TlsFull
| E2::TxnFull
| E2::CursorFull
| E2::MapResized
| E2::Incompatible
| E2::BadRslot
| E2::BadTxn
| E2::BadValSize
| E2::BadDbi
| E2::Problem => Self::Unknown(Cow::from(std::any::type_name_of_val(&mdb_error))),
},
// Database is shutting down.
E1::DatabaseClosing => Self::ShuttingDown,
// TODO: these will never occur once correct?
// TODO: (de)serialization is infallible?
E1::InvalidDatabaseTyping
| E1::BadOpenOptions { .. }
| E1::Encoding(_)
| E1::Decoding(_) => unreachable!(),
}
}
}
//---------------------------------------------------------------------------------------------------- Tests
#[cfg(test)]
mod test {
// use super::*;
}

View file

@ -3,6 +3,8 @@
mod env; mod env;
pub use env::ConcreteEnv; pub use env::ConcreteEnv;
mod error;
mod database; mod database;
mod serde; mod serde;

View file

@ -4,8 +4,6 @@
//---------------------------------------------------------------------------------------------------- Import //---------------------------------------------------------------------------------------------------- Import
use std::{borrow::Cow, fmt::Debug}; use std::{borrow::Cow, fmt::Debug};
use crate::constants::DATABASE_BACKEND;
//---------------------------------------------------------------------------------------------------- InitError //---------------------------------------------------------------------------------------------------- InitError
/// Database errors that occur during initialization. /// Database errors that occur during initialization.
/// ///
@ -45,18 +43,12 @@ pub enum InitError {
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum RuntimeError { pub enum RuntimeError {
/// The given key already existed in the database. /// The given key already existed in the database.
/// #[error("key already existed")]
/// The string inside is the output of KeyExists,
/// [`std::any::type_name`] on the key type.
#[error("key of type `{0}` already existed")]
KeyExists(&'static str),
/// The given key did not exist in the database. /// The given key did not exist in the database.
/// #[error("key/value pair was not found")]
/// The string inside is the output of KeyNotFound,
/// [`std::any::type_name`] on the key type.
#[error("key/value pair was not found: {0}")]
KeyNotFound(&'static str),
/// The database environment has reached /// The database environment has reached
/// maximum memory map size, it must be /// maximum memory map size, it must be
@ -76,21 +68,21 @@ pub enum RuntimeError {
#[error("I/O error: {0}")] #[error("I/O error: {0}")]
Io(#[from] std::io::Error), Io(#[from] std::io::Error),
/// The database is currently in the process
/// of shutting down and cannot respond.
#[error("database is shutting down")]
ShuttingDown,
/// The expected database version was not the version found. /// The expected database version was not the version found.
#[error("database version mismatch: expected {expected}, found {found}")] #[error("database version mismatch")]
VersionMismatch { VersionMismatch,
/// The expected database version.
expected: &'static str,
/// The database version found.
found: &'static str,
},
/// The database has reached maximum parallel readers. /// The database has reached maximum parallel readers.
/// ///
/// TODO: this can be used for retry logic in reader threads, /// TODO: this can be used for retry logic in reader threads,
/// although, does this error ever actually occur in practice? /// although, does this error ever actually occur in practice?
#[error("database maximum parallel readers reached")] #[error("database maximum parallel readers reached")]
MaxReaders, ReadersFull,
// TODO: this could be removed once we have all errors figured out. // TODO: this could be removed once we have all errors figured out.
/// An unknown error occurred. /// An unknown error occurred.