Skip to main content

stratum_node_cli/
exit.rs

1//! The process exit codes of `stratum`, stable across releases.
2//!
3//! | Code | Meaning |
4//! |---|---|
5//! | 0 | Success; for `status`, the node is `ready` |
6//! | 1 | Local failure: a key, token or resume file, the clock, or writing output |
7//! | 2 | The node could not be reached |
8//! | 3 | `status`: the node is `unconfigured` |
9//! | 4 | `status`: the node is `degraded` |
10//! | 5 | The node sent a response that does not follow the API contract |
11//! | 6 | Changes were previewed but not applied: no confirmation |
12//! | 7 | A local check refused the node's answer (claim body, manifest signature, admission notice, probe), or a profile card that does not name its owner |
13//! | 8 | A bounded wait ran out: `join` (the pod was not provisioned within `--wait-secs`) or `member suspend --wait` (the suspension was not yet enforced) |
14//! | 9 | `export`: the archive was written, but an entry changed, vanished or was skipped during the walk |
15//! | 10 | Server error `bad_request` |
16//! | 11 | Server error `unauthorized` |
17//! | 12 | Server error `forbidden` |
18//! | 13 | Server error `not_found` |
19//! | 14 | Server error `conflict` |
20//! | 15 | Server error `payload_too_large` |
21//! | 16 | Server error `unsupported_media_type` |
22//! | 17 | Server error `invalid`, or a preview that reports validation errors |
23//! | 18 | Server error `too_many_challenges` |
24//! | 19 | Server error `backup_refused` |
25//! | 20 | Server error `internal` |
26//! | 21 | A server error code this client does not know |
27//! | 22 | Server error `request_timeout` |
28//! | 23 | Server error `capacity_exhausted` |
29//! | 24 | Server error `invitation_refused` |
30//! | 25 | Server error `invitations_disabled` |
31//! | 26 | Server error `gate_unavailable` |
32//! | 27 | Server error `too_many_requests` |
33//! | 28 | Server error `misdirected_request` |
34//! | 29 | Server error `method_not_allowed` |
35//! | 30 | Server error `insufficient_storage` |
36//! | 31 | `precondition_failed`: a conditional pod write found another version |
37//! | 32 | Server error `bad_gateway` (the gate got no valid answer upstream) |
38//! | 33 | Server error `gateway_timeout` (the gate's upstream did not answer in time) |
39//! | 34 | Server error `suspended` (the administrator suspended the membership the request depends on) |
40//! | 35 | Server error `archive_required` (`member remove` needs an archive from the current suspension) |
41//! | 36 | Server error `pod_data_present` (the key's earlier pod data has not been purged) |
42//! | 38 | Server error `pod_unavailable` (the pod server cannot be reached, or the node has no pods) |
43//! | 64 | Invalid command line |
44//!
45//! Code 37 is unassigned. An earlier pre-release gave it to `lease_expired`,
46//! which is only the gate's log name for the decision; the gate answers an
47//! expired lease with `gate_unavailable` (26).
48
49/// Success. For `status`, the node is `ready`.
50pub const OK: u8 = 0;
51/// A local failure: a key, token or resume file, the clock, or output.
52pub const FAILURE: u8 = 1;
53/// The node could not be reached.
54pub const UNREACHABLE: u8 = 2;
55/// `status`: the node is `unconfigured`.
56pub const UNCONFIGURED: u8 = 3;
57/// `status`: the node is `degraded`.
58pub const DEGRADED: u8 = 4;
59/// The node's response does not follow the API contract.
60pub const PROTOCOL: u8 = 5;
61/// Changes were previewed but not applied because nothing confirmed them.
62pub const NOT_CONFIRMED: u8 = 6;
63/// A local check refused the node's answer.
64pub const REFUSED: u8 = 7;
65/// A bounded wait ran out. For `join`, the pod was not provisioned within
66/// the wait; rerun `join` to continue. For `member suspend --wait`, the
67/// suspension is still `enforcing`; `member show` reports when it is
68/// `enforced`.
69pub const TIMED_OUT: u8 = 8;
70/// `export`: the archive and manifest were written, but at least one entry
71/// changed, vanished or was skipped during the walk; the manifest lists
72/// them.
73pub const EXPORT_INCOMPLETE: u8 = 9;
74/// Server error `bad_request` (HTTP 400).
75pub const BAD_REQUEST: u8 = 10;
76/// Server error `unauthorized` (HTTP 401).
77pub const UNAUTHORIZED: u8 = 11;
78/// Server error `forbidden` (HTTP 403).
79pub const FORBIDDEN: u8 = 12;
80/// Server error `not_found` (HTTP 404).
81pub const NOT_FOUND: u8 = 13;
82/// Server error `conflict` (HTTP 409): stale revision, already bound or
83/// operation conflict.
84pub const CONFLICT: u8 = 14;
85/// Server error `payload_too_large` (HTTP 413).
86pub const PAYLOAD_TOO_LARGE: u8 = 15;
87/// Server error `unsupported_media_type` (HTTP 415).
88pub const UNSUPPORTED_MEDIA_TYPE: u8 = 16;
89/// Server error `invalid` (HTTP 422), or a preview with validation errors.
90pub const INVALID: u8 = 17;
91/// Server error `too_many_challenges` (HTTP 429).
92pub const TOO_MANY_CHALLENGES: u8 = 18;
93/// Server error `backup_refused` (HTTP 409): no backup recipient configured.
94pub const BACKUP_REFUSED: u8 = 19;
95/// Server error `internal` (HTTP 500).
96pub const INTERNAL: u8 = 20;
97/// A server error code this client does not know.
98pub const UNKNOWN_SERVER_ERROR: u8 = 21;
99/// Server error `request_timeout` (HTTP 408): the node gave up waiting for
100/// the request to arrive.
101pub const REQUEST_TIMEOUT: u8 = 22;
102/// Server error `capacity_exhausted` (HTTP 409): every tenant slot is
103/// taken; the invitation is still `issued`.
104pub const CAPACITY_EXHAUSTED: u8 = 23;
105/// Server error `invitation_refused` (HTTP 403): the invitation is unknown,
106/// expired, revoked, used or for another identity.
107pub const INVITATION_REFUSED: u8 = 24;
108/// Server error `invitations_disabled` (HTTP 403):
109/// `membership.invitations_enabled` is off.
110pub const INVITATIONS_DISABLED: u8 = 25;
111/// Server error `gate_unavailable` (HTTP 503). From the node's API: the gate
112/// could not be paused and drained for a backup. From the gate: pod writes
113/// are paused for a backup or archive, or the gate has not heard from the
114/// node's controller in time (its directory lease ran out). The gate
115/// forwarded nothing, so the request can be retried shortly.
116pub const GATE_UNAVAILABLE: u8 = 26;
117/// Server error `too_many_requests` (HTTP 429): over the admission rate
118/// limit.
119pub const TOO_MANY_REQUESTS: u8 = 27;
120/// Server error `misdirected_request` (HTTP 421): `Host` is not the node's
121/// canonical authority, so `--origin` is wrong.
122pub const MISDIRECTED_REQUEST: u8 = 28;
123/// Server error `method_not_allowed` (HTTP 405), including a `DELETE` of
124/// the root ACL.
125pub const METHOD_NOT_ALLOWED: u8 = 29;
126/// Server error `insufficient_storage` (HTTP 507): the pod volume is low
127/// and writes are stopped.
128pub const INSUFFICIENT_STORAGE: u8 = 30;
129/// `precondition_failed` (HTTP 412): `--if-match` named another version,
130/// or `--create-only` found the resource already there.
131pub const PRECONDITION_FAILED: u8 = 31;
132/// Server error `bad_gateway` (HTTP 502): the gate got no valid answer
133/// from the pod server or the controller.
134pub const BAD_GATEWAY: u8 = 32;
135/// Server error `gateway_timeout` (HTTP 504): the pod server or the
136/// controller did not answer the gate in time. A write may or may not have
137/// happened; read before writing again.
138pub const GATEWAY_TIMEOUT: u8 = 33;
139/// Server error `suspended` (HTTP 403): the administrator has suspended the
140/// membership the request depends on, the caller's own or the pod owner's.
141/// Signed requests by or to a suspended member are refused until the
142/// administrator resumes it; an existing public profile stays readable.
143pub const SUSPENDED: u8 = 34;
144/// Server error `archive_required` (HTTP 409): `member remove` needs an
145/// archive taken during the member's current suspension. Run
146/// `member archive` first, or waive it with `--no-archive`.
147pub const ARCHIVE_REQUIRED: u8 = 35;
148/// Server error `pod_data_present` (HTTP 409): an invitation or redemption
149/// for a removed or cancelled key whose earlier pod data has not been
150/// purged yet.
151pub const POD_DATA_PRESENT: u8 = 36;
152/// Server error `pod_unavailable` (HTTP 503): the pod server could not be
153/// reached or broke off (from the gate), or the node runs without pods or
154/// has no canonical origin yet (from the controller).
155pub const POD_UNAVAILABLE: u8 = 38;
156/// The command line could not be parsed.
157pub const USAGE: u8 = 64;
158
159/// Returns the exit code for a server error code from the API contract.
160///
161/// # Examples
162///
163/// ```
164/// use stratum_node_cli::exit;
165///
166/// assert_eq!(exit::for_server_code("conflict"), exit::CONFLICT);
167/// assert_eq!(exit::for_server_code("request_timeout"), exit::REQUEST_TIMEOUT);
168/// assert_eq!(exit::for_server_code("capacity_exhausted"), exit::CAPACITY_EXHAUSTED);
169/// assert_eq!(exit::for_server_code("suspended"), exit::SUSPENDED);
170/// assert_eq!(exit::for_server_code("pod_unavailable"), exit::POD_UNAVAILABLE);
171/// assert_eq!(exit::for_server_code("something_new"), exit::UNKNOWN_SERVER_ERROR);
172/// // `lease_expired` is the gate's log name for a decision, not a wire code.
173/// assert_eq!(exit::for_server_code("lease_expired"), exit::UNKNOWN_SERVER_ERROR);
174/// ```
175pub fn for_server_code(code: &str) -> u8 {
176    match code {
177        "bad_request" => BAD_REQUEST,
178        "unauthorized" => UNAUTHORIZED,
179        "forbidden" | "app_forbidden" => FORBIDDEN,
180        "not_found" => NOT_FOUND,
181        "conflict" => CONFLICT,
182        "payload_too_large" => PAYLOAD_TOO_LARGE,
183        "unsupported_media_type" => UNSUPPORTED_MEDIA_TYPE,
184        "invalid" => INVALID,
185        "too_many_challenges" => TOO_MANY_CHALLENGES,
186        "backup_refused" => BACKUP_REFUSED,
187        "internal" => INTERNAL,
188        "request_timeout" => REQUEST_TIMEOUT,
189        "capacity_exhausted" => CAPACITY_EXHAUSTED,
190        "invitation_refused" => INVITATION_REFUSED,
191        "invitations_disabled" => INVITATIONS_DISABLED,
192        "gate_unavailable" => GATE_UNAVAILABLE,
193        "too_many_requests" | "rate_limited" => TOO_MANY_REQUESTS,
194        "misdirected_request" => MISDIRECTED_REQUEST,
195        "method_not_allowed" => METHOD_NOT_ALLOWED,
196        "insufficient_storage" => INSUFFICIENT_STORAGE,
197        "precondition_failed" => PRECONDITION_FAILED,
198        "bad_gateway" => BAD_GATEWAY,
199        "gateway_timeout" => GATEWAY_TIMEOUT,
200        "suspended" => SUSPENDED,
201        "archive_required" => ARCHIVE_REQUIRED,
202        "pod_data_present" => POD_DATA_PRESENT,
203        "pod_unavailable" => POD_UNAVAILABLE,
204        _ => UNKNOWN_SERVER_ERROR,
205    }
206}