nomad_protocol/core/
error.rs

1//! Error types for NOMAD protocol.
2
3use thiserror::Error;
4
5/// Errors that can occur when applying a diff.
6#[derive(Debug, Error, Clone)]
7pub enum ApplyError {
8    /// Invalid diff format.
9    #[error("invalid diff format")]
10    InvalidFormat,
11
12    /// Diff version mismatch.
13    #[error("diff version mismatch: expected {expected}, got {actual}")]
14    VersionMismatch {
15        /// Expected version.
16        expected: u64,
17        /// Actual version.
18        actual: u64,
19    },
20
21    /// State corruption detected.
22    #[error("state corruption detected")]
23    StateCorruption,
24}
25
26/// Errors that can occur when decoding a diff.
27#[derive(Debug, Error, Clone)]
28pub enum DecodeError {
29    /// Invalid encoding.
30    #[error("invalid encoding: {0}")]
31    InvalidEncoding(String),
32
33    /// Unexpected end of data.
34    #[error("unexpected end of data")]
35    UnexpectedEof,
36
37    /// Unsupported version.
38    #[error("unsupported version: {0}")]
39    UnsupportedVersion(u8),
40}
41
42/// Errors in the crypto layer.
43#[derive(Debug, Error)]
44pub enum CryptoError {
45    /// Handshake failed.
46    #[error("handshake failed: {0}")]
47    HandshakeFailed(String),
48
49    /// AEAD encryption failed.
50    #[error("AEAD encryption failed")]
51    EncryptionFailed,
52
53    /// AEAD decryption failed (invalid tag or corrupted).
54    #[error("AEAD decryption failed (invalid tag or corrupted)")]
55    DecryptionFailed,
56
57    /// Nonce counter exhausted - session must terminate.
58    #[error("nonce counter exhausted - session must terminate")]
59    CounterExhaustion,
60
61    /// Epoch exhausted - session must terminate.
62    #[error("epoch exhausted - session must terminate")]
63    EpochExhaustion,
64
65    /// Replay detected.
66    #[error("replay detected")]
67    ReplayDetected,
68
69    /// Key derivation failed.
70    #[error("key derivation failed")]
71    KeyDerivationFailed,
72}
73
74/// Errors in the sync layer.
75#[derive(Debug, Error)]
76pub enum SyncError {
77    /// Apply error.
78    #[error("apply error: {0}")]
79    Apply(#[from] ApplyError),
80
81    /// Decode error.
82    #[error("decode error: {0}")]
83    Decode(#[from] DecodeError),
84}
85
86/// Top-level NOMAD errors.
87#[derive(Debug, Error)]
88pub enum NomadError {
89    /// Sync error.
90    #[error("sync error: {0}")]
91    Sync(#[from] SyncError),
92
93    /// Crypto error.
94    #[error("crypto error: {0}")]
95    Crypto(#[from] CryptoError),
96
97    /// Configuration error.
98    #[error("configuration error: {0}")]
99    Config(String),
100
101    /// I/O error.
102    #[error("i/o error: {0}")]
103    Io(#[from] std::io::Error),
104}