morpho_rs_contracts/
error.rs

1//! Error types for the contracts crate.
2
3use alloy_primitives::U256;
4use thiserror::Error;
5
6/// Errors that can occur when using contract clients.
7#[derive(Debug, Error)]
8pub enum ContractError {
9    /// RPC connection failed.
10    #[error("RPC connection failed: {0}")]
11    RpcConnection(String),
12
13    /// Transaction failed.
14    #[error("Transaction failed: {0}")]
15    TransactionFailed(String),
16
17    /// Insufficient balance.
18    #[error("Insufficient balance: have {have}, need {need}")]
19    InsufficientBalance { have: U256, need: U256 },
20
21    /// Invalid private key.
22    #[error("Invalid private key")]
23    InvalidPrivateKey,
24}
25
26/// Result type alias for contract operations.
27pub type Result<T> = std::result::Result<T, ContractError>;
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test_error_display_rpc_connection() {
35        let error = ContractError::RpcConnection("connection refused".to_string());
36        assert_eq!(
37            error.to_string(),
38            "RPC connection failed: connection refused"
39        );
40    }
41
42    #[test]
43    fn test_error_display_transaction_failed() {
44        let error = ContractError::TransactionFailed("out of gas".to_string());
45        assert_eq!(error.to_string(), "Transaction failed: out of gas");
46    }
47
48    #[test]
49    fn test_error_display_insufficient_balance() {
50        let error = ContractError::InsufficientBalance {
51            have: U256::from(100),
52            need: U256::from(200),
53        };
54        assert_eq!(
55            error.to_string(),
56            "Insufficient balance: have 100, need 200"
57        );
58    }
59
60    #[test]
61    fn test_error_display_invalid_private_key() {
62        let error = ContractError::InvalidPrivateKey;
63        assert_eq!(error.to_string(), "Invalid private key");
64    }
65}