use super::Severity;
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Eq)]
pub enum NetworkClientError {
#[error("Request timed out")]
Timeout,
#[error("Node is offline or unreachable")]
Offline,
#[error("Routing table is empty")]
RoutingTableEmpty,
#[error("Internal channel closed")]
ChannelClosed,
#[error("Stream dropped by peer")]
StreamDropped,
#[error("Unsupported protocol")]
UnsupportedProtocol,
#[error("Gossipsub error: {0}")]
GossipSubError(String),
#[error("Kademlia store error: {0}")]
StoreError(String),
#[error("Other network error: {0}")]
Other(String),
}
impl NetworkClientError {
pub fn code(&self) -> &'static str {
match self {
Self::Timeout => "KIN-NET-001",
Self::Offline => "KIN-NET-002",
Self::RoutingTableEmpty => "KIN-NET-003",
Self::ChannelClosed => "KIN-NET-004",
Self::StreamDropped => "KIN-NET-005",
Self::UnsupportedProtocol => "KIN-NET-006",
Self::GossipSubError(_) => "KIN-NET-007",
Self::StoreError(_) => "KIN-NET-008",
Self::Other(_) => "KIN-NET-009",
}
}
pub fn error_type_uri(&self) -> String {
format!("{}/errors/{}", crate::constants::DOCS_URL, self.code())
}
pub fn severity(&self) -> Severity {
match self {
Self::Timeout
| Self::Offline
| Self::RoutingTableEmpty
| Self::ChannelClosed
| Self::StreamDropped
| Self::GossipSubError(_) => Severity::Warning,
Self::UnsupportedProtocol | Self::StoreError(_) | Self::Other(_) => Severity::Error,
}
}
pub fn is_retryable(&self) -> bool {
matches!(
self,
Self::Timeout | Self::Offline | Self::RoutingTableEmpty | Self::StreamDropped
)
}
pub fn user_message(&self) -> String {
match self {
Self::Timeout => "The network request timed out.".to_string(),
Self::Offline => "The node is offline or unreachable.".to_string(),
Self::RoutingTableEmpty => "The Kademlia routing table is empty.".to_string(),
Self::ChannelClosed => "Internal channel closed.".to_string(),
Self::StreamDropped => "Stream dropped by peer.".to_string(),
Self::UnsupportedProtocol => "Unsupported protocol.".to_string(),
Self::GossipSubError(_) => "A GossipSub operation failed.".to_string(),
Self::StoreError(_) => "Kademlia store error.".to_string(),
Self::Other(_) => "A network error occurred.".to_string(),
}
}
}