mirror of
https://github.com/aljazceru/cdk.git
synced 2026-01-05 05:55:30 +01:00
refactor: Errors
This commit is contained in:
@@ -51,7 +51,7 @@ impl CashuWallet {
|
||||
|
||||
/// Request Token Mint
|
||||
pub async fn request_mint(&self, amount: Amount) -> Result<RequestMintResponse, Error> {
|
||||
self.client.request_mint(amount).await
|
||||
Ok(self.client.request_mint(amount).await?)
|
||||
}
|
||||
|
||||
/// Mint Token
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
//! Client to connet to mint
|
||||
use std::fmt;
|
||||
|
||||
use bitcoin::Amount;
|
||||
use serde_json::Value;
|
||||
@@ -6,15 +7,77 @@ use url::Url;
|
||||
|
||||
pub use crate::Invoice;
|
||||
use crate::{
|
||||
error::Error,
|
||||
keyset::{Keys, MintKeySets},
|
||||
types::{
|
||||
BlindedMessage, BlindedMessages, CheckFeesRequest, CheckFeesResponse,
|
||||
CheckSpendableRequest, CheckSpendableResponse, MeltRequest, MeltResponse, MintError,
|
||||
MintInfo, MintRequest, PostMintResponse, Proof, RequestMintResponse, SplitRequest,
|
||||
SplitResponse,
|
||||
CheckSpendableRequest, CheckSpendableResponse, MeltRequest, MeltResponse, MintInfo,
|
||||
MintRequest, PostMintResponse, Proof, RequestMintResponse, SplitRequest, SplitResponse,
|
||||
},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
InvoiceNotPaid,
|
||||
/// Parse Url Error
|
||||
UrlParseError(url::ParseError),
|
||||
/// Serde Json error
|
||||
SerdeJsonError(serde_json::Error),
|
||||
/// Min req error
|
||||
MinReqError(minreq::Error),
|
||||
/// Custom Error
|
||||
Custom(String),
|
||||
}
|
||||
|
||||
impl From<url::ParseError> for Error {
|
||||
fn from(err: url::ParseError) -> Error {
|
||||
Error::UrlParseError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for Error {
|
||||
fn from(err: serde_json::Error) -> Error {
|
||||
Error::SerdeJsonError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<minreq::Error> for Error {
|
||||
fn from(err: minreq::Error) -> Error {
|
||||
Error::MinReqError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Error::InvoiceNotPaid => write!(f, "Invoice not paid"),
|
||||
Error::UrlParseError(err) => write!(f, "{}", err),
|
||||
Error::SerdeJsonError(err) => write!(f, "{}", err),
|
||||
Error::MinReqError(err) => write!(f, "{}", err),
|
||||
Error::Custom(message) => write!(f, "{}", message),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct MintErrorResponse {
|
||||
code: u32,
|
||||
error: String,
|
||||
}
|
||||
|
||||
impl Error {
|
||||
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." => Error::InvoiceNotPaid,
|
||||
_ => Error::Custom(mint_res.error),
|
||||
};
|
||||
Ok(mint_error)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Client {
|
||||
@@ -71,7 +134,7 @@ impl Client {
|
||||
|
||||
match response {
|
||||
Ok(res) => Ok(res),
|
||||
Err(_) => Err(MintError::from_json(&res.to_string())?.into()),
|
||||
Err(_) => Err(Error::from_json(&res.to_string())?),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +151,7 @@ impl Client {
|
||||
|
||||
match response {
|
||||
Ok(res) => Ok(res),
|
||||
Err(_) => Err(MintError::from_json(&res.to_string())?.into()),
|
||||
Err(_) => Err(Error::from_json(&res.to_string())?),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +178,7 @@ impl Client {
|
||||
|
||||
match response {
|
||||
Ok(res) => Ok(res),
|
||||
Err(_) => Err(MintError::from_json(&res.to_string())?.into()),
|
||||
Err(_) => Err(Error::from_json(&res.to_string())?),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +198,7 @@ impl Client {
|
||||
|
||||
match response {
|
||||
Ok(res) => Ok(res),
|
||||
Err(_) => Err(MintError::from_json(&res.to_string())?.into()),
|
||||
Err(_) => Err(Error::from_json(&res.to_string())?),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,7 +228,7 @@ impl Client {
|
||||
|
||||
match response {
|
||||
Ok(res) => Ok(res),
|
||||
Err(_) => Err(MintError::from_json(&value.to_string())?.into()),
|
||||
Err(_) => Err(Error::from_json(&value.to_string())?),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,7 +246,7 @@ impl Client {
|
||||
|
||||
match response {
|
||||
Ok(res) => Ok(res),
|
||||
Err(_) => Err(MintError::from_json(&res.to_string())?.into()),
|
||||
Err(_) => Err(Error::from_json(&res.to_string())?),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,7 +270,7 @@ impl Client {
|
||||
|
||||
match response {
|
||||
Ok(res) => Ok(res),
|
||||
Err(_) => Err(MintError::from_json(&res.to_string())?.into()),
|
||||
Err(_) => Err(Error::from_json(&res.to_string())?),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,7 +283,7 @@ impl Client {
|
||||
|
||||
match response {
|
||||
Ok(res) => Ok(res),
|
||||
Err(_) => Err(MintError::from_json(&res.to_string())?.into()),
|
||||
Err(_) => Err(Error::from_json(&res.to_string())?),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
30
src/error.rs
30
src/error.rs
@@ -1,7 +1,7 @@
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
use crate::types::MintError;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
/// Min req error
|
||||
@@ -23,9 +23,29 @@ pub enum Error {
|
||||
HexError(hex::FromHexError),
|
||||
/// From elliptic curve
|
||||
EllipticError(k256::elliptic_curve::Error),
|
||||
CrabMintError(MintError),
|
||||
CrabMintError(crate::client::Error),
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Error::MinReqError(err) => write!(f, "{}", err),
|
||||
Error::UrlParseError(err) => write!(f, "{}", err),
|
||||
Error::UnsupportedToken => write!(f, "Unsuppported Token"),
|
||||
Error::Utf8ParseError(err) => write!(f, "{}", err),
|
||||
Error::SerdeJsonError(err) => write!(f, "{}", err),
|
||||
Error::Base64Error(err) => write!(f, "{}", err),
|
||||
Error::InsufficantFunds => write!(f, "Insufficant Funds"),
|
||||
Error::CustomError(err) => write!(f, "{}", err),
|
||||
Error::HexError(err) => write!(f, "{}", err),
|
||||
Error::EllipticError(err) => write!(f, "{}", err),
|
||||
Error::CrabMintError(err) => write!(f, "{}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for Error {}
|
||||
|
||||
impl From<minreq::Error> for Error {
|
||||
fn from(err: minreq::Error) -> Error {
|
||||
Error::MinReqError(err)
|
||||
@@ -68,8 +88,8 @@ impl From<k256::elliptic_curve::Error> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MintError> for Error {
|
||||
fn from(err: MintError) -> Error {
|
||||
impl From<crate::client::Error> for Error {
|
||||
fn from(err: crate::client::Error) -> Error {
|
||||
Error::CrabMintError(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,3 +9,5 @@ pub mod utils;
|
||||
|
||||
pub use bitcoin::Amount;
|
||||
pub use lightning_invoice::Invoice;
|
||||
|
||||
pub type Result<T, E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
|
||||
|
||||
37
src/types.rs
37
src/types.rs
@@ -1,7 +1,5 @@
|
||||
//! Types for `cashu-crab`
|
||||
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
|
||||
use base64::{engine::general_purpose, Engine as _};
|
||||
@@ -16,41 +14,6 @@ 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 {
|
||||
|
||||
Reference in New Issue
Block a user