amaters_cluster/
error.rs

1//! Error types for the Raft consensus module
2
3use std::fmt;
4
5/// Result type for Raft operations
6pub type RaftResult<T> = Result<T, RaftError>;
7
8/// Errors that can occur during Raft consensus operations
9#[derive(Debug, Clone, PartialEq)]
10pub enum RaftError {
11    /// Node is not the leader
12    NotLeader {
13        /// Current leader ID if known
14        leader_id: Option<u64>,
15    },
16    /// Invalid node state for this operation
17    InvalidState {
18        /// Expected state
19        expected: String,
20        /// Actual state
21        actual: String,
22    },
23    /// Log inconsistency detected
24    LogInconsistency {
25        /// Description of the inconsistency
26        reason: String,
27    },
28    /// Storage operation failed
29    StorageError {
30        /// Error message
31        message: String,
32    },
33    /// Term is stale
34    StaleTerm {
35        /// Current term
36        current: u64,
37        /// Received term
38        received: u64,
39    },
40    /// Vote already granted to another candidate
41    VoteAlreadyGranted {
42        /// Node that received the vote
43        voted_for: u64,
44    },
45    /// Configuration error
46    ConfigError {
47        /// Error message
48        message: String,
49    },
50    /// Network error
51    NetworkError {
52        /// Error message
53        message: String,
54    },
55    /// Timeout occurred
56    Timeout {
57        /// Timeout description
58        description: String,
59    },
60    /// Generic error
61    Other {
62        /// Error message
63        message: String,
64    },
65}
66
67impl fmt::Display for RaftError {
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        match self {
70            RaftError::NotLeader { leader_id } => {
71                write!(f, "Not leader")?;
72                if let Some(id) = leader_id {
73                    write!(f, " (current leader: {})", id)?;
74                }
75                Ok(())
76            }
77            RaftError::InvalidState { expected, actual } => {
78                write!(f, "Invalid state: expected {}, got {}", expected, actual)
79            }
80            RaftError::LogInconsistency { reason } => {
81                write!(f, "Log inconsistency: {}", reason)
82            }
83            RaftError::StorageError { message } => {
84                write!(f, "Storage error: {}", message)
85            }
86            RaftError::StaleTerm { current, received } => {
87                write!(f, "Stale term: current {}, received {}", current, received)
88            }
89            RaftError::VoteAlreadyGranted { voted_for } => {
90                write!(f, "Vote already granted to node {}", voted_for)
91            }
92            RaftError::ConfigError { message } => {
93                write!(f, "Configuration error: {}", message)
94            }
95            RaftError::NetworkError { message } => {
96                write!(f, "Network error: {}", message)
97            }
98            RaftError::Timeout { description } => {
99                write!(f, "Timeout: {}", description)
100            }
101            RaftError::Other { message } => {
102                write!(f, "Error: {}", message)
103            }
104        }
105    }
106}
107
108impl std::error::Error for RaftError {}