openraft/error/
streaming_error.rs

1use std::error::Error;
2
3use crate::error::Fatal;
4use crate::error::Infallible;
5use crate::error::NetworkError;
6use crate::error::RPCError;
7use crate::error::RemoteError;
8use crate::error::ReplicationClosed;
9use crate::error::ReplicationError;
10use crate::error::Timeout;
11use crate::error::Unreachable;
12use crate::RaftTypeConfig;
13use crate::StorageError;
14
15/// Error occurred when streaming local data to a remote raft node.
16///
17/// Thus this error includes storage error, network error, and remote error.
18#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
19#[cfg_attr(
20    feature = "serde",
21    derive(serde::Serialize, serde::Deserialize),
22    serde(bound(serialize = "E: serde::Serialize")),
23    serde(bound(deserialize = "E: for <'d> serde::Deserialize<'d>"))
24)]
25pub enum StreamingError<C: RaftTypeConfig, E: Error = Infallible> {
26    /// The replication stream is closed intentionally.
27    #[error(transparent)]
28    Closed(#[from] ReplicationClosed),
29
30    /// Storage error occurs when reading local data.
31    #[error(transparent)]
32    StorageError(#[from] StorageError<C::NodeId>),
33
34    /// Timeout when streaming data to remote node.
35    #[error(transparent)]
36    Timeout(#[from] Timeout<C::NodeId>),
37
38    /// The node is temporarily unreachable and should backoff before retrying.
39    #[error(transparent)]
40    Unreachable(#[from] Unreachable),
41
42    /// Failed to send the RPC request and should retry immediately.
43    #[error(transparent)]
44    Network(#[from] NetworkError),
45
46    /// Remote node returns an error.
47    #[error(transparent)]
48    RemoteError(#[from] RemoteError<C::NodeId, C::Node, E>),
49}
50
51impl<C: RaftTypeConfig> From<StreamingError<C, Fatal<C::NodeId>>> for ReplicationError<C::NodeId, C::Node> {
52    fn from(e: StreamingError<C, Fatal<C::NodeId>>) -> Self {
53        match e {
54            StreamingError::Closed(e) => ReplicationError::Closed(e),
55            StreamingError::StorageError(e) => ReplicationError::StorageError(e),
56            StreamingError::Timeout(e) => ReplicationError::RPCError(RPCError::Timeout(e)),
57            StreamingError::Unreachable(e) => ReplicationError::RPCError(RPCError::Unreachable(e)),
58            StreamingError::Network(e) => ReplicationError::RPCError(RPCError::Network(e)),
59            StreamingError::RemoteError(e) => {
60                // Fatal on remote error is considered as unreachable.
61                ReplicationError::RPCError(RPCError::Unreachable(Unreachable::new(&e.source)))
62            }
63        }
64    }
65}
66
67impl<C: RaftTypeConfig> From<StreamingError<C>> for ReplicationError<C::NodeId, C::Node> {
68    fn from(e: StreamingError<C>) -> Self {
69        #[allow(unreachable_patterns)]
70        match e {
71            StreamingError::Closed(e) => ReplicationError::Closed(e),
72            StreamingError::StorageError(e) => ReplicationError::StorageError(e),
73            StreamingError::Timeout(e) => ReplicationError::RPCError(RPCError::Timeout(e)),
74            StreamingError::Unreachable(e) => ReplicationError::RPCError(RPCError::Unreachable(e)),
75            StreamingError::Network(e) => ReplicationError::RPCError(RPCError::Network(e)),
76            StreamingError::RemoteError(_e) => {
77                unreachable!("Infallible error should not be converted to ReplicationError")
78            }
79        }
80    }
81}