Skip to main content

cdk_payment_processor/
error.rs

1//! Error for payment processor
2
3use thiserror::Error;
4use tonic::Status;
5
6/// CDK Payment processor error
7#[derive(Debug, Error)]
8pub enum Error {
9    /// Invalid ID
10    #[error("Invalid id")]
11    InvalidId,
12    /// Invalid payment identifier
13    #[error("Invalid payment identifier")]
14    InvalidPaymentIdentifier,
15    /// Invalid melt options
16    #[error("Invalid melt options")]
17    InvalidMeltOptions,
18    /// Invalid hash
19    #[error("Invalid hash")]
20    InvalidHash,
21    /// Invalid currency unit
22    #[error("Invalid currency unit: {0}")]
23    InvalidCurrencyUnit(String),
24    /// Missing amount field
25    #[error("Missing amount field")]
26    MissingAmount,
27    /// Parse invoice error
28    #[error(transparent)]
29    Invoice(#[from] lightning_invoice::ParseOrSemanticError),
30    /// Hex decode error
31    #[error(transparent)]
32    Hex(#[from] hex::FromHexError),
33    /// BOLT12 parse error
34    #[error("BOLT12 parse error")]
35    Bolt12Parse,
36    /// NUT00 Error
37    #[error(transparent)]
38    NUT00(#[from] cdk_common::nuts::nut00::Error),
39    /// NUT05 error
40    #[error(transparent)]
41    NUT05(#[from] cdk_common::nuts::nut05::Error),
42    /// Payment error
43    #[error(transparent)]
44    Payment(#[from] cdk_common::payment::Error),
45}
46
47impl From<Error> for Status {
48    fn from(error: Error) -> Self {
49        match error {
50            Error::InvalidId => Status::invalid_argument("Invalid ID"),
51            Error::InvalidPaymentIdentifier => {
52                Status::invalid_argument("Invalid payment identifier")
53            }
54            Error::InvalidMeltOptions => Status::invalid_argument("Invalid melt options"),
55            Error::InvalidHash => Status::invalid_argument("Invalid hash"),
56            Error::InvalidCurrencyUnit(unit) => {
57                Status::invalid_argument(format!("Invalid currency unit: {unit}"))
58            }
59            Error::MissingAmount => Status::invalid_argument("Missing amount field"),
60            Error::Invoice(err) => Status::invalid_argument(format!("Invoice error: {err}")),
61            Error::Hex(err) => Status::invalid_argument(format!("Hex decode error: {err}")),
62            Error::Bolt12Parse => Status::invalid_argument("BOLT12 parse error"),
63            Error::NUT00(err) => Status::internal(format!("NUT00 error: {err}")),
64            Error::NUT05(err) => Status::internal(format!("NUT05 error: {err}")),
65            Error::Payment(err) => Status::internal(format!("Payment error: {err}")),
66        }
67    }
68}
69
70impl From<Error> for cdk_common::payment::Error {
71    fn from(error: Error) -> Self {
72        match error {
73            Error::InvalidId => Self::Custom("Invalid ID".to_string()),
74            Error::InvalidPaymentIdentifier => {
75                Self::Custom("Invalid payment identifier".to_string())
76            }
77            Error::InvalidMeltOptions => Self::Custom("Invalid melt options".to_string()),
78            Error::InvalidHash => Self::Custom("Invalid hash".to_string()),
79            Error::InvalidCurrencyUnit(unit) => {
80                Self::Custom(format!("Invalid currency unit: {unit}"))
81            }
82            Error::MissingAmount => Self::Custom("Missing amount field".to_string()),
83            Error::Invoice(err) => Self::Custom(format!("Invoice error: {err}")),
84            Error::Hex(err) => Self::Custom(format!("Hex decode error: {err}")),
85            Error::Bolt12Parse => Self::Custom("BOLT12 parse error".to_string()),
86            Error::NUT00(err) => Self::Custom(format!("NUT00 error: {err}")),
87            Error::NUT05(err) => err.into(),
88            Error::Payment(err) => err,
89        }
90    }
91}