1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! Error types for the Raft consensus module
use std::fmt;
/// Result type for Raft operations
pub type RaftResult<T> = Result<T, RaftError>;
/// Errors that can occur during Raft consensus operations
#[derive(Debug, Clone, PartialEq)]
pub enum RaftError {
/// Node is not the leader
NotLeader {
/// Current leader ID if known
leader_id: Option<u64>,
},
/// Invalid node state for this operation
InvalidState {
/// Expected state
expected: String,
/// Actual state
actual: String,
},
/// Log inconsistency detected
LogInconsistency {
/// Description of the inconsistency
reason: String,
},
/// Storage operation failed
StorageError {
/// Error message
message: String,
},
/// Term is stale
StaleTerm {
/// Current term
current: u64,
/// Received term
received: u64,
},
/// Vote already granted to another candidate
VoteAlreadyGranted {
/// Node that received the vote
voted_for: u64,
},
/// Configuration error
ConfigError {
/// Error message
message: String,
},
/// Network error
NetworkError {
/// Error message
message: String,
},
/// Timeout occurred
Timeout {
/// Timeout description
description: String,
},
/// A membership change is already in progress (joint consensus active)
MembershipChangeInProgress,
/// The target node is already a member of the cluster
NodeAlreadyMember {
/// The node ID that is already a member
node_id: u64,
},
/// The target node is not a member of the cluster
NodeNotMember {
/// The node ID that was not found
node_id: u64,
},
/// State machine application error
StateMachineError {
/// Error message
message: String,
},
/// Node is currently replaying its WAL on startup and cannot serve requests
Recovering,
/// Generic error
Other {
/// Error message
message: String,
},
}
impl fmt::Display for RaftError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RaftError::NotLeader { leader_id } => {
write!(f, "Not leader")?;
if let Some(id) = leader_id {
write!(f, " (current leader: {})", id)?;
}
Ok(())
}
RaftError::InvalidState { expected, actual } => {
write!(f, "Invalid state: expected {}, got {}", expected, actual)
}
RaftError::LogInconsistency { reason } => {
write!(f, "Log inconsistency: {}", reason)
}
RaftError::StorageError { message } => {
write!(f, "Storage error: {}", message)
}
RaftError::StaleTerm { current, received } => {
write!(f, "Stale term: current {}, received {}", current, received)
}
RaftError::VoteAlreadyGranted { voted_for } => {
write!(f, "Vote already granted to node {}", voted_for)
}
RaftError::ConfigError { message } => {
write!(f, "Configuration error: {}", message)
}
RaftError::NetworkError { message } => {
write!(f, "Network error: {}", message)
}
RaftError::Timeout { description } => {
write!(f, "Timeout: {}", description)
}
RaftError::MembershipChangeInProgress => {
write!(
f,
"A membership change is already in progress (joint consensus active)"
)
}
RaftError::NodeAlreadyMember { node_id } => {
write!(f, "Node {} is already a member of the cluster", node_id)
}
RaftError::NodeNotMember { node_id } => {
write!(f, "Node {} is not a member of the cluster", node_id)
}
RaftError::StateMachineError { message } => {
write!(f, "State machine error: {}", message)
}
RaftError::Recovering => {
write!(
f,
"Node is replaying WAL on startup and cannot serve requests"
)
}
RaftError::Other { message } => {
write!(f, "Error: {}", message)
}
}
}
}
impl std::error::Error for RaftError {}