stratum_node_cli/error.rs
1//! The error type of every `stratum` command.
2
3use std::path::PathBuf;
4
5use crate::exit;
6use crate::key::KeyFileError;
7use crate::private_file::PrivateFileError;
8use crate::token::TokenFileError;
9use crate::url::ParseUrlError;
10
11/// Why a `stratum` command failed.
12///
13/// Each variant maps to a stable process exit code through
14/// [`exit_code`](Self::exit_code) and to a short machine-readable code
15/// through [`code`](Self::code). A server error displays as
16/// `code: message`, exactly as the node sent it (with control characters
17/// escaped). No variant ever carries the operator's secret key or the
18/// bootstrap token.
19#[derive(Debug, thiserror::Error)]
20#[non_exhaustive]
21pub enum CliError {
22 /// The command line asks for something impossible, such as an unknown
23 /// configuration path or a value of the wrong type.
24 #[error("{0}")]
25 Usage(String),
26 /// The `--url` value was refused.
27 #[error("invalid --url")]
28 Url(#[source] ParseUrlError),
29 /// A command needs the operator key and no `--key-file` was given.
30 #[error("this command needs the operator key: pass --key-file or set STRATUM_KEY_FILE")]
31 MissingKeyFile,
32 /// The operator key file could not be created or loaded.
33 #[error("key file {}", path.display())]
34 KeyFile {
35 /// The key file.
36 path: PathBuf,
37 /// What went wrong.
38 #[source]
39 source: KeyFileError,
40 },
41 /// The bootstrap token file could not be read.
42 #[error("token file {}", path.display())]
43 TokenFile {
44 /// The token file.
45 path: PathBuf,
46 /// What went wrong.
47 #[source]
48 source: TokenFileError,
49 },
50 /// A file other than the key or token file (resume file, claim body
51 /// file) could not be created, read or removed.
52 #[error("file {}", path.display())]
53 File {
54 /// The file.
55 path: PathBuf,
56 /// What went wrong.
57 #[source]
58 source: PrivateFileError,
59 },
60 /// A file was readable but its contents are not what the command needs.
61 #[error("file {}: {message}", path.display())]
62 FileContents {
63 /// The file.
64 path: PathBuf,
65 /// What is wrong with it.
66 message: String,
67 },
68 /// The node could not be reached.
69 #[error("cannot reach the node at {url}: {message}")]
70 Unreachable {
71 /// The URL that was tried.
72 url: String,
73 /// The transport error.
74 message: String,
75 },
76 /// The node answered with something the API contract does not allow.
77 #[error("unexpected response from the node: {0}")]
78 Protocol(String),
79 /// The node refused the request with an error from the API contract.
80 #[error("{code}: {message}")]
81 Server {
82 /// The HTTP status.
83 status: u16,
84 /// The `error.code` member.
85 code: String,
86 /// The `error.message` member.
87 message: String,
88 /// Validation errors sent with an `invalid` error, as `path: message`.
89 details: Vec<String>,
90 },
91 /// A local check refused what the node sent: a claim body naming
92 /// another claimant or node, an expired claim, a manifest with a bad
93 /// signature or another author.
94 #[error("refused: {0}")]
95 Refused(String),
96 /// A preview reported validation errors, so nothing was sent to apply.
97 #[error("the previewed configuration is invalid")]
98 InvalidPreview {
99 /// The validation errors, as `path: message`.
100 details: Vec<String>,
101 },
102 /// Changes were previewed but not confirmed, so nothing was applied.
103 #[error("not applied: confirm interactively or pass --yes")]
104 NotConfirmed,
105 /// Writing output, reading the clock or the random source, or signing
106 /// failed.
107 #[error("{0}")]
108 Local(String),
109}
110
111impl CliError {
112 /// Returns the process exit code; see [`exit`](crate::exit).
113 ///
114 /// # Examples
115 ///
116 /// ```
117 /// use stratum_node_cli::{exit, CliError};
118 ///
119 /// let err = CliError::Server {
120 /// status: 409,
121 /// code: "conflict".into(),
122 /// message: "stale base revision".into(),
123 /// details: Vec::new(),
124 /// };
125 /// assert_eq!(err.to_string(), "conflict: stale base revision");
126 /// assert_eq!(err.exit_code(), exit::CONFLICT);
127 /// ```
128 pub fn exit_code(&self) -> u8 {
129 match self {
130 Self::Usage(_) | Self::Url(_) | Self::MissingKeyFile => exit::USAGE,
131 Self::KeyFile { .. }
132 | Self::TokenFile { .. }
133 | Self::File { .. }
134 | Self::FileContents { .. }
135 | Self::Local(_) => exit::FAILURE,
136 Self::Unreachable { .. } => exit::UNREACHABLE,
137 Self::Protocol(_) => exit::PROTOCOL,
138 Self::Server { code, .. } => exit::for_server_code(code),
139 Self::Refused(_) => exit::REFUSED,
140 Self::InvalidPreview { .. } => exit::INVALID,
141 Self::NotConfirmed => exit::NOT_CONFIRMED,
142 }
143 }
144
145 /// Returns a short machine-readable code: the server's `error.code`, or
146 /// one of `usage`, `local`, `unreachable`, `protocol`, `refused`,
147 /// `invalid` and `not_confirmed` for failures found by the client.
148 pub fn code(&self) -> &str {
149 match self {
150 Self::Usage(_) | Self::Url(_) | Self::MissingKeyFile => "usage",
151 Self::KeyFile { .. }
152 | Self::TokenFile { .. }
153 | Self::File { .. }
154 | Self::FileContents { .. }
155 | Self::Local(_) => "local",
156 Self::Unreachable { .. } => "unreachable",
157 Self::Protocol(_) => "protocol",
158 Self::Server { code, .. } => code,
159 Self::Refused(_) => "refused",
160 Self::InvalidPreview { .. } => "invalid",
161 Self::NotConfirmed => "not_confirmed",
162 }
163 }
164
165 /// Returns the validation errors attached to an `invalid` failure, as
166 /// `path: message` lines; empty for every other failure.
167 pub fn details(&self) -> &[String] {
168 match self {
169 Self::Server { details, .. } | Self::InvalidPreview { details } => details,
170 _ => &[],
171 }
172 }
173}