apex_sdk/
error.rs

1//! Error types for 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/// Apex SDK error types
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    /// Chain not supported
24    #[error("Chain not supported: {0}")]
25    UnsupportedChain(String),
26
27    /// Invalid address format
28    #[error("Invalid address format: {0}")]
29    InvalidAddress(String),
30
31    /// Substrate adapter error
32    #[error("Substrate adapter error: {0}")]
33    Substrate(#[from] apex_sdk_substrate::Error),
34
35    /// EVM adapter error
36    #[error("EVM adapter error: {0}")]
37    Evm(#[from] apex_sdk_evm::Error),
38
39    /// Serialization error
40    #[error("Serialization error: {0}")]
41    Serialization(String),
42
43    /// Generic error
44    #[error("{0}")]
45    Other(String),
46}
47
48impl From<anyhow::Error> for Error {
49    fn from(err: anyhow::Error) -> Self {
50        Error::Other(err.to_string())
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_config_error_display() {
60        let error = Error::Config("test config error".to_string());
61        assert_eq!(error.to_string(), "Configuration error: test config error");
62    }
63
64    #[test]
65    fn test_connection_error_display() {
66        let error = Error::Connection("failed to connect".to_string());
67        assert_eq!(error.to_string(), "Connection error: failed to connect");
68    }
69
70    #[test]
71    fn test_transaction_error_display() {
72        let error = Error::Transaction("invalid transaction".to_string());
73        assert_eq!(error.to_string(), "Transaction error: invalid transaction");
74    }
75
76    #[test]
77    fn test_unsupported_chain_error_display() {
78        let error = Error::UnsupportedChain("Unknown".to_string());
79        assert_eq!(error.to_string(), "Chain not supported: Unknown");
80    }
81
82    #[test]
83    fn test_invalid_address_error_display() {
84        let error = Error::InvalidAddress("0xinvalid".to_string());
85        assert_eq!(error.to_string(), "Invalid address format: 0xinvalid");
86    }
87
88    #[test]
89    fn test_serialization_error_display() {
90        let error = Error::Serialization("JSON parse error".to_string());
91        assert_eq!(error.to_string(), "Serialization error: JSON parse error");
92    }
93
94    #[test]
95    fn test_other_error_display() {
96        let error = Error::Other("generic error".to_string());
97        assert_eq!(error.to_string(), "generic error");
98    }
99
100    #[test]
101    fn test_from_anyhow_error() {
102        let anyhow_err = anyhow::anyhow!("test error");
103        let error: Error = anyhow_err.into();
104        assert!(matches!(error, Error::Other(_)));
105        assert_eq!(error.to_string(), "test error");
106    }
107
108    #[test]
109    fn test_error_is_send_sync() {
110        fn assert_send<T: Send>() {}
111        fn assert_sync<T: Sync>() {}
112        assert_send::<Error>();
113        assert_sync::<Error>();
114    }
115}