stratum_node_core/error_code.rs
1//! The error codes Step 3a adds to the JSON error shape, with their HTTP
2//! statuses.
3
4use std::fmt;
5
6/// An error code and the HTTP status it is sent with.
7///
8/// Every Stratum error response is `{"error":{"code","message"}}`. The codes
9/// defined before Step 3a are written by the crate that sends them. This
10/// type gives the codes Step 3a adds one definition, shared by the
11/// controller and the gate that send them and the client that reads them.
12///
13/// # Examples
14///
15/// ```
16/// use stratum_node_core::ErrorCode;
17///
18/// assert_eq!(ErrorCode::SUSPENDED.as_str(), "suspended");
19/// assert_eq!(ErrorCode::SUSPENDED.status(), 403);
20/// assert_eq!(ErrorCode::from_code("archive_required"), Some(ErrorCode::ARCHIVE_REQUIRED));
21/// assert_eq!(ErrorCode::from_code("conflict"), None);
22/// ```
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
24pub struct ErrorCode {
25 code: &'static str,
26 status: u16,
27}
28
29impl ErrorCode {
30 /// `suspended`, 403: the requester, or the owner of the pod in the path,
31 /// is suspended. Also the answer to `redeem` and `confirm` by a
32 /// suspended key, decided before any stored response is looked up.
33 ///
34 /// # Examples
35 ///
36 /// ```
37 /// use stratum_node_core::ErrorCode;
38 ///
39 /// assert_eq!(ErrorCode::SUSPENDED.to_string(), "suspended");
40 /// ```
41 pub const SUSPENDED: Self = Self {
42 code: "suspended",
43 status: 403,
44 };
45
46 /// `archive_required`, 409: `remove` requires an archive, and none was
47 /// taken for the member's current generation and suspension number.
48 ///
49 /// # Examples
50 ///
51 /// ```
52 /// use stratum_node_core::ErrorCode;
53 ///
54 /// assert_eq!(ErrorCode::ARCHIVE_REQUIRED.status(), 409);
55 /// ```
56 pub const ARCHIVE_REQUIRED: Self = Self {
57 code: "archive_required",
58 status: 409,
59 };
60
61 /// `pod_data_present`, 409: an invitation or redemption for a `removed`
62 /// or `cancelled` key whose old pod data has not been purged.
63 ///
64 /// # Examples
65 ///
66 /// ```
67 /// use stratum_node_core::ErrorCode;
68 ///
69 /// assert_eq!(ErrorCode::POD_DATA_PRESENT.as_str(), "pod_data_present");
70 /// ```
71 pub const POD_DATA_PRESENT: Self = Self {
72 code: "pod_data_present",
73 status: 409,
74 };
75
76 /// `app_forbidden`, 403: an application key used on a route, listener or
77 /// pod it may not reach, or a member key sent to the gate's proxy
78 /// listener.
79 pub const APP_FORBIDDEN: Self = Self {
80 code: "app_forbidden",
81 status: 403,
82 };
83
84 /// `rate_limited`, 429: a principal, all application keys together, or
85 /// the whole node exceeded its signed-pod-request rate limit.
86 pub const RATE_LIMITED: Self = Self {
87 code: "rate_limited",
88 status: 429,
89 };
90
91 /// Every code this type defines.
92 ///
93 /// # Examples
94 ///
95 /// ```
96 /// use stratum_node_core::ErrorCode;
97 ///
98 /// assert!(ErrorCode::ALL.iter().all(|c| ErrorCode::from_code(c.as_str()) == Some(*c)));
99 /// ```
100 pub const ALL: [Self; 5] = [
101 Self::SUSPENDED,
102 Self::ARCHIVE_REQUIRED,
103 Self::POD_DATA_PRESENT,
104 Self::APP_FORBIDDEN,
105 Self::RATE_LIMITED,
106 ];
107
108 /// Returns the code as it appears in `error.code`.
109 ///
110 /// # Examples
111 ///
112 /// ```
113 /// use stratum_node_core::ErrorCode;
114 ///
115 /// assert_eq!(ErrorCode::POD_DATA_PRESENT.as_str(), "pod_data_present");
116 /// ```
117 pub const fn as_str(self) -> &'static str {
118 self.code
119 }
120
121 /// Returns the HTTP status the code is sent with.
122 ///
123 /// # Examples
124 ///
125 /// ```
126 /// use stratum_node_core::ErrorCode;
127 ///
128 /// assert_eq!(ErrorCode::ARCHIVE_REQUIRED.status(), 409);
129 /// ```
130 pub const fn status(self) -> u16 {
131 self.status
132 }
133
134 /// Returns the code named `code`, if this type defines it. The match is
135 /// exact and case-sensitive.
136 ///
137 /// # Examples
138 ///
139 /// ```
140 /// use stratum_node_core::ErrorCode;
141 ///
142 /// assert_eq!(ErrorCode::from_code("suspended"), Some(ErrorCode::SUSPENDED));
143 /// assert_eq!(ErrorCode::from_code("Suspended"), None);
144 /// ```
145 pub fn from_code(code: &str) -> Option<Self> {
146 Self::ALL.into_iter().find(|known| known.code == code)
147 }
148}
149
150impl fmt::Display for ErrorCode {
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 f.write_str(self.code)
153 }
154}