kinetic_core/error/
network.rs1use super::Severity;
18use thiserror::Error;
19
20#[derive(Error, Debug, PartialEq, Eq)]
22pub enum NetworkClientError {
23 #[error("Request timed out")]
25 Timeout,
26 #[error("Node is offline or unreachable")]
28 Offline,
29 #[error("Routing table is empty")]
31 RoutingTableEmpty,
32 #[error("Internal channel closed")]
34 ChannelClosed,
35 #[error("Stream dropped by peer")]
37 StreamDropped,
38 #[error("Unsupported protocol")]
40 UnsupportedProtocol,
41 #[error("Gossipsub error: {0}")]
43 GossipSubError(String),
44 #[error("Kademlia store error: {0}")]
46 StoreError(String),
47 #[error("Other network error: {0}")]
49 Other(String),
50}
51
52impl NetworkClientError {
53 pub fn code(&self) -> &'static str {
55 match self {
56 Self::Timeout => "KIN-NET-001",
57 Self::Offline => "KIN-NET-002",
58 Self::RoutingTableEmpty => "KIN-NET-003",
59 Self::ChannelClosed => "KIN-NET-004",
60 Self::StreamDropped => "KIN-NET-005",
61 Self::UnsupportedProtocol => "KIN-NET-006",
62 Self::GossipSubError(_) => "KIN-NET-007",
63 Self::StoreError(_) => "KIN-NET-008",
64 Self::Other(_) => "KIN-NET-009",
65 }
66 }
67
68 pub fn error_type_uri(&self) -> String {
70 format!("{}/errors/{}", crate::constants::DOCS_URL, self.code())
71 }
72
73 pub fn severity(&self) -> Severity {
75 match self {
76 Self::Timeout
77 | Self::Offline
78 | Self::RoutingTableEmpty
79 | Self::ChannelClosed
80 | Self::StreamDropped
81 | Self::GossipSubError(_) => Severity::Warning,
82 Self::UnsupportedProtocol | Self::StoreError(_) | Self::Other(_) => Severity::Error,
83 }
84 }
85
86 pub fn is_retryable(&self) -> bool {
88 matches!(
89 self,
90 Self::Timeout | Self::Offline | Self::RoutingTableEmpty | Self::StreamDropped
91 )
92 }
93
94 pub fn user_message(&self) -> String {
96 match self {
97 Self::Timeout => "The network request timed out.".to_string(),
98 Self::Offline => "The node is offline or unreachable.".to_string(),
99 Self::RoutingTableEmpty => "The Kademlia routing table is empty.".to_string(),
100 Self::ChannelClosed => "Internal channel closed.".to_string(),
101 Self::StreamDropped => "Stream dropped by peer.".to_string(),
102 Self::UnsupportedProtocol => "Unsupported protocol.".to_string(),
103 Self::GossipSubError(_) => "A GossipSub operation failed.".to_string(),
104 Self::StoreError(_) => "Kademlia store error.".to_string(),
105 Self::Other(_) => "A network error occurred.".to_string(),
106 }
107 }
108}