thesimplekid hace 1 año
padre
commit
ca8d01be2a
Se han modificado 3 ficheros con 51 adiciones y 9 borrados
  1. 9 8
      src/client.rs
  2. 4 0
      src/error.rs
  3. 38 1
      src/types.rs

+ 9 - 8
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()),
         }
     }
 }

+ 4 - 0
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),
 }

+ 38 - 1
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<Self, Error> {
+        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 {