Browse Source

refactor: client error

thesimplekid 11 months ago
parent
commit
9e26c48fc2
3 changed files with 31 additions and 14 deletions
  1. 27 4
      crates/cdk/src/client.rs
  2. 1 10
      crates/cdk/src/error/mod.rs
  3. 3 0
      crates/cdk/src/wallet/mod.rs

+ 27 - 4
crates/cdk/src/client.rs

@@ -1,10 +1,11 @@
-//! Wallet HTTP wallet client
+//! Wallet client
 
 use reqwest::Client;
 use serde_json::Value;
+use thiserror::Error;
 use url::Url;
 
-use crate::error::{Error, ErrorResponse};
+use crate::error::ErrorResponse;
 use crate::nuts::{
     BlindedMessage, CheckStateRequest, CheckStateResponse, CurrencyUnit, Id, KeySet, KeysResponse,
     KeysetResponse, MeltBolt11Request, MeltBolt11Response, MeltQuoteBolt11Request,
@@ -14,16 +15,38 @@ use crate::nuts::{
 };
 use crate::{Amount, Bolt11Invoice};
 
+#[derive(Debug, Error)]
+pub enum Error {
+    /// Unknown Keyset
+    #[error("Url Path segments could not be joined")]
+    UrlPathSegments,
+    /// Serde Json error
+    #[error(transparent)]
+    SerdeJsonError(#[from] serde_json::Error),
+    /// From hex error
+    #[error(transparent)]
+    ReqwestError(#[from] reqwest::Error),
+    ///  Min req error
+    #[error("Unknown Error response")]
+    UnknownErrorResponse(crate::error::ErrorResponse),
+}
+
+impl From<ErrorResponse> for Error {
+    fn from(err: ErrorResponse) -> Error {
+        Self::UnknownErrorResponse(err)
+    }
+}
+
 fn join_url(url: Url, paths: &[&str]) -> Result<Url, Error> {
     let mut url = url;
     for path in paths {
         if !url.path().ends_with('/') {
             url.path_segments_mut()
-                .map_err(|_| Error::CustomError("Url Path Segmants".to_string()))?
+                .map_err(|_| Error::UrlPathSegments)?
                 .push(path);
         } else {
             url.path_segments_mut()
-                .map_err(|_| Error::CustomError("Url Path Segmants".to_string()))?
+                .map_err(|_| Error::UrlPathSegments)?
                 .pop()
                 .push(path);
         }

+ 1 - 10
crates/cdk/src/error/mod.rs

@@ -61,7 +61,7 @@ pub enum Error {
     #[cfg(feature = "wallet")]
     /// From hex error
     #[error(transparent)]
-    HReeqwestError(#[from] reqwest::Error),
+    ReqwestError(#[from] reqwest::Error),
     /// Nut01 error
     #[error(transparent)]
     NUT01(#[from] crate::nuts::nut01::Error),
@@ -71,9 +71,6 @@ pub enum Error {
     /// NUT11 Error
     #[error(transparent)]
     NUT11(#[from] crate::nuts::nut11::Error),
-    ///  Min req error
-    #[error("Unknown Error response")]
-    UnknownErrorResponse(crate::error::ErrorResponse),
     /// Custom error
     #[error("`{0}`")]
     CustomError(String),
@@ -99,9 +96,3 @@ impl ErrorResponse {
         }
     }
 }
-
-impl From<ErrorResponse> for Error {
-    fn from(err: ErrorResponse) -> Error {
-        Self::UnknownErrorResponse(err)
-    }
-}

+ 3 - 0
crates/cdk/src/wallet/mod.rs

@@ -51,6 +51,9 @@ pub enum Error {
     /// Cashu Url Error
     #[error(transparent)]
     CashuUrl(#[from] crate::url::Error),
+    /// NUT11 Error
+    #[error(transparent)]
+    Client(#[from] crate::client::Error),
     /// NUT00 Error
     #[error(transparent)]
     NUT00(#[from] crate::nuts::nut00::Error),