raft_log/
errors.rs

1mod storage_errors;
2
3use std::io;
4
5pub use storage_errors::InvalidChunkFileName;
6
7use crate::api::types::Types;
8
9#[derive(Debug, Clone, PartialEq, Eq)]
10#[derive(thiserror::Error)]
11pub enum RaftLogStateError<T: Types> {
12    #[error(transparent)]
13    VoteReversal(#[from] VoteReversal<T>),
14
15    #[error(transparent)]
16    LogIdReversal(#[from] LogIdReversal<T>),
17
18    #[error(transparent)]
19    LogIdNonConsecutive(#[from] LogIdNonConsecutive<T>),
20
21    #[error(transparent)]
22    LogIndexNotFound(#[from] LogIndexNotFound),
23}
24
25impl<T: Types> From<RaftLogStateError<T>> for io::Error {
26    fn from(value: RaftLogStateError<T>) -> Self {
27        io::Error::new(io::ErrorKind::InvalidInput, value.to_string())
28    }
29}
30
31/// Error indicating that a vote cannot be reversed.
32#[derive(Debug, Clone, PartialEq, Eq)]
33#[derive(thiserror::Error)]
34#[error(
35    "Vote cannot be reversed: current {current:?}, attempted {attempted:?}"
36)]
37pub struct VoteReversal<T: Types> {
38    pub current: T::Vote,
39    pub attempted: T::Vote,
40}
41
42impl<T: Types> VoteReversal<T> {
43    pub fn new(current: T::Vote, attempted: T::Vote) -> Self {
44        Self { current, attempted }
45    }
46}
47
48/// Error indicating that a log id cannot be reversed.
49#[derive(Debug, Clone, PartialEq, Eq)]
50#[derive(thiserror::Error)]
51#[error("Log id cannot be reversed when {when}: current {current:?}, attempted {attempted:?}")]
52pub struct LogIdReversal<T: Types> {
53    pub current: T::LogId,
54    pub attempted: T::LogId,
55    pub when: &'static str,
56}
57
58impl<T: Types> LogIdReversal<T> {
59    pub fn new(
60        current: T::LogId,
61        attempted: T::LogId,
62        when: &'static str,
63    ) -> Self {
64        Self {
65            current,
66            attempted,
67            when,
68        }
69    }
70}
71
72/// Error indicating that a log id is not consecutive to the last know one.
73#[derive(Debug, Clone, PartialEq, Eq)]
74#[derive(thiserror::Error)]
75#[error("Log id is not consecutive when append: last {last:?}, attempted {attempted:?}")]
76pub struct LogIdNonConsecutive<T: Types> {
77    pub last: Option<T::LogId>,
78    pub attempted: T::LogId,
79}
80
81impl<T: Types> LogIdNonConsecutive<T> {
82    pub fn new(last: Option<T::LogId>, attempted: T::LogId) -> Self {
83        Self { last, attempted }
84    }
85}
86
87/// Error indicating that a log index is not found.
88#[derive(Debug, Clone, PartialEq, Eq)]
89#[derive(thiserror::Error)]
90#[error("Log not found at index {index:?}")]
91pub struct LogIndexNotFound {
92    pub index: u64,
93}
94
95impl LogIndexNotFound {
96    pub fn new(index: u64) -> Self {
97        Self { index }
98    }
99}