1use thiserror::Error;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Error, Debug)]
10pub enum Error {
11 #[error("Configuration error: {0}")]
13 Config(String),
14
15 #[error("Connection error: {0}")]
17 Connection(String),
18
19 #[error("Transaction error: {0}")]
21 Transaction(String),
22
23 #[error("Chain not supported: {0}")]
25 UnsupportedChain(String),
26
27 #[error("Invalid address format: {0}")]
29 InvalidAddress(String),
30
31 #[error("Substrate adapter error: {0}")]
33 Substrate(#[from] apex_sdk_substrate::Error),
34
35 #[error("EVM adapter error: {0}")]
37 Evm(#[from] apex_sdk_evm::Error),
38
39 #[error("Serialization error: {0}")]
41 Serialization(String),
42
43 #[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}