use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("Configuration error: {0}")]
Config(String),
#[error("Connection error: {0}")]
Connection(String),
#[error("Transaction error: {0}")]
Transaction(String),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Invalid address: {0}")]
InvalidAddress(String),
#[error("Unsupported chain: {0}")]
UnsupportedChain(String),
#[error("Error: {0}")]
Other(String),
}
impl From<anyhow::Error> for Error {
fn from(err: anyhow::Error) -> Self {
Error::Other(err.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_is_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<Error>();
}
#[test]
fn test_config_error_display() {
let error = Error::Config("test config error".to_string());
assert_eq!(error.to_string(), "Configuration error: test config error");
}
#[test]
fn test_connection_error_display() {
let error = Error::Connection("test connection error".to_string());
assert_eq!(error.to_string(), "Connection error: test connection error");
}
#[test]
fn test_transaction_error_display() {
let error = Error::Transaction("test transaction error".to_string());
assert_eq!(
error.to_string(),
"Transaction error: test transaction error"
);
}
#[test]
fn test_serialization_error_display() {
let error = Error::Serialization("test serialization error".to_string());
assert_eq!(
error.to_string(),
"Serialization error: test serialization error"
);
}
#[test]
fn test_invalid_address_error_display() {
let error = Error::InvalidAddress("test invalid address".to_string());
assert_eq!(error.to_string(), "Invalid address: test invalid address");
}
#[test]
fn test_unsupported_chain_error_display() {
let error = Error::UnsupportedChain("test unsupported chain".to_string());
assert_eq!(
error.to_string(),
"Unsupported chain: test unsupported chain"
);
}
#[test]
fn test_other_error_display() {
let error = Error::Other("test other error".to_string());
assert_eq!(error.to_string(), "Error: test other error");
}
#[test]
fn test_from_anyhow_error() {
let anyhow_err = anyhow::anyhow!("test anyhow error");
let error: Error = anyhow_err.into();
assert!(matches!(error, Error::Other(_)));
assert_eq!(error.to_string(), "Error: test anyhow error");
}
}