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
use thiserror::Error as ThisError;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, ThisError)]
pub enum Error {
    #[error("raft error: `{0}`")]
    RaftError(#[from] tikv_raft::Error),
    #[error("Error joining the cluster")]
    JoinError,
    #[error("gprc error: `{0}`")]
    Grpc(#[from] tonic::transport::Error),
    #[error("error calling remote procedure: `{0}`")]
    RemoteCall(#[from] tonic::Status),
    #[error("io error: {0}")]
    Io(String),
    #[error("unexpected error, {0}")]
    Other(#[source] Box<dyn std::error::Error + Sync + Send + 'static>),
    #[error("unexpected error")]
    Unknown,
    #[error("leader does not exist")]
    LeaderNotExist,
    #[error("Not a Leader")]
    NotLeader,
    #[error("timeout")]
    Elapsed,
    #[error("{0}")]
    Msg(String),
    #[error("send error, {0}")]
    SendError(String),
    #[error("recv error, {0}")]
    RecvError(String),
    #[error("{0}")]
    Anyhow(anyhow::Error),
}

impl Error {
    pub fn boxed(self) -> Box<Self> {
        Box::new(self)
    }
}

impl From<prost::DecodeError> for Error {
    fn from(e: prost::DecodeError) -> Self {
        Self::Other(Box::new(e))
    }
}

impl From<prost::EncodeError> for Error {
    fn from(e: prost::EncodeError) -> Self {
        Self::Other(Box::new(e))
    }
}

impl From<tokio::io::Error> for Error {
    fn from(e: tokio::io::Error) -> Self {
        Self::Io(e.to_string())
    }
}

impl From<bincode::Error> for Error {
    fn from(e: bincode::Error) -> Self {
        Self::Other(e)
    }
}

impl From<std::string::FromUtf8Error> for Error {
    fn from(e: std::string::FromUtf8Error) -> Self {
        Self::Other(Box::new(e))
    }
}

impl From<String> for Error {
    fn from(e: String) -> Self {
        Self::Msg(e)
    }
}

impl From<&str> for Error {
    fn from(e: &str) -> Self {
        Self::Msg(e.to_owned())
    }
}

impl From<anyhow::Error> for Error {
    #[inline]
    fn from(e: anyhow::Error) -> Self {
        Error::Anyhow(e)
    }
}
impl From<tokio::time::error::Elapsed> for Error {
    #[inline]
    fn from(_: tokio::time::error::Elapsed) -> Self {
        Error::Elapsed
    }
}