Skip to main content

raft_seedable/
errors.rs

1// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
2use thiserror::Error;
3
4/// The base error type for raft
5#[derive(Debug, Error)]
6pub enum Error {
7    /// An IO error occurred
8    #[error("{0}")]
9    Io(#[from] std::io::Error),
10    /// A storage error occurred.
11    #[error("{0}")]
12    Store(#[from] StorageError),
13    /// Raft cannot step the local message.
14    #[error("raft: cannot step raft local message")]
15    StepLocalMsg,
16    /// The raft peer is not found and thus cannot step.
17    #[error("raft: cannot step as peer not found")]
18    StepPeerNotFound,
19    /// The proposal of changes was dropped.
20    #[error("raft: proposal dropped")]
21    ProposalDropped,
22    /// The configuration is invalid.
23    #[error("{0}")]
24    ConfigInvalid(String),
25    /// A protobuf message codec failed in some manner.
26    #[error("protobuf codec error {0:?}")]
27    CodecError(#[from] protobuf::ProtobufError),
28    /// The node exists, but should not.
29    #[error("The node {id} already exists in the {set} set.")]
30    Exists {
31        /// The node id.
32        id: u64,
33        /// The node set.
34        set: &'static str,
35    },
36    /// The node does not exist, but should.
37    #[error("The node {id} is not in the {set} set.")]
38    NotExists {
39        /// The node id.
40        id: u64,
41        /// The node set.
42        set: &'static str,
43    },
44    /// ConfChange proposal is invalid.
45    #[error("{0}")]
46    ConfChangeError(String),
47    /// The request snapshot is dropped.
48    #[error("raft: request snapshot dropped")]
49    RequestSnapshotDropped,
50}
51
52impl PartialEq for Error {
53    #[cfg_attr(feature = "cargo-clippy", allow(clippy::match_same_arms))]
54    fn eq(&self, other: &Error) -> bool {
55        match (self, other) {
56            (Error::StepPeerNotFound, Error::StepPeerNotFound) => true,
57            (Error::ProposalDropped, Error::ProposalDropped) => true,
58            (Error::Store(ref e1), Error::Store(ref e2)) => e1 == e2,
59            (Error::Io(ref e1), Error::Io(ref e2)) => e1.kind() == e2.kind(),
60            (Error::StepLocalMsg, Error::StepLocalMsg) => true,
61            (Error::ConfigInvalid(ref e1), Error::ConfigInvalid(ref e2)) => e1 == e2,
62            (Error::RequestSnapshotDropped, Error::RequestSnapshotDropped) => true,
63            (Error::ConfChangeError(e1), Error::ConfChangeError(e2)) => e1 == e2,
64            _ => false,
65        }
66    }
67}
68
69/// An error with the storage.
70#[derive(Debug, Error)]
71pub enum StorageError {
72    /// The storage was compacted and not accessible
73    #[error("log compacted")]
74    Compacted,
75    /// The log is not available.
76    #[error("log unavailable")]
77    Unavailable,
78    /// The log is being fetched.
79    #[error("log is temporarily unavailable")]
80    LogTemporarilyUnavailable,
81    /// The snapshot is out of date.
82    #[error("snapshot out of date")]
83    SnapshotOutOfDate,
84    /// The snapshot is being created.
85    #[error("snapshot is temporarily unavailable")]
86    SnapshotTemporarilyUnavailable,
87    /// Some other error occurred.
88    #[error("unknown error {0}")]
89    Other(#[from] Box<dyn std::error::Error + Sync + Send>),
90}
91
92impl PartialEq for StorageError {
93    #[cfg_attr(feature = "cargo-clippy", allow(clippy::match_same_arms))]
94    fn eq(&self, other: &StorageError) -> bool {
95        matches!(
96            (self, other),
97            (StorageError::Compacted, StorageError::Compacted)
98                | (StorageError::Unavailable, StorageError::Unavailable)
99                | (
100                    StorageError::LogTemporarilyUnavailable,
101                    StorageError::LogTemporarilyUnavailable
102                )
103                | (
104                    StorageError::SnapshotOutOfDate,
105                    StorageError::SnapshotOutOfDate
106                )
107                | (
108                    StorageError::SnapshotTemporarilyUnavailable,
109                    StorageError::SnapshotTemporarilyUnavailable,
110                )
111        )
112    }
113}
114
115/// A result type that wraps up the raft errors.
116pub type Result<T> = std::result::Result<T, Error>;
117
118#[allow(clippy::eq_op)]
119#[cfg(test)]
120mod tests {
121    use super::*;
122    use std::io;
123
124    #[test]
125    fn test_error_equal() {
126        assert_eq!(Error::StepPeerNotFound, Error::StepPeerNotFound);
127        assert_eq!(
128            Error::Store(StorageError::Compacted),
129            Error::Store(StorageError::Compacted)
130        );
131        assert_eq!(
132            Error::Io(io::Error::new(io::ErrorKind::UnexpectedEof, "oh no!")),
133            Error::Io(io::Error::new(io::ErrorKind::UnexpectedEof, "oh yes!"))
134        );
135        assert_ne!(
136            Error::Io(io::Error::new(io::ErrorKind::NotFound, "error")),
137            Error::Io(io::Error::new(io::ErrorKind::BrokenPipe, "error"))
138        );
139        assert_eq!(Error::StepLocalMsg, Error::StepLocalMsg);
140        assert_eq!(
141            Error::ConfigInvalid(String::from("config error")),
142            Error::ConfigInvalid(String::from("config error"))
143        );
144        assert_ne!(
145            Error::ConfigInvalid(String::from("config error")),
146            Error::ConfigInvalid(String::from("other error"))
147        );
148        assert_eq!(
149            Error::from(io::Error::new(io::ErrorKind::Other, "oh no!")),
150            Error::from(io::Error::new(io::ErrorKind::Other, "oh yes!"))
151        );
152        assert_ne!(
153            Error::StepPeerNotFound,
154            Error::Store(StorageError::Compacted)
155        );
156    }
157
158    #[test]
159    fn test_storage_error_equal() {
160        assert_eq!(StorageError::Compacted, StorageError::Compacted);
161        assert_eq!(StorageError::Unavailable, StorageError::Unavailable);
162        assert_eq!(
163            StorageError::SnapshotOutOfDate,
164            StorageError::SnapshotOutOfDate
165        );
166        assert_eq!(
167            StorageError::SnapshotTemporarilyUnavailable,
168            StorageError::SnapshotTemporarilyUnavailable
169        );
170        assert_ne!(StorageError::Compacted, StorageError::Unavailable);
171        assert_ne!(
172            StorageError::Other(Box::new(StorageError::Unavailable)),
173            StorageError::Unavailable
174        );
175    }
176}