network_protocol/
error.rs

1//! # Error Types
2//!
3//! Comprehensive error handling for the network protocol.
4//!
5//! This module defines all error variants that can occur during protocol operations,
6//! from low-level I/O errors to high-level protocol violations.
7//!
8//! ## Error Categories
9//! - **I/O Errors**: Network and file system failures
10//! - **Protocol Errors**: Invalid packets, handshake failures, timeouts
11//! - **Cryptographic Errors**: Encryption/decryption failures
12//! - **TLS Errors**: Certificate and connection issues
13//! - **Compression Errors**: Decompression failures, size limit violations
14//!
15//! All errors implement `std::error::Error` for interoperability.
16//!
17//! ## Example Usage
18//! ```rust
19//! use network_protocol::error::{ProtocolError, Result};
20//! use std::fs::File;
21//! use std::io::Read;
22//! use tracing::{info, error};
23//!
24//! fn read_file(path: &str) -> Result<String> {
25//!     let mut file = File::open(path).map_err(ProtocolError::Io)?;
26//!     let mut contents = String::new();
27//!     file.read_to_string(&mut contents).map_err(ProtocolError::Io)?;
28//!     Ok(contents)
29//! }
30//!
31//! fn main() {
32//!     match read_file("example.txt") {
33//!         Ok(contents) => info!(contents, "Successfully read file"),
34//!         Err(e) => error!(error=%e, "Error reading file"),
35//!     }
36//! }
37//! ```
38
39use serde::{Deserialize, Serialize};
40use std::io;
41use thiserror::Error;
42
43// ProtocolError is the primary error type for all protocol operations
44#[derive(Error, Debug, Serialize, Deserialize)]
45pub enum ProtocolError {
46    #[error("I/O error: {0}")]
47    #[serde(skip_serializing, skip_deserializing)]
48    Io(#[from] io::Error),
49
50    #[error("Serialization error: {0}")]
51    #[serde(skip_serializing, skip_deserializing)]
52    Serialization(#[from] bincode::Error),
53
54    #[error("Serialize error: {0}")]
55    SerializeError(String),
56
57    #[error("Deserialize error: {0}")]
58    DeserializeError(String),
59
60    #[error("Transport error: {0}")]
61    TransportError(String),
62
63    #[error("Connection closed")]
64    ConnectionClosed,
65
66    #[error("Security error: {0}")]
67    SecurityError(String),
68
69    #[error("Invalid protocol header")]
70    InvalidHeader,
71
72    #[error("Unsupported protocol version: {0}")]
73    UnsupportedVersion(u8),
74
75    #[error("Packet too large: {0} bytes")]
76    OversizedPacket(usize),
77
78    #[error("Decryption failed")]
79    DecryptionFailure,
80
81    #[error("Encryption failed")]
82    EncryptionFailure,
83
84    #[error("Compression failed")]
85    CompressionFailure,
86
87    #[error("Decompression failed")]
88    DecompressionFailure,
89
90    #[error("Handshake failed: {0}")]
91    HandshakeError(String),
92
93    #[error("Unexpected message type")]
94    UnexpectedMessage,
95
96    #[error("Timeout occurred")]
97    Timeout,
98
99    #[error("Connection timed out (no activity)")]
100    ConnectionTimeout,
101
102    #[error("Configuration error: {0}")]
103    ConfigError(String),
104
105    #[error("Custom error: {0}")]
106    Custom(String),
107
108    #[error("TLS error: {0}")]
109    TlsError(String),
110}
111
112/// Type alias for Results using ProtocolError
113pub type Result<T> = std::result::Result<T, ProtocolError>;