Skip to main content

vcl_protocol/
error.rs

1//! # VCL Error Types
2//!
3//! All errors returned by VCL Protocol operations are represented
4//! as variants of [`VCLError`]. It implements [`std::error::Error`]
5//! and can be used with the `?` operator throughout.
6
7use std::fmt;
8
9/// The main error type for VCL Protocol operations.
10///
11/// Every public method that can fail returns `Result<_, VCLError>`.
12///
13/// # Example
14///
15/// ```no_run
16/// use vcl_protocol::VCLError;
17///
18/// fn handle(err: VCLError) {
19///     match err {
20///         VCLError::ConnectionClosed => println!("Connection was closed"),
21///         VCLError::Timeout          => println!("Timed out"),
22///         other                      => println!("Other error: {}", other),
23///     }
24/// }
25/// ```
26#[derive(Debug)]
27pub enum VCLError {
28    /// Encryption or decryption operation failed.
29    CryptoError(String),
30    /// Ed25519 signature verification failed — packet may be tampered.
31    SignatureInvalid,
32    /// A key has wrong length or invalid format.
33    InvalidKey(String),
34    /// The `prev_hash` field does not match the expected value — chain is broken.
35    ChainValidationFailed,
36    /// A packet with this sequence number or nonce was already received.
37    ReplayDetected(String),
38    /// Packet has unexpected structure or payload.
39    InvalidPacket(String),
40    /// Operation attempted on a closed connection.
41    ConnectionClosed,
42    /// No activity for longer than the configured `timeout_secs`.
43    Timeout,
44    /// `send()` called before a peer address is known.
45    NoPeerAddress,
46    /// `send()` or `recv()` called before the handshake completed.
47    NoSharedSecret,
48    /// X25519 handshake could not be completed.
49    HandshakeFailed(String),
50    /// Server received a non-ClientHello message during handshake.
51    ExpectedClientHello,
52    /// Client received a non-ServerHello message during handshake.
53    ExpectedServerHello,
54    /// Bincode serialization or deserialization failed.
55    SerializationError(String),
56    /// UDP socket error or address parse failure.
57    IoError(String),
58}
59
60impl fmt::Display for VCLError {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        match self {
63            VCLError::CryptoError(msg)       => write!(f, "Crypto error: {}", msg),
64            VCLError::SignatureInvalid        => write!(f, "Signature validation failed"),
65            VCLError::InvalidKey(msg)         => write!(f, "Invalid key: {}", msg),
66            VCLError::ChainValidationFailed   => write!(f, "Chain validation failed"),
67            VCLError::ReplayDetected(msg)     => write!(f, "Replay detected: {}", msg),
68            VCLError::InvalidPacket(msg)      => write!(f, "Invalid packet: {}", msg),
69            VCLError::ConnectionClosed        => write!(f, "Connection closed"),
70            VCLError::Timeout                 => write!(f, "Connection timeout"),
71            VCLError::NoPeerAddress           => write!(f, "No peer address"),
72            VCLError::NoSharedSecret          => write!(f, "No shared secret"),
73            VCLError::HandshakeFailed(msg)    => write!(f, "Handshake failed: {}", msg),
74            VCLError::ExpectedClientHello     => write!(f, "Expected ClientHello"),
75            VCLError::ExpectedServerHello     => write!(f, "Expected ServerHello"),
76            VCLError::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
77            VCLError::IoError(msg)            => write!(f, "IO error: {}", msg),
78        }
79    }
80}
81
82impl std::error::Error for VCLError {}
83
84impl From<std::io::Error> for VCLError {
85    fn from(err: std::io::Error) -> Self {
86        VCLError::IoError(err.to_string())
87    }
88}
89
90impl From<bincode::Error> for VCLError {
91    fn from(err: bincode::Error) -> Self {
92        VCLError::SerializationError(err.to_string())
93    }
94}
95
96impl From<std::net::AddrParseError> for VCLError {
97    fn from(err: std::net::AddrParseError) -> Self {
98        VCLError::IoError(err.to_string())
99    }
100}
101
102impl From<VCLError> for String {
103    fn from(err: VCLError) -> Self {
104        err.to_string()
105    }
106}