lnurl_pay/
error.rs

1// Copyright (c) 2024 Yuki Kishimoto
2// Distributed under the MIT software license
3
4use alloc::string::String;
5use core::fmt;
6
7#[derive(Debug)]
8pub enum Error {
9    Fmt(fmt::Error),
10    Bech32Decode(bech32::DecodeError),
11    Bech32Encode(bech32::EncodeError),
12    #[cfg(feature = "api")]
13    Reqwest(reqwest::Error),
14    InvalidLnUrl,
15    InvalidLightningAddress,
16    UnknownTag,
17    AmountTooLow {
18        msats: u64,
19        min: u64,
20    },
21    AmountTooHigh {
22        msats: u64,
23        max: u64,
24    },
25    CantGetInvoice(Option<String>),
26}
27
28#[cfg(feature = "std")]
29impl std::error::Error for Error {}
30
31impl fmt::Display for Error {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            Self::Fmt(e) => write!(f, "{e}"),
35            Self::Bech32Decode(e) => write!(f, "{e}"),
36            Self::Bech32Encode(e) => write!(f, "{e}"),
37            #[cfg(feature = "api")]
38            Self::Reqwest(e) => write!(f, "Reqwest: {e}"),
39            Self::InvalidLnUrl => write!(f, "Invalid LNURL"),
40            Self::InvalidLightningAddress => write!(f, "Invalid Lightning Address"),
41            Self::UnknownTag => write!(f, "Unknown tag"),
42            Self::AmountTooLow { msats, min } => {
43                write!(f, "Amount too low: {msats} msats (min. {min} msats)")
44            }
45            Self::AmountTooHigh { msats, max } => {
46                write!(f, "Amount too high: {msats} msats (max. {max} msats)")
47            }
48            Self::CantGetInvoice(e) => write!(
49                f,
50                "Can't get invoice: {}",
51                e.as_deref().unwrap_or("unknown")
52            ),
53        }
54    }
55}
56
57impl From<fmt::Error> for Error {
58    fn from(e: fmt::Error) -> Self {
59        Self::Fmt(e)
60    }
61}
62
63impl From<bech32::DecodeError> for Error {
64    fn from(e: bech32::DecodeError) -> Self {
65        Self::Bech32Decode(e)
66    }
67}
68
69impl From<bech32::EncodeError> for Error {
70    fn from(e: bech32::EncodeError) -> Self {
71        Self::Bech32Encode(e)
72    }
73}
74
75#[cfg(feature = "api")]
76impl From<reqwest::Error> for Error {
77    fn from(e: reqwest::Error) -> Self {
78        Self::Reqwest(e)
79    }
80}