Skip to main content

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(
52    "Log id cannot be reversed when {when}: current {current:?}, attempted {attempted:?}"
53)]
54pub struct LogIdReversal<T: Types> {
55    pub current: T::LogId,
56    pub attempted: T::LogId,
57    pub when: &'static str,
58}
59
60impl<T: Types> LogIdReversal<T> {
61    pub fn new(
62        current: T::LogId,
63        attempted: T::LogId,
64        when: &'static str,
65    ) -> Self {
66        Self {
67            current,
68            attempted,
69            when,
70        }
71    }
72}
73
74/// Error indicating that a log id is not consecutive to the last know one.
75#[derive(Debug, Clone, PartialEq, Eq)]
76#[derive(thiserror::Error)]
77#[error(
78    "Log id is not consecutive when append: last {last:?}, attempted {attempted:?}"
79)]
80pub struct LogIdNonConsecutive<T: Types> {
81    pub last: Option<T::LogId>,
82    pub attempted: T::LogId,
83}
84
85impl<T: Types> LogIdNonConsecutive<T> {
86    pub fn new(last: Option<T::LogId>, attempted: T::LogId) -> Self {
87        Self { last, attempted }
88    }
89}
90
91/// Error indicating that a log index is not found.
92#[derive(Debug, Clone, PartialEq, Eq)]
93#[derive(thiserror::Error)]
94#[error("Log not found at index {index:?}")]
95pub struct LogIndexNotFound {
96    pub index: u64,
97}
98
99impl LogIndexNotFound {
100    pub fn new(index: u64) -> Self {
101        Self { index }
102    }
103}