raft_log/
errors.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
mod storage_errors;

use std::io;

pub use storage_errors::InvalidChunkFileName;

use crate::api::types::Types;

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(thiserror::Error)]
pub enum RaftLogStateError<T: Types> {
    #[error(transparent)]
    VoteReversal(#[from] VoteReversal<T>),

    #[error(transparent)]
    LogIdReversal(#[from] LogIdReversal<T>),

    #[error(transparent)]
    LogIdNonConsecutive(#[from] LogIdNonConsecutive<T>),

    #[error(transparent)]
    LogIndexNotFound(#[from] LogIndexNotFound),
}

impl<T: Types> From<RaftLogStateError<T>> for io::Error {
    fn from(value: RaftLogStateError<T>) -> Self {
        io::Error::new(io::ErrorKind::InvalidInput, value.to_string())
    }
}

/// Error indicating that a vote cannot be reversed.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(thiserror::Error)]
#[error(
    "Vote cannot be reversed: current {current:?}, attempted {attempted:?}"
)]
pub struct VoteReversal<T: Types> {
    pub current: T::Vote,
    pub attempted: T::Vote,
}

impl<T: Types> VoteReversal<T> {
    pub fn new(current: T::Vote, attempted: T::Vote) -> Self {
        Self { current, attempted }
    }
}

/// Error indicating that a log id cannot be reversed.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(thiserror::Error)]
#[error("Log id cannot be reversed when {when}: current {current:?}, attempted {attempted:?}")]
pub struct LogIdReversal<T: Types> {
    pub current: T::LogId,
    pub attempted: T::LogId,
    pub when: &'static str,
}

impl<T: Types> LogIdReversal<T> {
    pub fn new(
        current: T::LogId,
        attempted: T::LogId,
        when: &'static str,
    ) -> Self {
        Self {
            current,
            attempted,
            when,
        }
    }
}

/// Error indicating that a log id is not consecutive to the last know one.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(thiserror::Error)]
#[error("Log id is not consecutive when append: last {last:?}, attempted {attempted:?}")]
pub struct LogIdNonConsecutive<T: Types> {
    pub last: Option<T::LogId>,
    pub attempted: T::LogId,
}

impl<T: Types> LogIdNonConsecutive<T> {
    pub fn new(last: Option<T::LogId>, attempted: T::LogId) -> Self {
        Self { last, attempted }
    }
}

/// Error indicating that a log index is not found.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(thiserror::Error)]
#[error("Log not found at index {index:?}")]
pub struct LogIndexNotFound {
    pub index: u64,
}

impl LogIndexNotFound {
    pub fn new(index: u64) -> Self {
        Self { index }
    }
}