1use std::net::SocketAddr;
4
5use crate::NodeId;
6
7#[derive(Debug, thiserror::Error)]
9pub enum ClusterError {
10 #[error("slot {0} is not assigned to any node")]
12 SlotNotAssigned(u16),
13
14 #[error("MOVED {slot} {addr}")]
16 Moved { slot: u16, addr: SocketAddr },
17
18 #[error("ASK {slot} {addr}")]
20 Ask { slot: u16, addr: SocketAddr },
21
22 #[error("node {0} not found in cluster")]
24 NodeNotFound(NodeId),
25
26 #[error("cluster is down")]
28 ClusterDown,
29
30 #[error("operation not supported on {role} node")]
32 WrongRole { role: String },
33
34 #[error("cross-slot keys not allowed (keys span slots {0} and {1})")]
36 CrossSlot(u16, u16),
37
38 #[error("cluster communication error: {0}")]
40 Network(String),
41
42 #[error("cluster operation timed out")]
44 Timeout,
45
46 #[error("invalid cluster configuration: {0}")]
48 Configuration(String),
49}
50
51impl ClusterError {
52 pub fn is_redirect(&self) -> bool {
54 matches!(self, ClusterError::Moved { .. } | ClusterError::Ask { .. })
55 }
56
57 pub fn moved(slot: u16, addr: SocketAddr) -> Self {
59 ClusterError::Moved { slot, addr }
60 }
61
62 pub fn ask(slot: u16, addr: SocketAddr) -> Self {
64 ClusterError::Ask { slot, addr }
65 }
66}