1use alloc::{
2 boxed::Box,
3 format,
4 string::{String, ToString},
5 vec::Vec,
6};
7use core::{
8 fmt::{Debug, Display},
9 str::FromStr,
10};
11
12#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
14pub enum DatabaseError {
15 #[error("failed to open the database: {_0}")]
17 Open(DatabaseErrorInfo),
18 #[error("failed to create a table: {_0}")]
20 CreateTable(DatabaseErrorInfo),
21 #[error(transparent)]
23 Write(Box<DatabaseWriteError>),
24 #[error("failed to read a value from a database table: {_0}")]
26 Read(DatabaseErrorInfo),
27 #[error("database delete error code: {_0}")]
29 Delete(DatabaseErrorInfo),
30 #[error("failed to commit transaction changes: {_0}")]
32 Commit(DatabaseErrorInfo),
33 #[error("failed to initialize a transaction: {_0}")]
35 InitTx(DatabaseErrorInfo),
36 #[error("failed to initialize a cursor: {_0}")]
38 InitCursor(DatabaseErrorInfo),
39 #[error("failed to decode a key from a table")]
41 Decode,
42 #[error("failed to get stats: {_0}")]
44 Stats(DatabaseErrorInfo),
45 #[error("log level {_0:?} is not available")]
47 LogLevelUnavailable(LogLevel),
48 #[error("{_0}")]
50 Other(String),
51}
52
53#[derive(Debug, Clone, PartialEq, Eq, derive_more::Display)]
55#[display("{message} ({code})")]
56pub struct DatabaseErrorInfo {
57 pub message: Box<str>,
59 pub code: i32,
61}
62
63impl<E> From<E> for DatabaseErrorInfo
64where
65 E: Display + Into<i32>,
66{
67 #[inline]
68 fn from(error: E) -> Self {
69 Self { message: error.to_string().into(), code: error.into() }
70 }
71}
72
73impl From<DatabaseWriteError> for DatabaseError {
74 #[inline]
75 fn from(error: DatabaseWriteError) -> Self {
76 Self::Write(Box::new(error))
77 }
78}
79
80#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
82#[error("write operation {:?} failed for key \"{}\" in table {}: {}",
83 self.operation,
84 alloy_primitives::hex::encode(&self.key),
85 self.table_name,
86 self.info)]
87pub struct DatabaseWriteError {
88 pub info: DatabaseErrorInfo,
90 pub operation: DatabaseWriteOperation,
92 pub table_name: &'static str,
94 pub key: Vec<u8>,
96}
97
98#[derive(Clone, Copy, Debug, PartialEq, Eq)]
100pub enum DatabaseWriteOperation {
101 CursorAppend,
103 CursorUpsert,
105 CursorInsert,
107 CursorAppendDup,
109 PutUpsert,
111 PutAppend,
113}
114
115#[derive(Debug, PartialEq, Eq, Clone, Copy)]
117pub enum LogLevel {
118 Fatal,
120 Error,
122 Warn,
124 Notice,
126 Verbose,
128 Debug,
130 Trace,
132 Extra,
134}
135
136impl LogLevel {
137 pub const fn value_variants() -> &'static [Self] {
139 &[
140 Self::Fatal,
141 Self::Error,
142 Self::Warn,
143 Self::Notice,
144 Self::Verbose,
145 Self::Debug,
146 Self::Trace,
147 Self::Extra,
148 ]
149 }
150
151 pub const fn variant_name(&self) -> &'static str {
153 match self {
154 Self::Fatal => "fatal",
155 Self::Error => "error",
156 Self::Warn => "warn",
157 Self::Notice => "notice",
158 Self::Verbose => "verbose",
159 Self::Debug => "debug",
160 Self::Trace => "trace",
161 Self::Extra => "extra",
162 }
163 }
164
165 pub const fn help_message(&self) -> &'static str {
167 match self {
168 Self::Fatal => "Enables logging for critical conditions, i.e. assertion failures",
169 Self::Error => "Enables logging for error conditions",
170 Self::Warn => "Enables logging for warning conditions",
171 Self::Notice => "Enables logging for normal but significant condition",
172 Self::Verbose => "Enables logging for verbose informational",
173 Self::Debug => "Enables logging for debug-level messages",
174 Self::Trace => "Enables logging for trace debug-level messages",
175 Self::Extra => "Enables logging for extra debug-level messages",
176 }
177 }
178}
179
180impl FromStr for LogLevel {
181 type Err = String;
182
183 fn from_str(s: &str) -> Result<Self, Self::Err> {
184 match s.to_lowercase().as_str() {
185 "fatal" => Ok(Self::Fatal),
186 "error" => Ok(Self::Error),
187 "warn" => Ok(Self::Warn),
188 "notice" => Ok(Self::Notice),
189 "verbose" => Ok(Self::Verbose),
190 "debug" => Ok(Self::Debug),
191 "trace" => Ok(Self::Trace),
192 "extra" => Ok(Self::Extra),
193 _ => Err(format!("Invalid log level: {s}")),
194 }
195 }
196}