stratum_node_core/error_code.rs
1//! The error codes Steps 3a and 3b add to the JSON error shape, with their
2//! HTTP 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 Steps 3a and 3b add one definition, shared by the
11/// controller, the gate, the proxy, the signer and the runner that send
12/// them and the client and the job library that read them. Signer-internal
13/// codes (`stale_revision`, `no_recipients`) and outcomes that are never an
14/// HTTP answer (`limits_not_applied`, `keys_inconsistent`) are not here.
15///
16/// # Examples
17///
18/// ```
19/// use stratum_node_core::ErrorCode;
20///
21/// assert_eq!(ErrorCode::SUSPENDED.as_str(), "suspended");
22/// assert_eq!(ErrorCode::SUSPENDED.status(), 403);
23/// assert_eq!(ErrorCode::from_code("archive_required"), Some(ErrorCode::ARCHIVE_REQUIRED));
24/// assert_eq!(ErrorCode::from_code("conflict"), None);
25/// ```
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
27pub struct ErrorCode {
28 code: &'static str,
29 status: u16,
30}
31
32impl ErrorCode {
33 /// `suspended`, 403: the requester, or the owner of the pod in the path,
34 /// is suspended. Also the answer to `redeem` and `confirm` by a
35 /// suspended key, decided before any stored response is looked up.
36 ///
37 /// # Examples
38 ///
39 /// ```
40 /// use stratum_node_core::ErrorCode;
41 ///
42 /// assert_eq!(ErrorCode::SUSPENDED.to_string(), "suspended");
43 /// ```
44 pub const SUSPENDED: Self = Self {
45 code: "suspended",
46 status: 403,
47 };
48
49 /// `archive_required`, 409: `remove` requires an archive, and none was
50 /// taken for the member's current generation and suspension number.
51 ///
52 /// # Examples
53 ///
54 /// ```
55 /// use stratum_node_core::ErrorCode;
56 ///
57 /// assert_eq!(ErrorCode::ARCHIVE_REQUIRED.status(), 409);
58 /// ```
59 pub const ARCHIVE_REQUIRED: Self = Self {
60 code: "archive_required",
61 status: 409,
62 };
63
64 /// `pod_data_present`, 409: an invitation or redemption for a `removed`
65 /// or `cancelled` key whose old pod data has not been purged.
66 ///
67 /// # Examples
68 ///
69 /// ```
70 /// use stratum_node_core::ErrorCode;
71 ///
72 /// assert_eq!(ErrorCode::POD_DATA_PRESENT.as_str(), "pod_data_present");
73 /// ```
74 pub const POD_DATA_PRESENT: Self = Self {
75 code: "pod_data_present",
76 status: 409,
77 };
78
79 /// `app_forbidden`, 403: an application key used on a route, listener or
80 /// pod it may not reach, or a member key sent to the gate's proxy
81 /// listener.
82 pub const APP_FORBIDDEN: Self = Self {
83 code: "app_forbidden",
84 status: 403,
85 };
86
87 /// `rate_limited`, 429: a principal, all application keys together, or
88 /// the whole node exceeded its signed-pod-request rate limit.
89 pub const RATE_LIMITED: Self = Self {
90 code: "rate_limited",
91 status: 429,
92 };
93
94 /// `lease_revoked`, 403: the lease was revoked by withdrawal, suspension, application disable or removal. A job exits at once.
95 pub const LEASE_REVOKED: Self = Self {
96 code: "lease_revoked",
97 status: 403,
98 };
99
100 /// `outside_grant`, 403: the request's method or path is outside the installation's grant, or the path is an ACL.
101 pub const OUTSIDE_GRANT: Self = Self {
102 code: "outside_grant",
103 status: 403,
104 };
105
106 /// `lease_starting`, 503: the lease is claimed but not yet started: the runner has not verified the container's limits or the signing window is not open. A job retries.
107 pub const LEASE_STARTING: Self = Self {
108 code: "lease_starting",
109 status: 503,
110 };
111
112 /// `lease_expired`, 503: the proxy holds no live lease view: the controller has not answered its poll within the 300 s view lease. A job retries until its cut-off.
113 pub const LEASE_EXPIRED: Self = Self {
114 code: "lease_expired",
115 status: 503,
116 };
117
118 /// `busy`, 503: the proxy (or a signer socket) is at its concurrency limit. A job retries.
119 pub const BUSY: Self = Self {
120 code: "busy",
121 status: 503,
122 };
123
124 /// `pod_auth_refused`, 503: the pod server answered 401, which the proxy relays as retryable: its replay cache is full. A job retries.
125 pub const POD_AUTH_REFUSED: Self = Self {
126 code: "pod_auth_refused",
127 status: 503,
128 };
129
130 /// `key_disabled`, 403: the signer refuses the key: it is disabled, destroyed, unknown or the node key.
131 pub const KEY_DISABLED: Self = Self {
132 code: "key_disabled",
133 status: 403,
134 };
135
136 /// `no_window`, 403: the signer has no signing window open for this key and lease.
137 pub const NO_WINDOW: Self = Self {
138 code: "no_window",
139 status: 403,
140 };
141
142 /// `outside_pod`, 403: the signer refuses a URL that is not the key's origin followed by a non-ACL path in its owner's pod.
143 pub const OUTSIDE_POD: Self = Self {
144 code: "outside_pod",
145 status: 403,
146 };
147
148 /// `payload_rule`, 400: the signer refuses a `payload_sha256` that is malformed, present without a body or absent with one.
149 pub const PAYLOAD_RULE: Self = Self {
150 code: "payload_rule",
151 status: 400,
152 };
153
154 /// `application_unavailable`, 409: an installation names an application that is not `approved`.
155 pub const APPLICATION_UNAVAILABLE: Self = Self {
156 code: "application_unavailable",
157 status: 409,
158 };
159
160 /// `image_has_volumes`, 409: the image's configuration declares a volume, refused at approval and again at create.
161 pub const IMAGE_HAS_VOLUMES: Self = Self {
162 code: "image_has_volumes",
163 status: 409,
164 };
165
166 /// `image_absent`, 409: the runner reports the image ID absent from the tenant daemon.
167 pub const IMAGE_ABSENT: Self = Self {
168 code: "image_absent",
169 status: 409,
170 };
171
172 /// `workspace_unavailable`, 409: an installation asks for a workspace and no slot is available; always the answer to a workspace mode other than `none` in Step 3b-2.
173 pub const WORKSPACE_UNAVAILABLE: Self = Self {
174 code: "workspace_unavailable",
175 status: 409,
176 };
177
178 /// `container_identity_conflict`, 409: the runner found a container with the lease's name whose labels or effective profile differ from the request; it neither starts, stops nor removes it.
179 pub const CONTAINER_IDENTITY_CONFLICT: Self = Self {
180 code: "container_identity_conflict",
181 status: 409,
182 };
183
184 /// `create_expired`, 409: the runner received a create after the controller's monotonic `not_after` and did not begin it.
185 pub const CREATE_EXPIRED: Self = Self {
186 code: "create_expired",
187 status: 409,
188 };
189
190 /// `runner_unavailable`, 503: the controller could not complete a step
191 /// that needs the runner (an application's approval queries the image
192 /// on the tenant daemon): no runner is configured, the runner did not
193 /// answer, or it refused. Nothing was stored; the request can be sent
194 /// again once the runner answers.
195 ///
196 /// # Examples
197 ///
198 /// ```
199 /// use stratum_node_core::ErrorCode;
200 ///
201 /// assert_eq!(ErrorCode::RUNNER_UNAVAILABLE.as_str(), "runner_unavailable");
202 /// assert_eq!(ErrorCode::RUNNER_UNAVAILABLE.status(), 503);
203 /// ```
204 pub const RUNNER_UNAVAILABLE: Self = Self {
205 code: "runner_unavailable",
206 status: 503,
207 };
208
209 /// `signer_unavailable`, 503: an installation's create or consent was
210 /// committed but the signer did not finish its step (making the
211 /// application key, or confirming it enabled). The commit stays;
212 /// sending the same request again, with the same operation ID, finishes
213 /// it and answers once the signer has.
214 ///
215 /// # Examples
216 ///
217 /// ```
218 /// use stratum_node_core::ErrorCode;
219 ///
220 /// assert_eq!(ErrorCode::SIGNER_UNAVAILABLE.as_str(), "signer_unavailable");
221 /// assert_eq!(ErrorCode::SIGNER_UNAVAILABLE.status(), 503);
222 /// ```
223 pub const SIGNER_UNAVAILABLE: Self = Self {
224 code: "signer_unavailable",
225 status: 503,
226 };
227
228 /// Every code this type defines.
229 ///
230 /// # Examples
231 ///
232 /// ```
233 /// use stratum_node_core::ErrorCode;
234 ///
235 /// assert!(ErrorCode::ALL.iter().all(|c| ErrorCode::from_code(c.as_str()) == Some(*c)));
236 /// ```
237 pub const ALL: [Self; 23] = [
238 Self::SUSPENDED,
239 Self::ARCHIVE_REQUIRED,
240 Self::POD_DATA_PRESENT,
241 Self::APP_FORBIDDEN,
242 Self::RATE_LIMITED,
243 Self::LEASE_REVOKED,
244 Self::OUTSIDE_GRANT,
245 Self::LEASE_STARTING,
246 Self::LEASE_EXPIRED,
247 Self::BUSY,
248 Self::POD_AUTH_REFUSED,
249 Self::KEY_DISABLED,
250 Self::NO_WINDOW,
251 Self::OUTSIDE_POD,
252 Self::PAYLOAD_RULE,
253 Self::APPLICATION_UNAVAILABLE,
254 Self::IMAGE_HAS_VOLUMES,
255 Self::IMAGE_ABSENT,
256 Self::WORKSPACE_UNAVAILABLE,
257 Self::CONTAINER_IDENTITY_CONFLICT,
258 Self::CREATE_EXPIRED,
259 Self::RUNNER_UNAVAILABLE,
260 Self::SIGNER_UNAVAILABLE,
261 ];
262
263 /// Returns the code as it appears in `error.code`.
264 ///
265 /// # Examples
266 ///
267 /// ```
268 /// use stratum_node_core::ErrorCode;
269 ///
270 /// assert_eq!(ErrorCode::POD_DATA_PRESENT.as_str(), "pod_data_present");
271 /// ```
272 pub const fn as_str(self) -> &'static str {
273 self.code
274 }
275
276 /// Returns the HTTP status the code is sent with.
277 ///
278 /// # Examples
279 ///
280 /// ```
281 /// use stratum_node_core::ErrorCode;
282 ///
283 /// assert_eq!(ErrorCode::ARCHIVE_REQUIRED.status(), 409);
284 /// ```
285 pub const fn status(self) -> u16 {
286 self.status
287 }
288
289 /// Returns the code named `code`, if this type defines it. The match is
290 /// exact and case-sensitive.
291 ///
292 /// # Examples
293 ///
294 /// ```
295 /// use stratum_node_core::ErrorCode;
296 ///
297 /// assert_eq!(ErrorCode::from_code("suspended"), Some(ErrorCode::SUSPENDED));
298 /// assert_eq!(ErrorCode::from_code("Suspended"), None);
299 /// ```
300 pub fn from_code(code: &str) -> Option<Self> {
301 Self::ALL.into_iter().find(|known| known.code == code)
302 }
303}
304
305impl fmt::Display for ErrorCode {
306 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
307 f.write_str(self.code)
308 }
309}