cuprate_database/
error.rs

1//! Database error types.
2
3//---------------------------------------------------------------------------------------------------- Import
4use std::fmt::Debug;
5
6//---------------------------------------------------------------------------------------------------- Types
7/// Alias for a thread-safe boxed error.
8type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
9
10/// [`Result`] with [`RuntimeError`] as the error.
11pub type DbResult<T> = Result<T, RuntimeError>;
12
13//---------------------------------------------------------------------------------------------------- InitError
14/// Errors that occur during ([`Env::open`](crate::env::Env::open)).
15///
16/// # Handling
17/// As this is a database initialization error, the correct
18/// way to handle any of these occurring is probably just to
19/// exit the program.
20///
21/// There is not much we as Cuprate can do
22/// to recover from any of these errors.
23#[derive(thiserror::Error, Debug)]
24pub enum InitError {
25    /// The given `Path/File` existed and was accessible,
26    /// but was not a valid database file.
27    ///
28    /// This error can sometimes be returned after an
29    /// initial corruption error over [`InitError::Corrupt`].
30    #[error("database file exists but is not valid")]
31    Invalid,
32
33    /// The given `Path/File` existed, was a valid
34    /// database, but the version is incorrect.
35    #[error("database file is valid, but version is incorrect")]
36    InvalidVersion,
37
38    /// I/O error.
39    #[error("database I/O error: {0}")]
40    Io(#[from] std::io::Error),
41
42    /// The given `Path/File` existed,
43    /// was a valid database, but it is corrupt.
44    #[error("database file is corrupt")]
45    Corrupt,
46
47    /// The database is currently in the process
48    /// of shutting down and cannot respond.
49    ///
50    /// # Notes
51    /// This error can only occur with the `heed` backend when
52    /// the database environment is opened _right_ at the same time
53    /// another thread/process is closing it.
54    ///
55    /// This will never occur with other backends.
56    #[error("database is shutting down")]
57    ShuttingDown,
58
59    /// An unknown error occurred.
60    ///
61    /// This is for errors that cannot be recovered from,
62    /// but we'd still like to panic gracefully.
63    #[error("unknown error: {0}")]
64    Unknown(BoxError),
65}
66
67//---------------------------------------------------------------------------------------------------- RuntimeError
68/// Errors that occur _after_ successful [`Env::open`](crate::env::Env::open).
69///
70/// There are no errors for:
71/// 1. Missing tables
72/// 2. (De)serialization
73///
74/// as `cuprate_database` upholds the invariant that:
75///
76/// 1. All tables exist
77/// 2. (De)serialization never fails
78#[derive(thiserror::Error, Debug)]
79pub enum RuntimeError {
80    /// The given key already existed in the database.
81    #[error("key already existed")]
82    KeyExists,
83
84    /// The given key did not exist in the database.
85    #[error("key/value pair was not found")]
86    KeyNotFound,
87
88    /// The database memory map is full and needs a resize.
89    ///
90    /// # Invariant
91    /// This error can only occur if [`Env::MANUAL_RESIZE`](crate::Env::MANUAL_RESIZE) is `true`.
92    #[error("database memory map must be resized")]
93    ResizeNeeded,
94
95    /// The given table did not exist in the database.
96    #[error("database table did not exist")]
97    TableNotFound,
98
99    /// A [`std::io::Error`].
100    #[error("I/O error: {0}")]
101    Io(#[from] std::io::Error),
102}