Skip to main content

morpho_rs_contracts/
error.rs

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