From 9e26c48fc26bfe99138babe846b964d10e15be68 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Fri, 12 Apr 2024 22:22:38 +0100 Subject: [PATCH] refactor: client error --- crates/cdk/src/client.rs | 31 +++++++++++++++++++++++++++---- crates/cdk/src/error/mod.rs | 11 +---------- crates/cdk/src/wallet/mod.rs | 3 +++ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/crates/cdk/src/client.rs b/crates/cdk/src/client.rs index 3871d3ce..14e310c9 100644 --- a/crates/cdk/src/client.rs +++ b/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 for Error { + fn from(err: ErrorResponse) -> Error { + Self::UnknownErrorResponse(err) + } +} + fn join_url(url: Url, paths: &[&str]) -> Result { 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); } diff --git a/crates/cdk/src/error/mod.rs b/crates/cdk/src/error/mod.rs index e9a03d65..5c654d02 100644 --- a/crates/cdk/src/error/mod.rs +++ b/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 for Error { - fn from(err: ErrorResponse) -> Error { - Self::UnknownErrorResponse(err) - } -} diff --git a/crates/cdk/src/wallet/mod.rs b/crates/cdk/src/wallet/mod.rs index 54f05b95..540ac1ad 100644 --- a/crates/cdk/src/wallet/mod.rs +++ b/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),