use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error)]
pub enum Error {
#[error("Invalid configuration: {0}")]
InvalidConfig(String),
#[error("Invalid party ID: {0}")]
InvalidPartyId(usize),
#[error("Invalid party role: {0}")]
InvalidPartyRole(String),
#[error("Threshold not met: required {required}, got {actual}")]
ThresholdNotMet { required: usize, actual: usize },
#[error("Invalid signing party combination: {0}")]
InvalidSigningParties(String),
#[error("Policy violation: {0}")]
PolicyViolation(String),
#[error("Spending limit exceeded: {limit} {currency} (attempted: {attempted})")]
SpendingLimitExceeded {
limit: String,
attempted: String,
currency: String,
},
#[error("Address not whitelisted: {0}")]
AddressNotWhitelisted(String),
#[error("Address is blacklisted: {0}")]
AddressBlacklisted(String),
#[error("Transaction outside allowed time window: {0}")]
TimeWindowViolation(String),
#[error("Contract interaction not allowed: {0}")]
ContractNotAllowed(String),
#[error("Message verification failed: {0}")]
VerificationFailed(String),
#[error("Cryptographic error: {0}")]
Crypto(String),
#[error("Invalid signature: {0}")]
InvalidSignature(String),
#[error("Key share not found: {0}")]
KeyShareNotFound(String),
#[error("Storage error: {0}")]
Storage(String),
#[error("Encryption error: {0}")]
Encryption(String),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Deserialization error: {0}")]
Deserialization(String),
#[error("Relay error: {0}")]
Relay(String),
#[error("Timeout waiting for {0}")]
Timeout(String),
#[error("Session not found: {0}")]
SessionNotFound(String),
#[error("Session expired: {0}")]
SessionExpired(String),
#[error("Key derivation error: {0}")]
Derivation(String),
#[error("Hardened derivation not supported in threshold setting")]
HardenedDerivationNotSupported,
#[error("Unsupported chain: {0}")]
UnsupportedChain(String),
#[error("Chain error: {0}")]
ChainError(String),
#[error("Internal error: {0}")]
Internal(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::Serialization(e.to_string())
}
}
impl From<hex::FromHexError> for Error {
fn from(e: hex::FromHexError) -> Self {
Error::Deserialization(e.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = Error::SpendingLimitExceeded {
limit: "1.0".to_string(),
attempted: "2.0".to_string(),
currency: "ETH".to_string(),
};
assert!(err.to_string().contains("1.0"));
assert!(err.to_string().contains("2.0"));
assert!(err.to_string().contains("ETH"));
}
#[test]
fn test_policy_violation() {
let err = Error::PolicyViolation("daily limit exceeded".to_string());
assert!(err.to_string().contains("daily limit"));
}
}