1use thiserror::Error as ThisError;
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5#[allow(clippy::result_large_err)]
6#[derive(Debug, ThisError)]
7pub enum Error {
8 #[error("raft error: `{0}`")]
9 RaftError(#[from] tikv_raft::Error),
10 #[error("Error joining the cluster")]
11 JoinError,
12 #[error("gprc error: `{0}`")]
13 Grpc(#[from] tonic::transport::Error),
14 #[error("error calling remote procedure: `{0}`")]
15 RemoteCall(#[from] tonic::Status),
16 #[error("io error: {0}")]
17 Io(String),
18 #[error("unexpected error, {0}")]
19 Other(#[source] Box<dyn std::error::Error + Sync + Send + 'static>),
20 #[error("unexpected error")]
21 Unknown,
22 #[error("too busy")]
23 Busy,
24 #[error("leader does not exist")]
25 LeaderNotExist,
26 #[error("Not a Leader")]
27 NotLeader,
28 #[error("timeout")]
29 Elapsed,
30 #[error("{0}")]
31 Msg(String),
32 #[error("send error, {0}")]
33 SendError(String),
34 #[error("recv error, {0}")]
35 RecvError(String),
36 #[error("{0}")]
37 Anyhow(anyhow::Error),
38}
39
40impl Error {
41 pub fn boxed(self) -> Box<Self> {
42 Box::new(self)
43 }
44}
45
46impl From<prost::DecodeError> for Error {
47 fn from(e: prost::DecodeError) -> Self {
48 Self::Other(Box::new(e))
49 }
50}
51
52impl From<prost::EncodeError> for Error {
53 fn from(e: prost::EncodeError) -> Self {
54 Self::Other(Box::new(e))
55 }
56}
57
58impl From<tokio::io::Error> for Error {
59 fn from(e: tokio::io::Error) -> Self {
60 Self::Io(e.to_string())
61 }
62}
63
64impl From<bincode::Error> for Error {
65 fn from(e: bincode::Error) -> Self {
66 Self::Other(e)
67 }
68}
69
70impl From<std::string::FromUtf8Error> for Error {
71 fn from(e: std::string::FromUtf8Error) -> Self {
72 Self::Other(Box::new(e))
73 }
74}
75
76impl From<String> for Error {
77 fn from(e: String) -> Self {
78 Self::Msg(e)
79 }
80}
81
82impl From<&str> for Error {
83 fn from(e: &str) -> Self {
84 Self::Msg(e.to_owned())
85 }
86}
87
88impl From<anyhow::Error> for Error {
89 #[inline]
90 fn from(e: anyhow::Error) -> Self {
91 Error::Anyhow(e)
92 }
93}
94impl From<tokio::time::error::Elapsed> for Error {
95 #[inline]
96 fn from(_: tokio::time::error::Elapsed) -> Self {
97 Error::Elapsed
98 }
99}