From 8201baa56a221ef1b3d9863f19e2dee48368ffe1 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Wed, 14 Jun 2023 10:24:47 -0400 Subject: [PATCH] refactor: Errors --- src/cashu_wallet.rs | 2 +- src/client.rs | 87 ++++++++++++++++++++++++++++++++++++++------- src/error.rs | 30 +++++++++++++--- src/lib.rs | 2 ++ src/types.rs | 37 ------------------- 5 files changed, 103 insertions(+), 55 deletions(-) diff --git a/src/cashu_wallet.rs b/src/cashu_wallet.rs index 564360e6..ed37b4d8 100644 --- a/src/cashu_wallet.rs +++ b/src/cashu_wallet.rs @@ -51,7 +51,7 @@ impl CashuWallet { /// Request Token Mint pub async fn request_mint(&self, amount: Amount) -> Result { - self.client.request_mint(amount).await + Ok(self.client.request_mint(amount).await?) } /// Mint Token diff --git a/src/client.rs b/src/client.rs index 1feb60ff..c7cf2b35 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,4 +1,5 @@ //! Client to connet to mint +use std::fmt; use bitcoin::Amount; use serde_json::Value; @@ -6,15 +7,77 @@ use url::Url; pub use crate::Invoice; use crate::{ - error::Error, keyset::{Keys, MintKeySets}, types::{ BlindedMessage, BlindedMessages, CheckFeesRequest, CheckFeesResponse, - CheckSpendableRequest, CheckSpendableResponse, MeltRequest, MeltResponse, MintError, - MintInfo, MintRequest, PostMintResponse, Proof, RequestMintResponse, SplitRequest, - SplitResponse, + CheckSpendableRequest, CheckSpendableResponse, MeltRequest, MeltResponse, MintInfo, + MintRequest, PostMintResponse, Proof, RequestMintResponse, SplitRequest, SplitResponse, }, }; +use serde::{Deserialize, Serialize}; + +#[derive(Debug)] +pub enum Error { + InvoiceNotPaid, + /// Parse Url Error + UrlParseError(url::ParseError), + /// Serde Json error + SerdeJsonError(serde_json::Error), + /// Min req error + MinReqError(minreq::Error), + /// Custom Error + Custom(String), +} + +impl From for Error { + fn from(err: url::ParseError) -> Error { + Error::UrlParseError(err) + } +} + +impl From for Error { + fn from(err: serde_json::Error) -> Error { + Error::SerdeJsonError(err) + } +} + +impl From for Error { + fn from(err: minreq::Error) -> Error { + Error::MinReqError(err) + } +} + +impl std::error::Error for Error {} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::InvoiceNotPaid => write!(f, "Invoice not paid"), + Error::UrlParseError(err) => write!(f, "{}", err), + Error::SerdeJsonError(err) => write!(f, "{}", err), + Error::MinReqError(err) => write!(f, "{}", err), + Error::Custom(message) => write!(f, "{}", message), + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct MintErrorResponse { + code: u32, + error: String, +} + +impl Error { + pub fn from_json(json: &str) -> Result { + let mint_res: MintErrorResponse = serde_json::from_str(json)?; + + let mint_error = match mint_res.error.as_str() { + "Lightning invoice not paid yet." => Error::InvoiceNotPaid, + _ => Error::Custom(mint_res.error), + }; + Ok(mint_error) + } +} #[derive(Debug, Clone)] pub struct Client { @@ -71,7 +134,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(MintError::from_json(&res.to_string())?.into()), + Err(_) => Err(Error::from_json(&res.to_string())?), } } @@ -88,7 +151,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(MintError::from_json(&res.to_string())?.into()), + Err(_) => Err(Error::from_json(&res.to_string())?), } } @@ -115,7 +178,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(MintError::from_json(&res.to_string())?.into()), + Err(_) => Err(Error::from_json(&res.to_string())?), } } @@ -135,7 +198,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(MintError::from_json(&res.to_string())?.into()), + Err(_) => Err(Error::from_json(&res.to_string())?), } } @@ -165,7 +228,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(MintError::from_json(&value.to_string())?.into()), + Err(_) => Err(Error::from_json(&value.to_string())?), } } @@ -183,7 +246,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(MintError::from_json(&res.to_string())?.into()), + Err(_) => Err(Error::from_json(&res.to_string())?), } } @@ -207,7 +270,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(MintError::from_json(&res.to_string())?.into()), + Err(_) => Err(Error::from_json(&res.to_string())?), } } @@ -220,7 +283,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(MintError::from_json(&res.to_string())?.into()), + Err(_) => Err(Error::from_json(&res.to_string())?), } } } diff --git a/src/error.rs b/src/error.rs index a6076d76..240d2c61 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,7 +1,7 @@ +use std::error::Error as StdError; +use std::fmt; use std::string::FromUtf8Error; -use crate::types::MintError; - #[derive(Debug)] pub enum Error { /// Min req error @@ -23,9 +23,29 @@ pub enum Error { HexError(hex::FromHexError), /// From elliptic curve EllipticError(k256::elliptic_curve::Error), - CrabMintError(MintError), + CrabMintError(crate::client::Error), } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::MinReqError(err) => write!(f, "{}", err), + Error::UrlParseError(err) => write!(f, "{}", err), + Error::UnsupportedToken => write!(f, "Unsuppported Token"), + Error::Utf8ParseError(err) => write!(f, "{}", err), + Error::SerdeJsonError(err) => write!(f, "{}", err), + Error::Base64Error(err) => write!(f, "{}", err), + Error::InsufficantFunds => write!(f, "Insufficant Funds"), + Error::CustomError(err) => write!(f, "{}", err), + Error::HexError(err) => write!(f, "{}", err), + Error::EllipticError(err) => write!(f, "{}", err), + Error::CrabMintError(err) => write!(f, "{}", err), + } + } +} + +impl StdError for Error {} + impl From for Error { fn from(err: minreq::Error) -> Error { Error::MinReqError(err) @@ -68,8 +88,8 @@ impl From for Error { } } -impl From for Error { - fn from(err: MintError) -> Error { +impl From for Error { + fn from(err: crate::client::Error) -> Error { Error::CrabMintError(err) } } diff --git a/src/lib.rs b/src/lib.rs index 9612ba53..e879ca2a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,3 +9,5 @@ pub mod utils; pub use bitcoin::Amount; pub use lightning_invoice::Invoice; + +pub type Result> = std::result::Result; diff --git a/src/types.rs b/src/types.rs index 27b27655..7290cb67 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,7 +1,5 @@ //! Types for `cashu-crab` -use std::error::Error as StdError; -use std::fmt; use std::str::FromStr; use base64::{engine::general_purpose, Engine as _}; @@ -16,41 +14,6 @@ use crate::{ dhke::blind_message, error::Error, serde_utils, serde_utils::serde_url, utils::split_amount, }; -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum MintError { - InvoiceNotPaid, - Custom(String), -} - -impl StdError for MintError {} - -impl fmt::Display for MintError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - MintError::InvoiceNotPaid => write!(f, "Invoice not paid"), - MintError::Custom(message) => write!(f, "{}", message), - } - } -} - -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub struct MintErrorResponse { - code: u32, - error: String, -} - -impl MintError { - pub fn from_json(json: &str) -> Result { - let mint_res: MintErrorResponse = serde_json::from_str(json)?; - - let mint_error = match mint_res.error.as_str() { - "Lightning invoice not paid yet." => MintError::InvoiceNotPaid, - _ => MintError::Custom(mint_res.error), - }; - Ok(mint_error) - } -} - /// Blinded Message [NUT-00] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct BlindedMessage {