chaincraft_rust/
error.rs

1//! Error types for the Chaincraft library
2
3use std::net::SocketAddr;
4use thiserror::Error;
5
6/// Result type alias for Chaincraft operations
7pub type Result<T> = std::result::Result<T, ChaincraftError>;
8
9/// Main error type for Chaincraft operations
10#[derive(Error, Debug)]
11pub enum ChaincraftError {
12    /// Network-related errors
13    #[error("Network error: {0}")]
14    Network(#[from] NetworkError),
15
16    /// Cryptographic errors
17    #[error("Cryptographic error: {0}")]
18    Crypto(#[from] CryptoError),
19
20    /// Storage-related errors
21    #[error("Storage error: {0}")]
22    Storage(#[from] StorageError),
23
24    /// Serialization errors
25    #[error("Serialization error: {0}")]
26    Serialization(#[from] SerializationError),
27
28    /// Validation errors
29    #[error("Validation error: {0}")]
30    Validation(String),
31
32    /// Consensus-related errors
33    #[error("Consensus error: {0}")]
34    Consensus(String),
35
36    /// Configuration errors
37    #[error("Configuration error: {0}")]
38    Config(String),
39
40    /// Generic IO errors
41    #[error("IO error: {0}")]
42    Io(#[from] std::io::Error),
43
44    /// Generic errors with message
45    #[error("{0}")]
46    Generic(String),
47}
48
49/// Network-specific error types
50#[derive(Error, Debug)]
51pub enum NetworkError {
52    /// Failed to bind to socket
53    #[error("Failed to bind to {addr}: {source}")]
54    BindFailed {
55        addr: SocketAddr,
56        source: std::io::Error,
57    },
58
59    /// Failed to connect to peer
60    #[error("Failed to connect to {addr}: {source}")]
61    ConnectionFailed {
62        addr: SocketAddr,
63        source: std::io::Error,
64    },
65
66    /// Peer is banned
67    #[error("Peer {addr} is banned until {expires_at}")]
68    PeerBanned {
69        addr: SocketAddr,
70        expires_at: chrono::DateTime<chrono::Utc>,
71    },
72
73    /// Message too large
74    #[error("Message size {size} exceeds maximum {max_size}")]
75    MessageTooLarge { size: usize, max_size: usize },
76
77    /// Invalid message format
78    #[error("Invalid message format: {reason}")]
79    InvalidMessage { reason: String },
80
81    /// Timeout occurred
82    #[error("Operation timed out after {duration:?}")]
83    Timeout { duration: std::time::Duration },
84
85    /// No peers available
86    #[error("No peers available for operation")]
87    NoPeersAvailable,
88}
89
90/// Cryptographic error types
91#[derive(Error, Debug)]
92pub enum CryptoError {
93    /// Invalid signature
94    #[error("Invalid signature")]
95    InvalidSignature,
96
97    /// Invalid public key
98    #[error("Invalid public key: {reason}")]
99    InvalidPublicKey { reason: String },
100
101    /// Invalid private key
102    #[error("Invalid private key: {reason}")]
103    InvalidPrivateKey { reason: String },
104
105    /// Hash verification failed
106    #[error("Hash verification failed")]
107    HashVerificationFailed,
108
109    /// Proof of work verification failed
110    #[error("Proof of work verification failed")]
111    ProofOfWorkFailed,
112
113    /// VRF verification failed
114    #[error("VRF verification failed")]
115    VrfVerificationFailed,
116
117    /// VDF verification failed
118    #[error("VDF verification failed")]
119    VdfVerificationFailed,
120
121    /// Key generation failed
122    #[error("Key generation failed: {reason}")]
123    KeyGenerationFailed { reason: String },
124
125    /// Encryption failed
126    #[error("Encryption failed: {reason}")]
127    EncryptionFailed { reason: String },
128
129    /// Decryption failed
130    #[error("Decryption failed: {reason}")]
131    DecryptionFailed { reason: String },
132}
133
134/// Storage-related error types
135#[derive(Error, Debug)]
136pub enum StorageError {
137    /// Database operation failed
138    #[error("Database operation failed: {reason}")]
139    DatabaseOperation { reason: String },
140
141    /// Key not found
142    #[error("Key not found: {key}")]
143    KeyNotFound { key: String },
144
145    /// Serialization failed
146    #[error("Failed to serialize data: {reason}")]
147    SerializationFailed { reason: String },
148
149    /// Deserialization failed
150    #[error("Failed to deserialize data: {reason}")]
151    DeserializationFailed { reason: String },
152
153    /// Database corruption detected
154    #[error("Database corruption detected: {reason}")]
155    Corruption { reason: String },
156
157    /// Database is read-only
158    #[error("Attempted to write to read-only database")]
159    ReadOnly,
160
161    /// Transaction failed
162    #[error("Transaction failed: {reason}")]
163    TransactionFailed { reason: String },
164}
165
166/// Serialization error types
167#[derive(Error, Debug)]
168pub enum SerializationError {
169    /// JSON serialization error
170    #[error("JSON error: {0}")]
171    Json(#[from] serde_json::Error),
172
173    /// Binary serialization error
174    #[error("Binary serialization error: {0}")]
175    Binary(#[from] bincode::Error),
176
177    /// Invalid message format
178    #[error("Invalid message format: expected {expected}, got {actual}")]
179    InvalidFormat { expected: String, actual: String },
180
181    /// Missing required field
182    #[error("Missing required field: {field}")]
183    MissingField { field: String },
184
185    /// Field validation failed
186    #[error("Field validation failed for {field}: {reason}")]
187    FieldValidation { field: String, reason: String },
188}
189
190impl ChaincraftError {
191    /// Create a validation error
192    pub fn validation<T: Into<String>>(msg: T) -> Self {
193        ChaincraftError::Validation(msg.into())
194    }
195
196    /// Create a consensus error
197    pub fn consensus<T: Into<String>>(msg: T) -> Self {
198        ChaincraftError::Consensus(msg.into())
199    }
200
201    /// Create a configuration error
202    pub fn config<T: Into<String>>(msg: T) -> Self {
203        ChaincraftError::Config(msg.into())
204    }
205
206    /// Create a generic error
207    pub fn generic<T: Into<String>>(msg: T) -> Self {
208        ChaincraftError::Generic(msg.into())
209    }
210}
211
212impl From<serde_json::Error> for ChaincraftError {
213    fn from(err: serde_json::Error) -> Self {
214        ChaincraftError::Serialization(SerializationError::Json(err))
215    }
216}