From 27f718de08943803e7e85a3b0b5cb4e8012e1ce4 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Fri, 12 Apr 2024 19:29:34 +0100 Subject: [PATCH] refactor: mint and wallet errors as mod files --- crates/cdk/src/error.rs | 184 --------------------------------- crates/cdk/src/error/mint.rs | 38 +++++++ crates/cdk/src/error/mod.rs | 94 +++++++++++++++++ crates/cdk/src/error/wallet.rs | 49 +++++++++ 4 files changed, 181 insertions(+), 184 deletions(-) delete mode 100644 crates/cdk/src/error.rs create mode 100644 crates/cdk/src/error/mint.rs create mode 100644 crates/cdk/src/error/mod.rs create mode 100644 crates/cdk/src/error/wallet.rs diff --git a/crates/cdk/src/error.rs b/crates/cdk/src/error.rs deleted file mode 100644 index fa561251..00000000 --- a/crates/cdk/src/error.rs +++ /dev/null @@ -1,184 +0,0 @@ -use std::string::FromUtf8Error; - -use serde::{Deserialize, Serialize}; -use thiserror::Error; - -use crate::util::hex; - -#[derive(Debug, Error)] -pub enum Error { - /// Parse Url Error - #[error(transparent)] - UrlParseError(#[from] url::ParseError), - /// Utf8 parse error - #[error(transparent)] - Utf8ParseError(#[from] FromUtf8Error), - /// Serde Json error - #[error(transparent)] - SerdeJsonError(#[from] serde_json::Error), - /// Base64 error - #[error(transparent)] - Base64Error(#[from] base64::DecodeError), - /// From hex error - #[error(transparent)] - HexError(#[from] hex::Error), - /// Secp256k1 error - #[error(transparent)] - Secp256k1(#[from] bitcoin::secp256k1::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, - #[error("Proof missing required field")] - MissingProofField, - #[error("No valid point found")] - NoValidPoint, - #[error("Kind not found")] - KindNotFound, - #[error("Unknown Tag")] - UnknownTag, - #[error("Incorrect Secret Kind")] - IncorrectSecretKind, - #[error("Spending conditions not met")] - SpendConditionsNotMet, - #[error("Could not convert key")] - Key, - #[error("Invalid signature")] - InvalidSignature, - #[error("Locktime in past")] - LocktimeInPast, - #[error(transparent)] - Secret(#[from] super::secret::Error), - #[error(transparent)] - NUT01(#[from] crate::nuts::nut01::Error), - #[error(transparent)] - NUT02(#[from] crate::nuts::nut02::Error), - #[cfg(feature = "nut13")] - #[error(transparent)] - Bip32(#[from] bitcoin::bip32::Error), - #[error(transparent)] - ParseInt(#[from] std::num::ParseIntError), - /// Custom error - #[error("`{0}`")] - CustomError(String), -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct ErrorResponse { - pub code: u32, - pub error: Option, - pub detail: Option, -} - -impl ErrorResponse { - pub fn from_json(json: &str) -> Result { - if let Ok(res) = serde_json::from_str::(json) { - Ok(res) - } else { - Ok(Self { - code: 999, - error: Some(json.to_string()), - detail: None, - }) - } - } -} - -pub mod wallet { - use std::string::FromUtf8Error; - - use thiserror::Error; - - use crate::nuts::nut01; - - #[derive(Debug, Error)] - pub enum Error { - /// Serde Json error - #[error(transparent)] - SerdeJsonError(#[from] serde_json::Error), - /// Secp256k1 error - #[error(transparent)] - Secp256k1(#[from] bitcoin::secp256k1::Error), - /// NUT01 error - #[error(transparent)] - NUT01(#[from] nut01::Error), - /// Insufficient Funds - #[error("Insufficient funds")] - InsufficientFunds, - /// Utf8 parse error - #[error(transparent)] - Utf8ParseError(#[from] FromUtf8Error), - /// Base64 error - #[error(transparent)] - Base64Error(#[from] base64::DecodeError), - /// Unsupported Token - #[error("Token unsupported")] - UnsupportedToken, - /// Token Requires proofs - #[error("Proofs Required")] - ProofsRequired, - /// Url Parse error - #[error("Url Parse")] - UrlParse, - #[error(transparent)] - Secret(#[from] crate::secret::Error), - #[error(transparent)] - Cashu(#[from] super::Error), - /// Custom Error message - #[error("`{0}`")] - CustomError(String), - } - - impl From for Error { - fn from(_err: crate::url::Error) -> Error { - Error::UrlParse - } - } -} - -pub mod mint { - use thiserror::Error; - - use crate::nuts::nut01; - - #[derive(Debug, Error)] - pub enum Error { - #[error("No key for amount")] - AmountKey, - #[error("Amount miss match")] - Amount, - #[error("Token Already Spent")] - TokenSpent, - /// Secp256k1 error - #[error(transparent)] - Secp256k1(#[from] bitcoin::secp256k1::Error), - /// NUT01 error - #[error(transparent)] - NUT01(#[from] nut01::Error), - #[error("`Token not verified`")] - TokenNotVerifed, - #[error("Invoice amount undefined")] - InvoiceAmountUndefined, - /// Duplicate Proofs sent in request - #[error("Duplicate proofs")] - DuplicateProofs, - /// Keyset id not active - #[error("Keyset id is not active")] - InactiveKeyset, - /// Keyset is not known - #[error("Unknown Keyset")] - UnknownKeySet, - #[error(transparent)] - Secret(#[from] crate::secret::Error), - #[error(transparent)] - Cashu(#[from] super::Error), - #[error("`{0}`")] - CustomError(String), - } -} diff --git a/crates/cdk/src/error/mint.rs b/crates/cdk/src/error/mint.rs new file mode 100644 index 00000000..34311099 --- /dev/null +++ b/crates/cdk/src/error/mint.rs @@ -0,0 +1,38 @@ +use thiserror::Error; + +use crate::nuts::nut01; + +#[derive(Debug, Error)] +pub enum Error { + #[error("No key for amount")] + AmountKey, + #[error("Amount miss match")] + Amount, + #[error("Token Already Spent")] + TokenSpent, + /// Secp256k1 error + #[error(transparent)] + Secp256k1(#[from] bitcoin::secp256k1::Error), + /// NUT01 error + #[error(transparent)] + NUT01(#[from] nut01::Error), + #[error("`Token not verified`")] + TokenNotVerifed, + #[error("Invoice amount undefined")] + InvoiceAmountUndefined, + /// Duplicate Proofs sent in request + #[error("Duplicate proofs")] + DuplicateProofs, + /// Keyset id not active + #[error("Keyset id is not active")] + InactiveKeyset, + /// Keyset is not known + #[error("Unknown Keyset")] + UnknownKeySet, + #[error(transparent)] + Secret(#[from] crate::secret::Error), + #[error(transparent)] + Cashu(#[from] super::Error), + #[error("`{0}`")] + CustomError(String), +} diff --git a/crates/cdk/src/error/mod.rs b/crates/cdk/src/error/mod.rs new file mode 100644 index 00000000..45aa290d --- /dev/null +++ b/crates/cdk/src/error/mod.rs @@ -0,0 +1,94 @@ +use std::string::FromUtf8Error; + +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +use crate::util::hex; + +pub mod mint; +pub mod wallet; + +#[derive(Debug, Error)] +pub enum Error { + /// Parse Url Error + #[error(transparent)] + UrlParseError(#[from] url::ParseError), + /// Utf8 parse error + #[error(transparent)] + Utf8ParseError(#[from] FromUtf8Error), + /// Serde Json error + #[error(transparent)] + SerdeJsonError(#[from] serde_json::Error), + /// Base64 error + #[error(transparent)] + Base64Error(#[from] base64::DecodeError), + /// From hex error + #[error(transparent)] + HexError(#[from] hex::Error), + /// Secp256k1 error + #[error(transparent)] + Secp256k1(#[from] bitcoin::secp256k1::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, + #[error("Proof missing required field")] + MissingProofField, + #[error("No valid point found")] + NoValidPoint, + #[error("Kind not found")] + KindNotFound, + #[error("Unknown Tag")] + UnknownTag, + #[error("Incorrect Secret Kind")] + IncorrectSecretKind, + #[error("Spending conditions not met")] + SpendConditionsNotMet, + #[error("Could not convert key")] + Key, + #[error("Invalid signature")] + InvalidSignature, + #[error("Locktime in past")] + LocktimeInPast, + #[error(transparent)] + Secret(#[from] super::secret::Error), + #[error(transparent)] + NUT01(#[from] crate::nuts::nut01::Error), + #[error(transparent)] + NUT02(#[from] crate::nuts::nut02::Error), + #[cfg(feature = "nut13")] + #[error(transparent)] + Bip32(#[from] bitcoin::bip32::Error), + #[error(transparent)] + ParseInt(#[from] std::num::ParseIntError), + /// Custom error + #[error("`{0}`")] + CustomError(String), +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct ErrorResponse { + pub code: u32, + pub error: Option, + pub detail: Option, +} + +impl ErrorResponse { + pub fn from_json(json: &str) -> Result { + if let Ok(res) = serde_json::from_str::(json) { + Ok(res) + } else { + Ok(Self { + code: 999, + error: Some(json.to_string()), + detail: None, + }) + } + } +} diff --git a/crates/cdk/src/error/wallet.rs b/crates/cdk/src/error/wallet.rs new file mode 100644 index 00000000..356416f9 --- /dev/null +++ b/crates/cdk/src/error/wallet.rs @@ -0,0 +1,49 @@ +use std::string::FromUtf8Error; + +use thiserror::Error; + +use crate::nuts::nut01; + +#[derive(Debug, Error)] +pub enum Error { + /// Serde Json error + #[error(transparent)] + SerdeJsonError(#[from] serde_json::Error), + /// Secp256k1 error + #[error(transparent)] + Secp256k1(#[from] bitcoin::secp256k1::Error), + /// NUT01 error + #[error(transparent)] + NUT01(#[from] nut01::Error), + /// Insufficient Funds + #[error("Insufficient funds")] + InsufficientFunds, + /// Utf8 parse error + #[error(transparent)] + Utf8ParseError(#[from] FromUtf8Error), + /// Base64 error + #[error(transparent)] + Base64Error(#[from] base64::DecodeError), + /// Unsupported Token + #[error("Token unsupported")] + UnsupportedToken, + /// Token Requires proofs + #[error("Proofs Required")] + ProofsRequired, + /// Url Parse error + #[error("Url Parse")] + UrlParse, + #[error(transparent)] + Secret(#[from] crate::secret::Error), + #[error(transparent)] + Cashu(#[from] super::Error), + /// Custom Error message + #[error("`{0}`")] + CustomError(String), +} + +impl From for Error { + fn from(_err: crate::url::Error) -> Error { + Error::UrlParse + } +}