-
Notifications
You must be signed in to change notification settings - Fork 552
/
error.rs
209 lines (180 loc) · 6.11 KB
/
error.rs
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
208
209
use serde_derive::{Deserialize, Serialize};
/// toyDB errors.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Error {
/// The operation was aborted and must be retried. This typically happens
/// with e.g. Raft leader changes.
Abort,
/// Invalid data, typically decoding errors or unexpected internal values.
InvalidData(String),
/// Invalid user input, typically parser or query errors.
InvalidInput(String),
/// An IO error.
IO(String),
/// A write was attempted in a read-only transaction.
ReadOnly,
/// A write transaction conflicted with a different writer and lost. The
/// transaction must be retried.
Serialization,
}
impl std::error::Error for Error {}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Error::Abort => write!(f, "operation aborted"),
Error::InvalidData(msg) => write!(f, "invalid data: {msg}"),
Error::InvalidInput(msg) => write!(f, "invalid input: {msg}"),
Error::IO(msg) => write!(f, "io error: {msg}"),
Error::ReadOnly => write!(f, "read-only transaction"),
Error::Serialization => write!(f, "serialization failure, retry transaction"),
}
}
}
impl Error {
/// Returns whether the error is considered deterministic. State machine
/// application needs to know whether a command failure is deterministic on
/// the input command -- if it is, the command can be considered applied and
/// the error returned to the client, but otherwise the state machine must
/// panic to prevent replica divergence.
pub fn is_deterministic(&self) -> bool {
match self {
// Aborts don't happen during application, only leader changes.
Error::Abort => true,
// Possible data corruption local to this node.
Error::InvalidData(_) => false,
// Input errors are (likely) deterministic. We could employ command
// checksums to be sure.
Error::InvalidInput(_) => true,
// IO errors are typically node-local.
Error::IO(_) => false,
// Write commands in read-only transactions are deterministic.
Error::ReadOnly => true,
// Write conflicts are determinstic.
Error::Serialization => true,
}
}
}
/// Constructs an Error::InvalidData via format!() and into().
#[macro_export]
macro_rules! errdata {
($($args:tt)*) => { $crate::error::Error::InvalidData(format!($($args)*)).into() };
}
/// Constructs an Error::InvalidInput via format!() and into().
#[macro_export]
macro_rules! errinput {
($($args:tt)*) => { $crate::error::Error::InvalidInput(format!($($args)*)).into() };
}
/// Result returning Error.
pub type Result<T> = std::result::Result<T, Error>;
impl<T> From<Error> for Result<T> {
fn from(error: Error) -> Self {
Err(error)
}
}
impl serde::de::Error for Error {
fn custom<T: std::fmt::Display>(msg: T) -> Self {
Error::InvalidData(msg.to_string())
}
}
impl serde::ser::Error for Error {
fn custom<T: std::fmt::Display>(msg: T) -> Self {
Error::InvalidData(msg.to_string())
}
}
impl From<Box<bincode::ErrorKind>> for Error {
fn from(err: Box<bincode::ErrorKind>) -> Self {
Error::InvalidData(err.to_string())
}
}
impl From<config::ConfigError> for Error {
fn from(err: config::ConfigError) -> Self {
Error::InvalidInput(err.to_string())
}
}
impl From<crossbeam::channel::RecvError> for Error {
fn from(err: crossbeam::channel::RecvError) -> Self {
Error::IO(err.to_string())
}
}
impl<T> From<crossbeam::channel::SendError<T>> for Error {
fn from(err: crossbeam::channel::SendError<T>) -> Self {
Error::IO(err.to_string())
}
}
impl From<crossbeam::channel::TryRecvError> for Error {
fn from(err: crossbeam::channel::TryRecvError) -> Self {
Error::IO(err.to_string())
}
}
impl<T> From<crossbeam::channel::TrySendError<T>> for Error {
fn from(err: crossbeam::channel::TrySendError<T>) -> Self {
Error::IO(err.to_string())
}
}
impl From<hdrhistogram::CreationError> for Error {
fn from(err: hdrhistogram::CreationError) -> Self {
panic!("{err}")
}
}
impl From<hdrhistogram::RecordError> for Error {
fn from(err: hdrhistogram::RecordError) -> Self {
Error::InvalidInput(err.to_string())
}
}
impl From<log::ParseLevelError> for Error {
fn from(err: log::ParseLevelError) -> Self {
Error::InvalidInput(err.to_string())
}
}
impl From<log::SetLoggerError> for Error {
fn from(err: log::SetLoggerError) -> Self {
panic!("{err}")
}
}
impl From<regex::Error> for Error {
fn from(err: regex::Error) -> Self {
panic!("{err}")
}
}
impl From<rustyline::error::ReadlineError> for Error {
fn from(err: rustyline::error::ReadlineError) -> Self {
Error::IO(err.to_string())
}
}
impl From<std::array::TryFromSliceError> for Error {
fn from(err: std::array::TryFromSliceError) -> Self {
Error::InvalidData(err.to_string())
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::IO(err.to_string())
}
}
impl From<std::num::ParseFloatError> for Error {
fn from(err: std::num::ParseFloatError) -> Self {
Error::InvalidInput(err.to_string())
}
}
impl From<std::num::ParseIntError> for Error {
fn from(err: std::num::ParseIntError) -> Self {
Error::InvalidInput(err.to_string())
}
}
impl From<std::num::TryFromIntError> for Error {
fn from(err: std::num::TryFromIntError) -> Self {
Error::InvalidData(err.to_string())
}
}
impl From<std::string::FromUtf8Error> for Error {
fn from(err: std::string::FromUtf8Error) -> Self {
Error::InvalidData(err.to_string())
}
}
impl<T> From<std::sync::PoisonError<T>> for Error {
fn from(err: std::sync::PoisonError<T>) -> Self {
// This only happens when a different thread panics while holding a
// mutex. This should be fatal, so we panic here too.
panic!("{err}")
}
}