Skip to main content

apex_sdk/
error.rs

1//! Error types for the Apex SDK.
2
3use thiserror::Error;
4
5/// Result type alias for Apex SDK operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Main error type for the Apex SDK.
9#[derive(Error, Debug)]
10pub enum Error {
11    /// Configuration error
12    #[error("Configuration error: {0}")]
13    Config(String),
14
15    /// Connection error
16    #[error("Connection error: {0}")]
17    Connection(String),
18
19    /// Transaction error
20    #[error("Transaction error: {0}")]
21    Transaction(String),
22
23    /// Serialization error
24    #[error("Serialization error: {0}")]
25    Serialization(String),
26
27    /// Invalid address error
28    #[error("Invalid address: {0}")]
29    InvalidAddress(String),
30
31    /// Unsupported chain error
32    #[error("Unsupported chain: {0}")]
33    UnsupportedChain(String),
34
35    /// Generic error
36    #[error("Error: {0}")]
37    Other(String),
38}
39
40impl From<anyhow::Error> for Error {
41    fn from(err: anyhow::Error) -> Self {
42        Error::Other(err.to_string())
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_error_is_send_sync() {
52        fn assert_send_sync<T: Send + Sync>() {}
53        assert_send_sync::<Error>();
54    }
55
56    #[test]
57    fn test_config_error_display() {
58        let error = Error::Config("test config error".to_string());
59        assert_eq!(error.to_string(), "Configuration error: test config error");
60    }
61
62    #[test]
63    fn test_connection_error_display() {
64        let error = Error::Connection("test connection error".to_string());
65        assert_eq!(error.to_string(), "Connection error: test connection error");
66    }
67
68    #[test]
69    fn test_transaction_error_display() {
70        let error = Error::Transaction("test transaction error".to_string());
71        assert_eq!(
72            error.to_string(),
73            "Transaction error: test transaction error"
74        );
75    }
76
77    #[test]
78    fn test_serialization_error_display() {
79        let error = Error::Serialization("test serialization error".to_string());
80        assert_eq!(
81            error.to_string(),
82            "Serialization error: test serialization error"
83        );
84    }
85
86    #[test]
87    fn test_invalid_address_error_display() {
88        let error = Error::InvalidAddress("test invalid address".to_string());
89        assert_eq!(error.to_string(), "Invalid address: test invalid address");
90    }
91
92    #[test]
93    fn test_unsupported_chain_error_display() {
94        let error = Error::UnsupportedChain("test unsupported chain".to_string());
95        assert_eq!(
96            error.to_string(),
97            "Unsupported chain: test unsupported chain"
98        );
99    }
100
101    #[test]
102    fn test_other_error_display() {
103        let error = Error::Other("test other error".to_string());
104        assert_eq!(error.to_string(), "Error: test other error");
105    }
106
107    #[test]
108    fn test_from_anyhow_error() {
109        let anyhow_err = anyhow::anyhow!("test anyhow error");
110        let error: Error = anyhow_err.into();
111        assert!(matches!(error, Error::Other(_)));
112        assert_eq!(error.to_string(), "Error: test anyhow error");
113    }
114}