commit 6025e1ca69920e46946c6abfbd0929afb091c443
parent c7c6aa41c4c6108c4a8838258a6f8205bce23cc5
Author: William Casarin <jb55@jb55.com>
Date: Thu, 28 Nov 2024 16:21:20 -0800
error: simplify error implementation
Use thiserror for handy from and display macros
Diffstat:
5 files changed, 32 insertions(+), 39 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -20,6 +20,7 @@ bindgen = []
[dependencies]
flatbuffers = "23.5.26"
libc = "0.2.151"
+thiserror = "2.0.3"
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
diff --git a/src/error.rs b/src/error.rs
@@ -1,27 +1,46 @@
-use std::fmt;
+use thiserror::Error;
-#[derive(Debug, Eq, PartialEq)]
+/// Main error type
+#[derive(Debug, Error)]
pub enum Error {
+ #[error("Database open failed")]
DbOpenFailed,
+
+ #[error("Not found")]
NotFound,
+
+ #[error("Decode error")]
DecodeError,
+
+ #[error("Query failed")]
QueryError,
+
+ #[error("Note process failed")]
NoteProcessFailed,
+
+ #[error("Transaction failed")]
TransactionFailed,
+
+ #[error("Subscription failed")]
SubscriptionError,
+
+ #[error("Buffer overflow")]
BufferOverflow,
- Filter(FilterError),
-}
-impl Error {
- pub fn filter(ferr: FilterError) -> Self {
- Error::Filter(ferr)
- }
+ #[error("Filter error: {0}")]
+ Filter(#[from] FilterError),
+
+ #[error("IO error: {0}")]
+ IO(#[from] std::io::Error),
}
-#[derive(Debug, Eq, PartialEq)]
+/// Filter-specific error type
+#[derive(Debug, Error, Eq, PartialEq)]
pub enum FilterError {
+ #[error("Field already exists")]
FieldAlreadyExists,
+
+ #[error("Field already started")]
FieldAlreadyStarted,
}
@@ -34,30 +53,3 @@ impl FilterError {
Error::Filter(FilterError::FieldAlreadyStarted)
}
}
-
-impl fmt::Display for FilterError {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self {
- FilterError::FieldAlreadyExists => write!(f, "field already exists"),
- FilterError::FieldAlreadyStarted => write!(f, "field already started"),
- }
- }
-}
-
-impl fmt::Display for Error {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self {
- Error::DbOpenFailed => write!(f, "Open failed"),
- Error::NotFound => write!(f, "Not found"),
- Error::QueryError => write!(f, "Query failed"),
- Error::DecodeError => write!(f, "Decode error"),
- Error::NoteProcessFailed => write!(f, "Note process failed"),
- Error::TransactionFailed => write!(f, "Transaction failed"),
- Error::SubscriptionError => write!(f, "Subscription failed"),
- Error::BufferOverflow => write!(f, "Buffer overflow"),
- Error::Filter(filter_err) => write!(f, "Filter: {filter_err}"),
- }
- }
-}
-
-impl std::error::Error for Error {}
diff --git a/src/filter.rs b/src/filter.rs
@@ -401,7 +401,7 @@ impl FilterBuilder {
let r =
unsafe { bindings::ndb_filter_start_tag_field(self.as_mut_ptr(), tag as u8 as c_char) };
if r == 0 {
- return Err(Error::filter(FilterError::FieldAlreadyStarted));
+ return Err(FilterError::FieldAlreadyStarted.into());
}
Ok(())
}
diff --git a/src/note.rs b/src/note.rs
@@ -509,7 +509,7 @@ mod tests {
let err = ndb
.get_note_by_id(&mut txn, &[0; 32])
.expect_err("not found");
- assert!(err == Error::NotFound);
+ assert!(matches!(err, Error::NotFound));
}
test_util::cleanup_db(db);
diff --git a/src/transaction.rs b/src/transaction.rs
@@ -69,7 +69,7 @@ mod tests {
{
let _txn = Transaction::new(&ndb).expect("txn1 failed");
let txn2 = Transaction::new(&ndb).expect_err("tx2");
- assert!(txn2 == Error::TransactionFailed);
+ assert!(matches!(txn2, Error::TransactionFailed));
}
{