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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use crate::StateRole;
use protobuf::ProtobufError;
use std::error;
use std::{cmp, io, result};
quick_error! {
#[derive(Debug)]
pub enum Error {
Io(err: io::Error) {
from()
cause(err)
description(err.description())
}
Store(err: StorageError) {
from()
cause(err)
description(err.description())
}
StepLocalMsg {
description("raft: cannot step raft local message")
}
StepPeerNotFound {
description("raft: cannot step as peer not found")
}
ProposalDropped {
description("raft: proposal dropped")
}
ConfigInvalid(desc: String) {
description(desc)
}
CodecError(err: ProtobufError) {
from()
cause(err)
description(err.description())
display("protobuf codec error {:?}", err)
}
Exists(id: u64, set: &'static str) {
display("The node {} already exists in the {} set.", id, set)
}
NotExists(id: u64, set: &'static str) {
display("The node {} is not in the {} set.", id, set)
}
InvalidState(role: StateRole) {
display("Cannot complete that action while in {:?} role.", role)
}
NoPendingMembershipChange {
display("No pending membership change. Create a pending transition with `Raft::propose_membership_change` on the leader.")
}
ViolatesContract(contract: String) {
display("An argument violate a calling contract: {}", contract)
}
RequestSnapshotDropped {
description("raft: request snapshot dropped")
}
}
}
impl cmp::PartialEq for Error {
#[cfg_attr(feature = "cargo-clippy", allow(clippy::match_same_arms))]
fn eq(&self, other: &Error) -> bool {
match (self, other) {
(&Error::StepPeerNotFound, &Error::StepPeerNotFound) => true,
(&Error::ProposalDropped, &Error::ProposalDropped) => true,
(&Error::Store(ref e1), &Error::Store(ref e2)) => e1 == e2,
(&Error::Io(ref e1), &Error::Io(ref e2)) => e1.kind() == e2.kind(),
(&Error::StepLocalMsg, &Error::StepLocalMsg) => true,
(&Error::ConfigInvalid(ref e1), &Error::ConfigInvalid(ref e2)) => e1 == e2,
(&Error::RequestSnapshotDropped, &Error::RequestSnapshotDropped) => true,
_ => false,
}
}
}
quick_error! {
#[derive(Debug)]
pub enum StorageError {
Compacted {
description("log compacted")
}
Unavailable {
description("log unavailable")
}
SnapshotOutOfDate {
description("snapshot out of date")
}
SnapshotTemporarilyUnavailable {
description("snapshot is temporarily unavailable")
}
Other(err: Box<dyn error::Error + Sync + Send>) {
from()
cause(err.as_ref())
description(err.description())
display("unknown error {:?}", err)
}
}
}
impl cmp::PartialEq for StorageError {
#[cfg_attr(feature = "cargo-clippy", allow(clippy::match_same_arms))]
fn eq(&self, other: &StorageError) -> bool {
match (self, other) {
(&StorageError::Compacted, &StorageError::Compacted) => true,
(&StorageError::Unavailable, &StorageError::Unavailable) => true,
(&StorageError::SnapshotOutOfDate, &StorageError::SnapshotOutOfDate) => true,
(
&StorageError::SnapshotTemporarilyUnavailable,
&StorageError::SnapshotTemporarilyUnavailable,
) => true,
_ => false,
}
}
}
pub type Result<T> = result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::*;
use std::io;
#[test]
fn test_error_equal() {
assert_eq!(Error::StepPeerNotFound, Error::StepPeerNotFound);
assert_eq!(
Error::Store(StorageError::Compacted),
Error::Store(StorageError::Compacted)
);
assert_eq!(
Error::Io(io::Error::new(io::ErrorKind::UnexpectedEof, "oh no!")),
Error::Io(io::Error::new(io::ErrorKind::UnexpectedEof, "oh yes!"))
);
assert_ne!(
Error::Io(io::Error::new(io::ErrorKind::NotFound, "error")),
Error::Io(io::Error::new(io::ErrorKind::BrokenPipe, "error"))
);
assert_eq!(Error::StepLocalMsg, Error::StepLocalMsg);
assert_eq!(
Error::ConfigInvalid(String::from("config error")),
Error::ConfigInvalid(String::from("config error"))
);
assert_ne!(
Error::ConfigInvalid(String::from("config error")),
Error::ConfigInvalid(String::from("other error"))
);
assert_eq!(
Error::from(io::Error::new(io::ErrorKind::Other, "oh no!")),
Error::from(io::Error::new(io::ErrorKind::Other, "oh yes!"))
);
assert_ne!(
Error::StepPeerNotFound,
Error::Store(StorageError::Compacted)
);
}
#[test]
fn test_storage_error_equal() {
assert_eq!(StorageError::Compacted, StorageError::Compacted);
assert_eq!(StorageError::Unavailable, StorageError::Unavailable);
assert_eq!(
StorageError::SnapshotOutOfDate,
StorageError::SnapshotOutOfDate
);
assert_eq!(
StorageError::SnapshotTemporarilyUnavailable,
StorageError::SnapshotTemporarilyUnavailable
);
assert_ne!(StorageError::Compacted, StorageError::Unavailable);
assert_ne!(
StorageError::Other(Box::new(StorageError::Unavailable)),
StorageError::Unavailable
);
}
}