kinetic_core/error/
drand.rs1use super::Severity;
16use thiserror::Error;
17
18#[derive(Error, Debug)]
20pub enum DrandError {
21 #[error("All Drand endpoints failed")]
23 AllEndpointsFailed,
24 #[error("Network error: {0}")]
26 Network(String),
27 #[error("HTTP status error: {0}")]
29 HttpError(u16),
30 #[error("No cached kyn found")]
32 NoCachedKyn,
33 #[error("Serialization error: {0}")]
35 Serde(#[from] serde_json::Error),
36 #[error("Storage error: {0}")]
38 Storage(#[from] crate::error::StorageError),
39 #[error("Reqwest error: {0}")]
41 Reqwest(#[from] reqwest::Error),
42 #[error("Invalid Drand signature")]
44 InvalidSignature,
45 #[error("Stale kyn: expected kyn ~{expected}, but got {got}")]
47 StaleKyn {
48 expected: u64,
50 got: u64,
52 },
53}
54
55impl PartialEq for DrandError {
56 fn eq(&self, other: &Self) -> bool {
57 match (self, other) {
58 (Self::AllEndpointsFailed, Self::AllEndpointsFailed) => true,
59 (Self::Network(a), Self::Network(b)) => a == b,
60 (Self::HttpError(a), Self::HttpError(b)) => a == b,
61 (Self::NoCachedKyn, Self::NoCachedKyn) => true,
62 (Self::Serde(a), Self::Serde(b)) => a.to_string() == b.to_string(),
63 (Self::Storage(a), Self::Storage(b)) => a == b,
64 (Self::Reqwest(a), Self::Reqwest(b)) => a.to_string() == b.to_string(),
65 (Self::InvalidSignature, Self::InvalidSignature) => true,
66 (
67 Self::StaleKyn {
68 expected: e1,
69 got: g1,
70 },
71 Self::StaleKyn {
72 expected: e2,
73 got: g2,
74 },
75 ) => e1 == e2 && g1 == g2,
76 _ => false,
77 }
78 }
79}
80impl Eq for DrandError {}
81
82impl DrandError {
83 pub fn code(&self) -> &'static str {
85 match self {
86 Self::AllEndpointsFailed => "KIN-DRA-001",
87 Self::Network(_) => "KIN-DRA-002",
88 Self::HttpError(_) => "KIN-DRA-003",
89 Self::NoCachedKyn => "KIN-DRA-004",
90 Self::Serde(_) => "KIN-DRA-005",
91 Self::Storage(_) => "KIN-DRA-006",
92 Self::Reqwest(_) => "KIN-DRA-007",
93 Self::InvalidSignature => "KIN-DRA-008",
94 Self::StaleKyn { .. } => "KIN-DRA-009",
95 }
96 }
97
98 pub fn error_type_uri(&self) -> String {
100 format!("{}/errors/{}", crate::constants::DOCS_URL, self.code())
101 }
102
103 pub fn severity(&self) -> Severity {
105 match self {
106 Self::AllEndpointsFailed
107 | Self::Network(_)
108 | Self::HttpError(_)
109 | Self::NoCachedKyn
110 | Self::Reqwest(_)
111 | Self::StaleKyn { .. } => Severity::Warning,
112 Self::Serde(_) | Self::Storage(_) | Self::InvalidSignature => Severity::Error,
113 }
114 }
115
116 pub fn is_retryable(&self) -> bool {
118 matches!(
119 self,
120 Self::AllEndpointsFailed
121 | Self::Network(_)
122 | Self::HttpError(_)
123 | Self::Reqwest(_)
124 | Self::StaleKyn { .. }
125 )
126 }
127
128 pub fn user_message(&self) -> String {
130 match self {
131 Self::AllEndpointsFailed => "All network endpoints failed.".to_string(),
132 Self::Network(_) | Self::HttpError(_) | Self::Reqwest(_) => {
133 "A network error occurred while fetching the network kyn.".to_string()
134 }
135 Self::NoCachedKyn => "No cached network kyn found.".to_string(),
136 Self::Serde(_) => "Failed to parse the network kyn.".to_string(),
137 Self::Storage(_) => {
138 "A storage error occurred while reading or writing the kyn cache.".to_string()
139 }
140 Self::InvalidSignature => "Invalid network signature.".to_string(),
141 Self::StaleKyn { .. } => "The fetched network kyn was too old.".to_string(),
142 }
143 }
144}