mostro_core/
error.rs

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