Skip to main content

ousia_ledger/
error.rs

1// ledger/src/error.rs
2use std::fmt;
3
4#[derive(Debug)]
5pub enum MoneyError {
6    InsufficientFunds,
7    AssetNotFound(String),
8    InvalidAmount,
9    UnconsumedSlice,
10    ReservationNotFound,
11    InvalidAuthority,
12    TransactionNotFound,
13    DuplicateIdempotencyKey(uuid::Uuid),
14    Storage(String),
15}
16
17impl fmt::Display for MoneyError {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        match self {
20            Self::InsufficientFunds => write!(f, "Insufficient funds"),
21            Self::AssetNotFound(code) => write!(f, "Asset not found: {}", code),
22            Self::InvalidAmount => write!(f, "Invalid amount"),
23            Self::UnconsumedSlice => write!(f, "Not all slices were consumed"),
24            Self::ReservationNotFound => write!(f, "Reservation not found"),
25            Self::InvalidAuthority => write!(f, "Invalid authority"),
26            Self::TransactionNotFound => write!(f, "Transaction not found"),
27            Self::DuplicateIdempotencyKey(id) => write!(f, "Duplicate idempotency key: {}", id),
28            Self::Storage(msg) => write!(f, "Storage error: {}", msg),
29        }
30    }
31}
32
33impl std::error::Error for MoneyError {}
34
35impl MoneyError {
36    pub fn is_duplicate_idempotent_key(&self) -> bool {
37        match self {
38            MoneyError::DuplicateIdempotencyKey(_) => true,
39            _ => false,
40        }
41    }
42}