network_protocol/
error.rs1use serde::{Deserialize, Serialize};
40use std::io;
41use thiserror::Error;
42
43pub mod constants {
46 pub const ERR_DISPATCHER_WRITE_LOCK: &str = "Failed to acquire write lock on dispatcher";
48 pub const ERR_DISPATCHER_READ_LOCK: &str = "Failed to acquire read lock on dispatcher";
49
50 pub const ERR_INVALID_HEADER: &str = "Invalid protocol header";
52 pub const ERR_INVALID_PACKET: &str = "Invalid packet structure";
53 pub const ERR_OVERSIZED_PACKET: &str = "Packet exceeds maximum size";
54
55 pub const ERR_CONNECTION_CLOSED: &str = "Connection closed";
57 pub const ERR_CONNECTION_TIMEOUT: &str = "Connection timed out (no activity)";
58 pub const ERR_TIMEOUT: &str = "Operation timed out";
59
60 pub const ERR_ENCRYPTION_FAILED: &str = "Encryption failed";
62 pub const ERR_DECRYPTION_FAILED: &str = "Decryption failed";
63
64 pub const ERR_COMPRESSION_FAILED: &str = "Compression failed";
66 pub const ERR_DECOMPRESSION_FAILED: &str = "Decompression failed";
67
68 pub const ERR_UNSUPPORTED_VERSION: &str = "Unsupported protocol version";
70 pub const ERR_HANDSHAKE_FAILED: &str = "Handshake failed";
71 pub const ERR_UNEXPECTED_MESSAGE: &str = "Unexpected message type";
72
73 pub const ERR_SECURITY_ERROR: &str = "Security violation detected";
75 pub const ERR_LOCK_POISONED: &str = "Synchronization primitive poisoned";
76
77 pub const ERR_SYSTEM_TIME: &str = "System time error: time went backwards";
79 pub const ERR_INVALID_TIMESTAMP: &str = "Invalid or stale timestamp";
80 pub const ERR_REPLAY_ATTACK: &str = "Replay attack detected - nonce/timestamp already seen";
81 pub const ERR_CLIENT_NONCE_NOT_FOUND: &str = "Client nonce not found";
82 pub const ERR_SERVER_NONCE_NOT_FOUND: &str = "Server nonce not found";
83 pub const ERR_CLIENT_SECRET_NOT_FOUND: &str = "Client secret not found";
84 pub const ERR_SERVER_SECRET_NOT_FOUND: &str = "Server secret not found";
85 pub const ERR_CLIENT_PUBLIC_NOT_FOUND: &str = "Client public key not found";
86 pub const ERR_SERVER_PUBLIC_NOT_FOUND: &str = "Server public key not found";
87 pub const ERR_NONCE_VERIFICATION_FAILED: &str = "Server failed to verify client nonce";
88 pub const ERR_SERVER_VERIFICATION_FAILED: &str = "Client failed to verify server nonce";
89}
90
91#[derive(Error, Debug, Serialize, Deserialize)]
93pub enum ProtocolError {
94 #[error("I/O error: {0}")]
95 #[serde(skip_serializing, skip_deserializing)]
96 Io(#[from] io::Error),
97
98 #[error("Serialization error: {0}")]
99 #[serde(skip_serializing, skip_deserializing)]
100 Serialization(#[from] bincode::Error),
101
102 #[error("Serialize error: {0}")]
103 SerializeError(String),
104
105 #[error("Deserialize error: {0}")]
106 DeserializeError(String),
107
108 #[error("Serialization error: {0}")]
109 SerializationError(String),
110
111 #[error("Transport error: {0}")]
112 TransportError(String),
113
114 #[error("Connection closed")]
115 ConnectionClosed,
116
117 #[error("Security error: {0}")]
118 SecurityError(String),
119
120 #[error("Invalid protocol header")]
121 InvalidHeader,
122
123 #[error("Unsupported protocol version: {0}")]
124 UnsupportedVersion(u8),
125
126 #[error("Packet too large: {0} bytes")]
127 OversizedPacket(usize),
128
129 #[error("Decryption failed")]
130 DecryptionFailure,
131
132 #[error("Encryption failed")]
133 EncryptionFailure,
134
135 #[error("Compression failed")]
136 CompressionFailure,
137
138 #[error("Decompression failed")]
139 DecompressionFailure,
140
141 #[error("Handshake failed: {0}")]
142 HandshakeError(String),
143
144 #[error("Unexpected message type")]
145 UnexpectedMessage,
146
147 #[error("Timeout occurred")]
148 Timeout,
149
150 #[error("Connection timed out (no activity)")]
151 ConnectionTimeout,
152
153 #[error("Configuration error: {0}")]
154 ConfigError(String),
155
156 #[error("Custom error: {0}")]
157 Custom(String),
158
159 #[error("TLS error: {0}")]
160 TlsError(String),
161}
162
163pub type Result<T> = std::result::Result<T, ProtocolError>;