From f85df7316f9e6e774da7982b55c1b9a11f7422b3 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Fri, 10 Nov 2023 18:39:11 +0000 Subject: [PATCH] improve: use thiserror --- Cargo.toml | 2 +- crates/cashu/Cargo.toml | 1 + crates/cashu/src/error.rs | 188 ++++++++++---------------------------- 3 files changed, 49 insertions(+), 142 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8f7e8331..b471553a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,4 +34,4 @@ tokio = { version = "1.32", default-features = false } tracing = { version = "0.1", default-features = false } tracing-subscriber = "0.3" uniffi = "0.24" - +thiserror = "1.0.50" diff --git a/crates/cashu/Cargo.toml b/crates/cashu/Cargo.toml index 748fe6dc..54530364 100644 --- a/crates/cashu/Cargo.toml +++ b/crates/cashu/Cargo.toml @@ -34,6 +34,7 @@ serde_json = { workspace = true } url = { workspace = true } regex = "1.8.4" itertools = "0.11.0" +thiserror = { workspace = true } [dev-dependencies] # tokio = {version = "1.27.0", features = ["rt", "macros"] } diff --git a/crates/cashu/src/error.rs b/crates/cashu/src/error.rs index bfb4268c..7c29daf1 100644 --- a/crates/cashu/src/error.rs +++ b/crates/cashu/src/error.rs @@ -1,155 +1,77 @@ -use std::error::Error as StdError; -use std::fmt; use std::string::FromUtf8Error; -#[derive(Debug)] +use thiserror::Error; + +#[derive(Debug, Error)] pub enum Error { /// Parse Url Error - UrlParseError(url::ParseError), + #[error("`{0}`")] + UrlParseError(#[from] url::ParseError), /// Utf8 parse error - Utf8ParseError(FromUtf8Error), + #[error("`{0}`")] + Utf8ParseError(#[from] FromUtf8Error), /// Serde Json error + #[error("`{0}`")] SerdeJsonError(serde_json::Error), /// Base64 error - Base64Error(base64::DecodeError), + #[error("`{0}`")] + Base64Error(#[from] base64::DecodeError), + #[error("`{0}`")] CustomError(String), /// From hex error - HexError(hex::FromHexError), - EllipticCurve(k256::elliptic_curve::Error), + #[error("`{0}`")] + HexError(#[from] hex::FromHexError), + #[error("`{0}`")] + EllipticCurve(#[from] k256::elliptic_curve::Error), + #[error("No Key for Amoun")] AmountKey, + #[error("Amount miss match")] Amount, + #[error("Token already spent")] TokenSpent, + #[error("Token not verified")] TokenNotVerifed, + #[error("Invoice Amount undefined")] InvoiceAmountUndefined, } -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Error::UrlParseError(err) => write!(f, "{}", err), - Error::Utf8ParseError(err) => write!(f, "{}", err), - Error::SerdeJsonError(err) => write!(f, "{}", err), - Error::Base64Error(err) => write!(f, "{}", err), - Error::CustomError(err) => write!(f, "{}", err), - Error::HexError(err) => write!(f, "{}", err), - Error::AmountKey => write!(f, "No Key for amount"), - Error::Amount => write!(f, "Amount miss match."), - Error::TokenSpent => write!(f, "Token Spent"), - Error::TokenNotVerifed => write!(f, "Token Not Verified"), - Error::InvoiceAmountUndefined => write!(f, "Invoice without amount"), - Error::EllipticCurve(err) => write!(f, "{}", err), - } - } -} - -impl StdError for Error {} - -impl From for Error { - fn from(err: url::ParseError) -> Error { - Error::UrlParseError(err) - } -} - -impl From for Error { - fn from(err: FromUtf8Error) -> Error { - Error::Utf8ParseError(err) - } -} - -impl From for Error { - fn from(err: serde_json::Error) -> Error { - Error::SerdeJsonError(err) - } -} - -impl From for Error { - fn from(err: base64::DecodeError) -> Error { - Error::Base64Error(err) - } -} - -impl From for Error { - fn from(err: hex::FromHexError) -> Error { - Error::HexError(err) - } -} - -impl From for Error { - fn from(err: k256::elliptic_curve::Error) -> Error { - Error::EllipticCurve(err) - } -} - #[cfg(feature = "wallet")] pub mod wallet { - use std::error::Error as StdError; - use std::fmt; use std::string::FromUtf8Error; - #[derive(Debug)] + use thiserror::Error; + + #[derive(Debug, Error)] pub enum Error { /// Serde Json error - SerdeJsonError(serde_json::Error), + #[error("`{0}`")] + SerdeJsonError(#[from] serde_json::Error), /// From elliptic curve - EllipticError(k256::elliptic_curve::Error), + #[error("`{0}`")] + EllipticError(#[from] k256::elliptic_curve::Error), /// Insufficient Funds + #[error("Insufficient funds")] InsufficientFunds, /// Utf8 parse error - Utf8ParseError(FromUtf8Error), + #[error("`{0}`")] + Utf8ParseError(#[from] FromUtf8Error), /// Base64 error - Base64Error(base64::DecodeError), + #[error("`{0}`")] + Base64Error(#[from] base64::DecodeError), /// Unsupported Token + #[error("Token unsupported")] UnsupportedToken, /// Token Requires proofs + #[error("Proofs Required")] ProofsRequired, /// Url Parse error + #[error("Url Parse")] UrlParse, /// Custom Error message + #[error("`{0}`")] CustomError(String), } - impl StdError for Error {} - - impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Error::CustomError(err) => write!(f, "{}", err), - Error::InsufficientFunds => write!(f, "Insufficient Funds"), - Error::Utf8ParseError(err) => write!(f, "{}", err), - Error::Base64Error(err) => write!(f, "{}", err), - Error::UnsupportedToken => write!(f, "Unsuppported Token"), - Error::EllipticError(err) => write!(f, "{}", err), - Error::SerdeJsonError(err) => write!(f, "{}", err), - Error::UrlParse => write!(f, "Could not parse url"), - Error::ProofsRequired => write!(f, "Token must have at least one proof",), - } - } - } - - impl From for Error { - fn from(err: serde_json::Error) -> Error { - Error::SerdeJsonError(err) - } - } - - impl From for Error { - fn from(err: k256::elliptic_curve::Error) -> Error { - Error::EllipticError(err) - } - } - - impl From for Error { - fn from(err: FromUtf8Error) -> Error { - Error::Utf8ParseError(err) - } - } - - impl From for Error { - fn from(err: base64::DecodeError) -> Error { - Error::Base64Error(err) - } - } - impl From for Error { fn from(_err: crate::url::Error) -> Error { Error::UrlParse @@ -159,43 +81,27 @@ pub mod wallet { #[cfg(feature = "mint")] pub mod mint { - use std::error::Error as StdError; - use std::fmt; + use thiserror::Error; - #[derive(Debug)] + #[derive(Debug, Error)] pub enum Error { + #[error("No key for amount")] AmountKey, + #[error("Amount miss match")] Amount, + #[error("Token Already Spent")] TokenSpent, /// From elliptic curve - EllipticError(k256::elliptic_curve::Error), + #[error("`{0}`")] + EllipticError(#[from] k256::elliptic_curve::Error), + #[error("`Token not verified`")] TokenNotVerifed, + #[error("Invoice amount undefined")] InvoiceAmountUndefined, /// Duplicate Proofs sent in request + #[error("Duplicate proofs")] DuplicateProofs, + #[error("`{0}`")] CustomError(String), } - - impl StdError for Error {} - - impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Error::AmountKey => write!(f, "No Key for amount"), - Error::Amount => write!(f, "Amount miss match"), - Error::TokenSpent => write!(f, "Token Spent"), - Error::EllipticError(err) => write!(f, "{}", err), - Error::CustomError(err) => write!(f, "{}", err), - Error::TokenNotVerifed => write!(f, "Token Not Verified"), - Error::InvoiceAmountUndefined => write!(f, "Invoice without amount"), - Error::DuplicateProofs => write!(f, "Request has duplicate proofs"), - } - } - } - - impl From for Error { - fn from(err: k256::elliptic_curve::Error) -> Error { - Error::EllipticError(err) - } - } }