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