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) |
13//! | 8 | `join`: the pod was not provisioned within `--wait-secs` |
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//! | 64 | Invalid command line |
40
41/// Success. For `status`, the node is `ready`.
42pub const OK: u8 = 0;
43/// A local failure: a key, token or resume file, the clock, or output.
44pub const FAILURE: u8 = 1;
45/// The node could not be reached.
46pub const UNREACHABLE: u8 = 2;
47/// `status`: the node is `unconfigured`.
48pub const UNCONFIGURED: u8 = 3;
49/// `status`: the node is `degraded`.
50pub const DEGRADED: u8 = 4;
51/// The node's response does not follow the API contract.
52pub const PROTOCOL: u8 = 5;
53/// Changes were previewed but not applied because nothing confirmed them.
54pub const NOT_CONFIRMED: u8 = 6;
55/// A local check refused the node's answer.
56pub const REFUSED: u8 = 7;
57/// `join`: the pod was not provisioned within the wait; rerun `join` to
58/// continue.
59pub const TIMED_OUT: u8 = 8;
60/// `export`: the archive and manifest were written, but at least one entry
61/// changed, vanished or was skipped during the walk; the manifest lists
62/// them.
63pub const EXPORT_INCOMPLETE: u8 = 9;
64/// Server error `bad_request` (HTTP 400).
65pub const BAD_REQUEST: u8 = 10;
66/// Server error `unauthorized` (HTTP 401).
67pub const UNAUTHORIZED: u8 = 11;
68/// Server error `forbidden` (HTTP 403).
69pub const FORBIDDEN: u8 = 12;
70/// Server error `not_found` (HTTP 404).
71pub const NOT_FOUND: u8 = 13;
72/// Server error `conflict` (HTTP 409): stale revision, already bound or
73/// operation conflict.
74pub const CONFLICT: u8 = 14;
75/// Server error `payload_too_large` (HTTP 413).
76pub const PAYLOAD_TOO_LARGE: u8 = 15;
77/// Server error `unsupported_media_type` (HTTP 415).
78pub const UNSUPPORTED_MEDIA_TYPE: u8 = 16;
79/// Server error `invalid` (HTTP 422), or a preview with validation errors.
80pub const INVALID: u8 = 17;
81/// Server error `too_many_challenges` (HTTP 429).
82pub const TOO_MANY_CHALLENGES: u8 = 18;
83/// Server error `backup_refused` (HTTP 409): no backup recipient configured.
84pub const BACKUP_REFUSED: u8 = 19;
85/// Server error `internal` (HTTP 500).
86pub const INTERNAL: u8 = 20;
87/// A server error code this client does not know.
88pub const UNKNOWN_SERVER_ERROR: u8 = 21;
89/// Server error `request_timeout` (HTTP 408): the node gave up waiting for
90/// the request to arrive.
91pub const REQUEST_TIMEOUT: u8 = 22;
92/// Server error `capacity_exhausted` (HTTP 409): every tenant slot is
93/// taken; the invitation is still `issued`.
94pub const CAPACITY_EXHAUSTED: u8 = 23;
95/// Server error `invitation_refused` (HTTP 403): the invitation is unknown,
96/// expired, revoked, used or for another identity.
97pub const INVITATION_REFUSED: u8 = 24;
98/// Server error `invitations_disabled` (HTTP 403):
99/// `membership.invitations_enabled` is off.
100pub const INVITATIONS_DISABLED: u8 = 25;
101/// Server error `gate_unavailable` (HTTP 503): the gate could not be paused
102/// and drained for a backup.
103pub const GATE_UNAVAILABLE: u8 = 26;
104/// Server error `too_many_requests` (HTTP 429): over the admission rate
105/// limit.
106pub const TOO_MANY_REQUESTS: u8 = 27;
107/// Server error `misdirected_request` (HTTP 421): `Host` is not the node's
108/// canonical authority, so `--origin` is wrong.
109pub const MISDIRECTED_REQUEST: u8 = 28;
110/// Server error `method_not_allowed` (HTTP 405), including a `DELETE` of
111/// the root ACL.
112pub const METHOD_NOT_ALLOWED: u8 = 29;
113/// Server error `insufficient_storage` (HTTP 507): the pod volume is low
114/// and writes are stopped.
115pub const INSUFFICIENT_STORAGE: u8 = 30;
116/// `precondition_failed` (HTTP 412): `--if-match` named another version,
117/// or `--create-only` found the resource already there.
118pub const PRECONDITION_FAILED: u8 = 31;
119/// Server error `bad_gateway` (HTTP 502): the gate got no valid answer
120/// from the pod server or the controller.
121pub const BAD_GATEWAY: u8 = 32;
122/// Server error `gateway_timeout` (HTTP 504): the pod server or the
123/// controller did not answer the gate in time. A write may or may not have
124/// happened; read before writing again.
125pub const GATEWAY_TIMEOUT: u8 = 33;
126/// The command line could not be parsed.
127pub const USAGE: u8 = 64;
128
129/// Returns the exit code for a server error code from the API contract.
130///
131/// # Examples
132///
133/// ```
134/// use stratum_node_cli::exit;
135///
136/// assert_eq!(exit::for_server_code("conflict"), exit::CONFLICT);
137/// assert_eq!(exit::for_server_code("request_timeout"), exit::REQUEST_TIMEOUT);
138/// assert_eq!(exit::for_server_code("capacity_exhausted"), exit::CAPACITY_EXHAUSTED);
139/// assert_eq!(exit::for_server_code("something_new"), exit::UNKNOWN_SERVER_ERROR);
140/// ```
141pub fn for_server_code(code: &str) -> u8 {
142    match code {
143        "bad_request" => BAD_REQUEST,
144        "unauthorized" => UNAUTHORIZED,
145        "forbidden" => FORBIDDEN,
146        "not_found" => NOT_FOUND,
147        "conflict" => CONFLICT,
148        "payload_too_large" => PAYLOAD_TOO_LARGE,
149        "unsupported_media_type" => UNSUPPORTED_MEDIA_TYPE,
150        "invalid" => INVALID,
151        "too_many_challenges" => TOO_MANY_CHALLENGES,
152        "backup_refused" => BACKUP_REFUSED,
153        "internal" => INTERNAL,
154        "request_timeout" => REQUEST_TIMEOUT,
155        "capacity_exhausted" => CAPACITY_EXHAUSTED,
156        "invitation_refused" => INVITATION_REFUSED,
157        "invitations_disabled" => INVITATIONS_DISABLED,
158        "gate_unavailable" => GATE_UNAVAILABLE,
159        "too_many_requests" => TOO_MANY_REQUESTS,
160        "misdirected_request" => MISDIRECTED_REQUEST,
161        "method_not_allowed" => METHOD_NOT_ALLOWED,
162        "insufficient_storage" => INSUFFICIENT_STORAGE,
163        "precondition_failed" => PRECONDITION_FAILED,
164        "bad_gateway" => BAD_GATEWAY,
165        "gateway_timeout" => GATEWAY_TIMEOUT,
166        _ => UNKNOWN_SERVER_ERROR,
167    }
168}