use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("Invalid chain ID: {0}")]
InvalidChainId(u64),
#[error("Invalid address: {0}")]
InvalidAddress(String),
#[error("Invalid gas: {0}")]
InvalidGas(String),
#[error("Invalid value: {0}")]
InvalidValue(String),
#[error("Invalid nonce: {0}")]
InvalidNonce(String),
#[error("Transaction validation failed: {0}")]
ValidationError(String),
#[error("Signing error: {0}")]
SigningError(String),
#[error("RLP encoding error: {0}")]
RlpEncodingError(String),
#[error("BIP-32 error: {0}")]
Bip32Error(#[from] khodpay_bip32::Error),
#[error("BIP-44 error: {0}")]
Bip44Error(#[from] khodpay_bip44::Error),
#[error("Hex decode error: {0}")]
HexError(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_invalid_chain_id_error() {
let error = Error::InvalidChainId(999);
assert_eq!(error.to_string(), "Invalid chain ID: 999");
}
#[test]
fn test_invalid_address_error() {
let error = Error::InvalidAddress("0xinvalid".to_string());
assert_eq!(error.to_string(), "Invalid address: 0xinvalid");
}
#[test]
fn test_invalid_gas_error() {
let error = Error::InvalidGas("gas limit too low".to_string());
assert_eq!(error.to_string(), "Invalid gas: gas limit too low");
}
#[test]
fn test_invalid_value_error() {
let error = Error::InvalidValue("negative value".to_string());
assert_eq!(error.to_string(), "Invalid value: negative value");
}
#[test]
fn test_invalid_nonce_error() {
let error = Error::InvalidNonce("nonce overflow".to_string());
assert_eq!(error.to_string(), "Invalid nonce: nonce overflow");
}
#[test]
fn test_validation_error() {
let error = Error::ValidationError("max_fee < max_priority_fee".to_string());
assert_eq!(
error.to_string(),
"Transaction validation failed: max_fee < max_priority_fee"
);
}
#[test]
fn test_signing_error() {
let error = Error::SigningError("invalid private key".to_string());
assert_eq!(error.to_string(), "Signing error: invalid private key");
}
#[test]
fn test_rlp_encoding_error() {
let error = Error::RlpEncodingError("encoding failed".to_string());
assert_eq!(error.to_string(), "RLP encoding error: encoding failed");
}
#[test]
fn test_hex_error() {
let error = Error::HexError("invalid hex character".to_string());
assert_eq!(error.to_string(), "Hex decode error: invalid hex character");
}
#[test]
fn test_error_is_send_sync() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<Error>();
}
}