Skip to content

Commit

Permalink
refactor: client error
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Apr 12, 2024
1 parent 324d3c0 commit 9e26c48
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
31 changes: 27 additions & 4 deletions crates/cdk/src/client.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
}
Expand Down
11 changes: 1 addition & 10 deletions crates/cdk/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -99,9 +96,3 @@ impl ErrorResponse {
}
}
}

impl From<ErrorResponse> for Error {
fn from(err: ErrorResponse) -> Error {
Self::UnknownErrorResponse(err)
}
}
3 changes: 3 additions & 0 deletions crates/cdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 9e26c48

Please sign in to comment.