diff --git a/src/client.rs b/src/client.rs index e83f8416..2f1cac41 100644 --- a/src/client.rs +++ b/src/client.rs @@ -10,8 +10,9 @@ use crate::{ keyset::{Keys, MintKeySets}, types::{ BlindedMessage, BlindedMessages, CheckFeesRequest, CheckFeesResponse, - CheckSpendableRequest, CheckSpendableResponse, MeltRequest, MeltResponse, MintInfo, - MintRequest, PostMintResponse, Proof, RequestMintResponse, SplitRequest, SplitResponse, + CheckSpendableRequest, CheckSpendableResponse, MeltRequest, MeltResponse, MintError, + MintInfo, MintRequest, PostMintResponse, Proof, RequestMintResponse, SplitRequest, + SplitResponse, }, }; @@ -114,7 +115,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(Error::CustomError(res.to_string())), + Err(_) => Err(MintError::from_json(&res.to_string())?.into()), } } @@ -134,7 +135,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(Error::CustomError(res.to_string())), + Err(_) => Err(MintError::from_json(&res.to_string())?.into()), } } @@ -164,7 +165,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(Error::CustomError(value.to_string())), + Err(_) => Err(MintError::from_json(&value.to_string())?.into()), } } @@ -182,7 +183,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(Error::CustomError(res.to_string())), + Err(_) => Err(MintError::from_json(&res.to_string())?.into()), } } @@ -206,7 +207,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(Error::CustomError(res.to_string())), + Err(_) => Err(MintError::from_json(&res.to_string())?.into()), } } @@ -219,7 +220,7 @@ impl Client { match response { Ok(res) => Ok(res), - Err(_) => Err(Error::CustomError(res.to_string())), + Err(_) => Err(MintError::from_json(&res.to_string())?.into()), } } } diff --git a/src/error.rs b/src/error.rs index cec86412..d79b4204 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,7 @@ use std::string::FromUtf8Error; +use crate::types::MintError; + #[derive(Debug, thiserror::Error)] pub enum Error { /// Min req error @@ -31,4 +33,6 @@ pub enum Error { /// From elliptic curve #[error("From Elliptic: {0}")] EllipticError(#[from] k256::elliptic_curve::Error), + #[error("Mint Error: {0}")] + MintError(#[from] MintError), } diff --git a/src/types.rs b/src/types.rs index 87c42fe3..27b27655 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,5 +1,7 @@ -//! Types for `cashu-rs` +//! Types for `cashu-crab` +use std::error::Error as StdError; +use std::fmt; use std::str::FromStr; use base64::{engine::general_purpose, Engine as _}; @@ -14,6 +16,41 @@ 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 {