Skip to content

Commit

Permalink
error: simplify error implementation
Browse files Browse the repository at this point in the history
Use thiserror for handy from and display macros
  • Loading branch information
jb55 committed Nov 29, 2024
1 parent c7c6aa4 commit 6025e1c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
64 changes: 28 additions & 36 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand All @@ -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 {}
2 changes: 1 addition & 1 deletion src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

{
Expand Down

0 comments on commit 6025e1c

Please sign in to comment.