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 #[error("database file exists but is not valid")]
28 Invalid,
29
30 /// The given `Path/File` existed, was a valid
31 /// database, but the version is incorrect.
32 #[error("database file is valid, but version is incorrect")]
33 InvalidVersion,
34
35 /// I/O error.
36 #[error("database I/O error: {0}")]
37 Io(#[from] std::io::Error),
38
39 /// The given `Path/File` existed,
40 /// was a valid database, but it is corrupt.
41 #[error("database file is corrupt")]
42 Corrupt,
43
44 /// The database is currently in the process
45 /// of shutting down and cannot respond.
46 ///
47 /// # Notes
48 /// This error can only occur with the `heed` backend when
49 /// the database environment is opened _right_ at the same time
50 /// another thread/process is closing it.
51 ///
52 /// This will never occur with other backends.
53 #[error("database is shutting down")]
54 ShuttingDown,
55
56 /// An unknown error occurred.
57 ///
58 /// This is for errors that cannot be recovered from,
59 /// but we'd still like to panic gracefully.
60 #[error("unknown error: {0}")]
61 Unknown(BoxError),
62}
63
64//---------------------------------------------------------------------------------------------------- RuntimeError
65/// Errors that occur _after_ successful [`Env::open`](crate::env::Env::open).
66///
67/// There are no errors for:
68/// 1. Missing tables
69/// 2. (De)serialization
70///
71/// as `cuprate_database` upholds the invariant that:
72///
73/// 1. All tables exist
74/// 2. (De)serialization never fails
75#[derive(thiserror::Error, Debug)]
76pub enum RuntimeError {
77 /// The given key already existed in the database.
78 #[error("key already existed")]
79 KeyExists,
80
81 /// The given key did not exist in the database.
82 #[error("key/value pair was not found")]
83 KeyNotFound,
84
85 /// The database memory map is full and needs a resize.
86 ///
87 /// # Invariant
88 /// This error can only occur if [`Env::MANUAL_RESIZE`](crate::Env::MANUAL_RESIZE) is `true`.
89 #[error("database memory map must be resized")]
90 ResizeNeeded,
91
92 /// The given table did not exist in the database.
93 #[error("database table did not exist")]
94 TableNotFound,
95
96 /// A [`std::io::Error`].
97 #[error("I/O error: {0}")]
98 Io(#[from] std::io::Error),
99}