Skip to main content

raft_log/
errors.rs

1use std::io;
2
3pub use chunked_wal::errors::InvalidChunkFileName;
4
5use crate::api::types::Types;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8#[derive(thiserror::Error)]
9pub enum RaftLogStateError<T: Types> {
10    #[error(transparent)]
11    VoteReversal(#[from] VoteReversal<T>),
12
13    #[error(transparent)]
14    LogIdReversal(#[from] LogIdReversal<T>),
15
16    #[error(transparent)]
17    LogIdNonConsecutive(#[from] LogIdNonConsecutive<T>),
18
19    #[error(transparent)]
20    LogIndexNotFound(#[from] LogIndexNotFound),
21}
22
23impl<T: Types> From<RaftLogStateError<T>> for io::Error {
24    fn from(value: RaftLogStateError<T>) -> Self {
25        io::Error::new(io::ErrorKind::InvalidInput, value.to_string())
26    }
27}
28
29/// Error indicating that a vote cannot be reversed.
30#[derive(Debug, Clone, PartialEq, Eq)]
31#[derive(thiserror::Error)]
32#[error(
33    "Vote cannot be reversed: current {current:?}, attempted {attempted:?}"
34)]
35pub struct VoteReversal<T: Types> {
36    pub current: T::Vote,
37    pub attempted: T::Vote,
38}
39
40impl<T: Types> VoteReversal<T> {
41    pub fn new(current: T::Vote, attempted: T::Vote) -> Self {
42        Self { current, attempted }
43    }
44}
45
46/// Error indicating that a log id cannot be reversed.
47#[derive(Debug, Clone, PartialEq, Eq)]
48#[derive(thiserror::Error)]
49#[error(
50    "Log id cannot be reversed when {when}: current {current:?}, attempted {attempted:?}"
51)]
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(
76    "Log id is not consecutive when append: last {last:?}, attempted {attempted:?}"
77)]
78pub struct LogIdNonConsecutive<T: Types> {
79    pub last: Option<T::LogId>,
80    pub attempted: T::LogId,
81}
82
83impl<T: Types> LogIdNonConsecutive<T> {
84    pub fn new(last: Option<T::LogId>, attempted: T::LogId) -> Self {
85        Self { last, attempted }
86    }
87}
88
89/// Error indicating that a log index is not found.
90#[derive(Debug, Clone, PartialEq, Eq)]
91#[derive(thiserror::Error)]
92#[error("Log not found at index {index:?}")]
93pub struct LogIndexNotFound {
94    pub index: u64,
95}
96
97impl LogIndexNotFound {
98    pub fn new(index: u64) -> Self {
99        Self { index }
100    }
101}