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