templar_common/vault/
errors.rs1use super::*;
2
3#[near(serializers = [json])]
5pub enum Error {
6 IndexDrifted(ExpectedIdx, ActualIdx),
8 MarketDrifted {
10 expected: MarketId,
11 actual: MarketId,
12 },
13 MissingMarket(MarketId),
15 NotWithdrawing,
17 NotAllocating,
19 NotRefreshing,
21 NotPayout,
23 MarketTransferFailed,
25 MissingSupplyPosition,
27 PositionReadFailed,
29 BalanceReadFailed,
31 InsufficientLiquidity,
33 ZeroAmount,
35}
36
37impl std::fmt::Debug for Error {
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 std::fmt::Display::fmt(self, f)
40 }
41}
42
43impl std::fmt::Display for Error {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 match self {
46 Error::IndexDrifted(expected, actual) => {
47 write!(f, "vault index drifted: expected {expected}, got {actual}")
48 }
49 Error::MarketDrifted { expected, actual } => {
50 write!(f, "vault market drifted: expected {expected}, got {actual}")
51 }
52 Error::MissingMarket(market) => write!(f, "missing market: {market}"),
53 Error::NotWithdrawing => f.write_str("vault is not withdrawing"),
54 Error::NotAllocating => f.write_str("vault is not allocating"),
55 Error::NotRefreshing => f.write_str("vault is not refreshing"),
56 Error::NotPayout => f.write_str("vault is not in payout"),
57 Error::MarketTransferFailed => f.write_str("market transfer failed"),
58 Error::MissingSupplyPosition => f.write_str("missing supply position"),
59 Error::PositionReadFailed => f.write_str("position read failed"),
60 Error::BalanceReadFailed => f.write_str("balance read failed"),
61 Error::InsufficientLiquidity => f.write_str("insufficient liquidity"),
62 Error::ZeroAmount => f.write_str("zero amount"),
63 }
64 }
65}
66
67#[cfg(test)]
68mod tests {
69 use super::Error;
70 use crate::vault::MarketId;
71
72 #[test]
73 fn display_uses_human_readable_message() {
74 let error = Error::MarketDrifted {
75 expected: MarketId(1),
76 actual: MarketId(2),
77 };
78
79 assert_eq!(error.to_string(), "vault market drifted: expected 1, got 2");
80 }
81}