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