Skip to main content

csv_adapter_core/
error.rs

1//! Error types for CSV adapters
2
3use thiserror::Error;
4
5/// Result type alias for adapter operations
6pub type Result<T> = core::result::Result<T, AdapterError>;
7
8/// Error types for CSV adapter operations
9#[derive(Error, Debug)]
10pub enum AdapterError {
11    /// Seal has already been used (replay attack)
12    #[error("Seal replay detected: seal {0:?}")]
13    SealReplay(String),
14
15    /// Seal is invalid or malformed
16    #[error("Invalid seal: {0}")]
17    InvalidSeal(String),
18
19    /// Commitment hash mismatch
20    #[error("Commitment hash mismatch: expected {expected}, got {actual}")]
21    CommitmentMismatch {
22        /// Expected commitment hash
23        expected: String,
24        /// Actual commitment hash
25        actual: String,
26    },
27
28    /// Inclusion proof verification failed
29    #[error("Inclusion proof failed: {0}")]
30    InclusionProofFailed(String),
31
32    /// Finality not reached
33    #[error("Finality not reached: {0}")]
34    FinalityNotReached(String),
35
36    /// Chain reorg invalidated anchor
37    #[error("Anchor invalidated by reorg: {0:?}")]
38    ReorgInvalid(String),
39
40    /// Network or RPC error
41    #[error("Network error: {0}")]
42    NetworkError(String),
43
44    /// Transaction publishing failed
45    #[error("Publish failed: {0}")]
46    PublishFailed(String),
47
48    /// Serialization/deserialization error
49    #[error("Serialization error: {0}")]
50    SerializationError(String),
51
52    /// Invalid configuration
53    #[error("Invalid configuration: {0}")]
54    InvalidConfig(String),
55
56    /// Version mismatch
57    #[error("Version mismatch: expected {expected}, got {actual}")]
58    VersionMismatch {
59        /// Expected version
60        expected: u8,
61        /// Actual version
62        actual: u8,
63    },
64
65    /// Domain separator mismatch
66    #[error("Domain separator mismatch")]
67    DomainSeparatorMismatch,
68
69    /// Signature verification failed
70    #[error("Signature verification failed: {0}")]
71    SignatureVerificationFailed(String),
72
73    /// Generic error with message
74    #[error("Adapter error: {0}")]
75    Generic(String),
76}
77
78impl AdapterError {
79    /// Check if this error is a reorg-related error
80    pub fn is_reorg(&self) -> bool {
81        matches!(self, AdapterError::ReorgInvalid(_))
82    }
83
84    /// Check if this error is a replay attack detection
85    pub fn is_replay(&self) -> bool {
86        matches!(self, AdapterError::SealReplay(_))
87    }
88
89    /// Check if this error is a signature verification failure
90    pub fn is_signature_error(&self) -> bool {
91        matches!(self, AdapterError::SignatureVerificationFailed(_))
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98
99    #[test]
100    fn test_error_display() {
101        let err = AdapterError::SealReplay("abc123".to_string());
102        assert!(err.to_string().contains("replay"));
103    }
104
105    #[test]
106    fn test_error_is_reorg() {
107        let err = AdapterError::ReorgInvalid("anchor".to_string());
108        assert!(err.is_reorg());
109    }
110
111    #[test]
112    fn test_error_is_replay() {
113        let err = AdapterError::SealReplay("seal".to_string());
114        assert!(err.is_replay());
115    }
116
117    #[test]
118    fn test_error_is_signature_error() {
119        let err = AdapterError::SignatureVerificationFailed("invalid sig".to_string());
120        assert!(err.is_signature_error());
121    }
122
123    #[test]
124    fn test_error_signature_verification_failed() {
125        let err = AdapterError::SignatureVerificationFailed("bad signature".to_string());
126        assert!(err.to_string().contains("Signature verification failed"));
127    }
128}