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 hash
16    #[error("Invalid hash")]
17    InvalidHash,
18    /// Invalid currency unit
19    #[error("Invalid currency unit: {0}")]
20    InvalidCurrencyUnit(String),
21    /// Missing amount field
22    #[error("Missing amount field")]
23    MissingAmount,
24    /// Parse invoice error
25    #[error(transparent)]
26    Invoice(#[from] lightning_invoice::ParseOrSemanticError),
27    /// Hex decode error
28    #[error(transparent)]
29    Hex(#[from] hex::FromHexError),
30    /// BOLT12 parse error
31    #[error("BOLT12 parse error")]
32    Bolt12Parse,
33    /// NUT00 Error
34    #[error(transparent)]
35    NUT00(#[from] cdk_common::nuts::nut00::Error),
36    /// NUT05 error
37    #[error(transparent)]
38    NUT05(#[from] cdk_common::nuts::nut05::Error),
39    /// Payment error
40    #[error(transparent)]
41    Payment(#[from] cdk_common::payment::Error),
42}
43
44impl From<Error> for Status {
45    fn from(error: Error) -> Self {
46        match error {
47            Error::InvalidId => Status::invalid_argument("Invalid ID"),
48            Error::InvalidPaymentIdentifier => {
49                Status::invalid_argument("Invalid payment identifier")
50            }
51            Error::InvalidHash => Status::invalid_argument("Invalid hash"),
52            Error::InvalidCurrencyUnit(unit) => {
53                Status::invalid_argument(format!("Invalid currency unit: {unit}"))
54            }
55            Error::MissingAmount => Status::invalid_argument("Missing amount field"),
56            Error::Invoice(err) => Status::invalid_argument(format!("Invoice error: {err}")),
57            Error::Hex(err) => Status::invalid_argument(format!("Hex decode error: {err}")),
58            Error::Bolt12Parse => Status::invalid_argument("BOLT12 parse error"),
59            Error::NUT00(err) => Status::internal(format!("NUT00 error: {err}")),
60            Error::NUT05(err) => Status::internal(format!("NUT05 error: {err}")),
61            Error::Payment(err) => Status::internal(format!("Payment error: {err}")),
62        }
63    }
64}
65
66impl From<Error> for cdk_common::payment::Error {
67    fn from(error: Error) -> Self {
68        match error {
69            Error::InvalidId => Self::Custom("Invalid ID".to_string()),
70            Error::InvalidPaymentIdentifier => {
71                Self::Custom("Invalid payment identifier".to_string())
72            }
73            Error::InvalidHash => Self::Custom("Invalid hash".to_string()),
74            Error::InvalidCurrencyUnit(unit) => {
75                Self::Custom(format!("Invalid currency unit: {unit}"))
76            }
77            Error::MissingAmount => Self::Custom("Missing amount field".to_string()),
78            Error::Invoice(err) => Self::Custom(format!("Invoice error: {err}")),
79            Error::Hex(err) => Self::Custom(format!("Hex decode error: {err}")),
80            Error::Bolt12Parse => Self::Custom("BOLT12 parse error".to_string()),
81            Error::NUT00(err) => Self::Custom(format!("NUT00 error: {err}")),
82            Error::NUT05(err) => err.into(),
83            Error::Payment(err) => err,
84        }
85    }
86}