mostro_core/
error.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4/// Represents specific reasons why a requested action cannot be performed
5#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
6#[serde(rename_all = "snake_case")]
7pub enum CantDoReason {
8    /// The provided signature is invalid or missing
9    InvalidSignature,
10    /// The specified trade index does not exist or is invalid
11    InvalidTradeIndex,
12    /// The provided amount is invalid or out of acceptable range
13    InvalidAmount,
14    /// The provided invoice is malformed or expired
15    InvalidInvoice,
16    /// The payment request is invalid or cannot be processed
17    InvalidPaymentRequest,
18    /// The specified peer is invalid or not found
19    InvalidPeer,
20    /// The rating value is invalid or out of range
21    InvalidRating,
22    /// The text message is invalid or contains prohibited content
23    InvalidTextMessage,
24    /// The order kind is invalid
25    InvalidOrderKind,
26    /// The order status is invalid
27    InvalidOrderStatus,
28    /// Invalid pubkey
29    InvalidPubkey,
30    /// Invalid parameters
31    InvalidParameters,
32    /// The order is already canceled
33    OrderAlreadyCanceled,
34    /// Can't create user
35    CantCreateUser,
36    /// For users trying to do actions on orders that are not theirs
37    IsNotYourOrder,
38    /// For users trying to do actions on orders not allowed by status
39    NotAllowedByStatus,
40    /// Fiat amount is out of range
41    OutOfRangeFiatAmount,
42    /// Sats amount is out of range
43    OutOfRangeSatsAmount,
44    /// For users trying to do actions on dispute that are not theirs
45    IsNotYourDispute,
46    /// For users trying to create a dispute on an order that is not in dispute
47    DisputeCreationError,
48    /// Generic not found
49    NotFound,
50    /// Invalid dispute status
51    InvalidDisputeStatus,
52    /// Invalid action
53    InvalidAction,
54    /// Pending order exists
55    PendingOrderExists,
56}
57
58#[derive(Debug, PartialEq, Eq)]
59pub enum ServiceError {
60    NostrError(String),
61    ParsingInvoiceError,
62    ParsingNumberError,
63    InvoiceExpiredError,
64    InvoiceInvalidError,
65    MinExpirationTimeError,
66    MinAmountError,
67    WrongAmountError,
68    NoAPIResponse,
69    NoCurrency,
70    MalformedAPIRes,
71    NegativeAmount,
72    LnAddressParseError,
73    LnAddressWrongAmount,
74    LnPaymentError(String),
75    LnNodeError(String),
76    InvalidOrderId,
77    DbAccessError(String),
78    InvalidPubkey,
79    HoldInvoiceError(String),
80    UpdateOrderStatusError,
81    InvalidOrderStatus,
82    InvalidOrderKind,
83    DisputeAlreadyExists,
84    DisputeEventError,
85    InvalidRating,
86    InvalidRatingValue,
87    MessageSerializationError,
88    InvalidDisputeId,
89    InvalidDisputeStatus,
90    InvalidPayload,
91    UnexpectedError(String),
92}
93
94#[derive(Debug, PartialEq, Eq)]
95pub enum MostroError {
96    MostroInternalErr(ServiceError),
97    MostroCantDo(CantDoReason),
98}
99
100impl std::error::Error for MostroError {}
101
102impl fmt::Display for MostroError {
103    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104        match self {
105            MostroError::MostroInternalErr(m) => write!(f, "Error caused by {}", m),
106            MostroError::MostroCantDo(m) => write!(f, "Sending cantDo message to user for {:?}", m),
107        }
108    }
109}
110
111impl fmt::Display for ServiceError {
112    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113        match self {
114            ServiceError::ParsingInvoiceError => write!(f, "Incorrect invoice"),
115            ServiceError::ParsingNumberError => write!(f, "Error parsing the number"),
116            ServiceError::InvoiceExpiredError => write!(f, "Invoice has expired"),
117            ServiceError::MinExpirationTimeError => write!(f, "Minimal expiration time on invoice"),
118            ServiceError::InvoiceInvalidError => write!(f, "Invoice is invalid"),
119            ServiceError::MinAmountError => write!(f, "Minimal payment amount"),
120            ServiceError::WrongAmountError => write!(f, "The amount on this invoice is wrong"),
121            ServiceError::NoAPIResponse => write!(f, "Price API not answered - retry"),
122            ServiceError::NoCurrency => write!(f, "Currency requested is not present in the exchange list, please specify a fixed rate"),
123            ServiceError::MalformedAPIRes => write!(f, "Malformed answer from exchange quoting request"),
124            ServiceError::NegativeAmount => write!(f, "Negative amount is not valid"),
125            ServiceError::LnAddressWrongAmount => write!(f, "Ln address need amount of 0 sats - please check your order"),
126            ServiceError::LnAddressParseError  => write!(f, "Ln address parsing error - please check your address"),
127            ServiceError::LnPaymentError(e) => write!(f, "Lightning payment failure cause: {}",e),
128            ServiceError::LnNodeError(e) => write!(f, "Lightning node connection failure caused by: {}",e),
129            ServiceError::InvalidOrderId => write!(f, "Order id not present in database"),
130            ServiceError::InvalidPubkey => write!(f, "Invalid pubkey"),
131            ServiceError::DbAccessError(e) => write!(f, "Error in database access: {}",e),
132            ServiceError::HoldInvoiceError(e) => write!(f, "Error holding invoice: {}",e),
133            ServiceError::UpdateOrderStatusError => write!(f, "Error updating order status"),
134            ServiceError::InvalidOrderStatus => write!(f, "Invalid order status"),
135            ServiceError::InvalidOrderKind => write!(f, "Invalid order kind"),
136            ServiceError::DisputeAlreadyExists => write!(f, "Dispute already exists"),
137            ServiceError::DisputeEventError => write!(f, "Error publishing dispute event"),
138            ServiceError::NostrError(e) => write!(f, "Error in nostr: {}",e),
139            ServiceError::InvalidRating => write!(f, "Invalid rating message"),
140            ServiceError::InvalidRatingValue => write!(f, "Invalid rating value"),
141            ServiceError::MessageSerializationError => write!(f, "Error serializing message"),
142            ServiceError::InvalidDisputeId => write!(f, "Invalid dispute id"),
143            ServiceError::InvalidDisputeStatus => write!(f, "Invalid dispute status"),
144            ServiceError::InvalidPayload => write!(f, "Invalid payload"),
145            ServiceError::UnexpectedError(e) => write!(f, "Unexpected error: {}", e),
146        }
147    }
148}
149
150// impl From<lightning_invoice::Bolt11ParseError> for MostroError {
151//     fn from(_: lightning_invoice::Bolt11ParseError) -> Self {
152//         MostroError::ParsingInvoiceError
153//     }
154// }
155
156// impl From<lightning_invoice::ParseOrSemanticError> for MostroError {
157//     fn from(_: lightning_invoice::ParseOrSemanticError) -> Self {
158//         MostroError::ParsingInvoiceError
159//     }
160// }
161
162// impl From<std::num::ParseIntError> for MostroError {
163//     fn from(_: std::num::ParseIntError) -> Self {
164//         MostroError::ParsingNumberError
165//     }
166// }
167
168// impl From<reqwest::Error> for MostroError {
169//     fn from(_: reqwest::Error) -> Self {
170//         MostroError::NoAPIResponse
171//     }
172// }