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}
55
56#[derive(Debug, PartialEq, Eq)]
57pub enum ServiceError {
58    NostrError(String),
59    ParsingInvoiceError,
60    ParsingNumberError,
61    InvoiceExpiredError,
62    InvoiceInvalidError,
63    MinExpirationTimeError,
64    MinAmountError,
65    WrongAmountError,
66    NoAPIResponse,
67    NoCurrency,
68    MalformedAPIRes,
69    NegativeAmount,
70    LnAddressParseError,
71    LnAddressWrongAmount,
72    LnPaymentError(String),
73    LnNodeError(String),
74    InvalidOrderId,
75    DbAccessError(String),
76    InvalidPubkey,
77    HoldInvoiceError(String),
78    UpdateOrderStatusError,
79    InvalidOrderStatus,
80    InvalidOrderKind,
81    DisputeAlreadyExists,
82    DisputeEventError,
83    InvalidRating,
84    InvalidRatingValue,
85    MessageSerializationError,
86    InvalidDisputeId,
87    InvalidDisputeStatus,
88    InvalidPayload,
89    UnexpectedError(String),
90}
91
92#[derive(Debug, PartialEq, Eq)]
93pub enum MostroError {
94    MostroInternalErr(ServiceError),
95    MostroCantDo(CantDoReason),
96}
97
98impl std::error::Error for MostroError {}
99
100impl fmt::Display for MostroError {
101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102        match self {
103            MostroError::MostroInternalErr(m) => write!(f, "Error caused by {}", m),
104            MostroError::MostroCantDo(m) => write!(f, "Sending cantDo message to user for {:?}", m),
105        }
106    }
107}
108
109impl fmt::Display for ServiceError {
110    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111        match self {
112            ServiceError::ParsingInvoiceError => write!(f, "Incorrect invoice"),
113            ServiceError::ParsingNumberError => write!(f, "Error parsing the number"),
114            ServiceError::InvoiceExpiredError => write!(f, "Invoice has expired"),
115            ServiceError::MinExpirationTimeError => write!(f, "Minimal expiration time on invoice"),
116            ServiceError::InvoiceInvalidError => write!(f, "Invoice is invalid"),
117            ServiceError::MinAmountError => write!(f, "Minimal payment amount"),
118            ServiceError::WrongAmountError => write!(f, "The amount on this invoice is wrong"),
119            ServiceError::NoAPIResponse => write!(f, "Price API not answered - retry"),
120            ServiceError::NoCurrency => write!(f, "Currency requested is not present in the exchange list, please specify a fixed rate"),
121            ServiceError::MalformedAPIRes => write!(f, "Malformed answer from exchange quoting request"),
122            ServiceError::NegativeAmount => write!(f, "Negative amount is not valid"),
123            ServiceError::LnAddressWrongAmount => write!(f, "Ln address need amount of 0 sats - please check your order"),
124            ServiceError::LnAddressParseError  => write!(f, "Ln address parsing error - please check your address"),
125            ServiceError::LnPaymentError(e) => write!(f, "Lightning payment failure cause: {}",e),
126            ServiceError::LnNodeError(e) => write!(f, "Lightning node connection failure caused by: {}",e),
127            ServiceError::InvalidOrderId => write!(f, "Order id not present in database"),
128            ServiceError::InvalidPubkey => write!(f, "Invalid pubkey"),
129            ServiceError::DbAccessError(e) => write!(f, "Error in database access: {}",e),
130            ServiceError::HoldInvoiceError(e) => write!(f, "Error holding invoice: {}",e),
131            ServiceError::UpdateOrderStatusError => write!(f, "Error updating order status"),
132            ServiceError::InvalidOrderStatus => write!(f, "Invalid order status"),
133            ServiceError::InvalidOrderKind => write!(f, "Invalid order kind"),
134            ServiceError::DisputeAlreadyExists => write!(f, "Dispute already exists"),
135            ServiceError::DisputeEventError => write!(f, "Error publishing dispute event"),
136            ServiceError::NostrError(e) => write!(f, "Error in nostr: {}",e),
137            ServiceError::InvalidRating => write!(f, "Invalid rating message"),
138            ServiceError::InvalidRatingValue => write!(f, "Invalid rating value"),
139            ServiceError::MessageSerializationError => write!(f, "Error serializing message"),
140            ServiceError::InvalidDisputeId => write!(f, "Invalid dispute id"),
141            ServiceError::InvalidDisputeStatus => write!(f, "Invalid dispute status"),
142            ServiceError::InvalidPayload => write!(f, "Invalid payload"),
143            ServiceError::UnexpectedError(e) => write!(f, "Unexpected error: {}", e),
144        }
145    }
146}
147
148// impl From<lightning_invoice::Bolt11ParseError> for MostroError {
149//     fn from(_: lightning_invoice::Bolt11ParseError) -> Self {
150//         MostroError::ParsingInvoiceError
151//     }
152// }
153
154// impl From<lightning_invoice::ParseOrSemanticError> for MostroError {
155//     fn from(_: lightning_invoice::ParseOrSemanticError) -> Self {
156//         MostroError::ParsingInvoiceError
157//     }
158// }
159
160// impl From<std::num::ParseIntError> for MostroError {
161//     fn from(_: std::num::ParseIntError) -> Self {
162//         MostroError::ParsingNumberError
163//     }
164// }
165
166// impl From<reqwest::Error> for MostroError {
167//     fn from(_: reqwest::Error) -> Self {
168//         MostroError::NoAPIResponse
169//     }
170// }