1use std::path::PathBuf;
5
6pub use self::code::ErrorCode;
7pub use self::detail::{DETAIL_VERSION, Detail, DownloadKind, ErrorDetail, PortHolder, ProviderNeed};
8pub use self::report::ErrorReport;
9pub use self::timed_out::TimedOut;
10pub(crate) use self::timed_out::kept_line;
11
12mod code;
13mod detail;
14mod report;
15mod timed_out;
16
17pub type Result<T, E = Error> = std::result::Result<T, E>;
19
20#[derive(Debug, thiserror::Error)]
22#[non_exhaustive]
23pub enum Error {
24 #[error("{what} isn't supported")]
27 Unsupported {
28 what: String,
31 },
32 #[error("{what} isn't supported")]
36 Declined {
37 what: String,
39 },
40 #[error("{} couldn't be {action}: {source}", path.display())]
42 Io {
43 path: PathBuf,
45 action: &'static str,
47 source: std::io::Error,
49 },
50 #[error("run `{run}` is busy: another process runs an operation on it")]
52 Locked {
53 run: String,
55 },
56 #[error("step `{step}`: `{tool}` isn't installed or isn't on PATH")]
58 ToolMissing {
59 step: String,
61 tool: String,
63 },
64 #[error("`{tool}` is version {found}; this run needs {required} or later")]
66 ToolTooOld {
67 tool: String,
69 found: String,
71 required: String,
73 },
74 #[error("step `{step}` failed: {reason}")]
76 ToolFailed {
77 step: String,
79 reason: String,
81 },
82 #[error("step `{step}` failed: `{tool}` doesn't answer: {reason}")]
86 ToolUnanswered {
87 step: String,
89 tool: String,
91 reason: String,
93 },
94 #[error("step `{step}` failed: `{tool}` refused this account: {reason}")]
96 ToolDenied {
97 step: String,
99 tool: String,
101 reason: String,
103 },
104 #[error("step `{step}`: the sign-in to {} ran out or is missing: {reason}", cloud.map_or_else(|| "the cloud".to_owned(), |c| format!("`{}`", crate::encoding::wire_name(&c))))]
106 CredentialsExpired {
107 step: String,
109 cloud: Option<crate::project::platform::Cloud>,
111 reason: String,
113 },
114 #[error("step `{step}`: another network holds the addresses a network of the run needs: {reason}")]
116 AddressPoolOverlap {
117 step: String,
119 reason: String,
121 },
122 #[error("step `{step}` {timed_out}")]
124 Timeout {
125 step: String,
127 timed_out: TimedOut,
129 },
130 #[error("step `{step}` showed no progress downloading for {after_s}s and was stopped{}", last_line.as_ref().map_or_else(String::new, |l| format!("; its last line: `{l}`")))]
134 DownloadStalled {
135 what: DownloadKind,
137 step: String,
140 after_s: u64,
142 last_line: Option<String>,
145 },
146 #[error("step `{step}` couldn't download what it needs: {reason}")]
148 DownloadFailed {
149 what: DownloadKind,
151 step: String,
153 reason: String,
155 },
156 #[error("run `{run}` is already running")]
158 AlreadyRunning {
159 run: String,
161 },
162 #[error("run `{run}` is recorded on `{recorded}`, not `{asked}`")]
164 RunningElsewhere {
165 run: String,
167 recorded: String,
169 asked: String,
171 },
172 #[error("host port {port}/{protocol} is in use{}", holder.as_ref().map_or_else(String::new, |h| format!(": `{}` (pid {}) listens on it", h.program, h.pid)))]
174 PortInUse {
175 port: crate::port::HostPort,
177 protocol: String,
179 holder: Option<PortHolder>,
182 },
183 #[error("run `{run}` can't be parked ({mode}): its manifest has no steps for it")]
185 NotParkable {
186 run: String,
188 mode: &'static str,
190 },
191 #[error("run `{run}` has nothing left to resume: start it again")]
193 NothingToResume {
194 run: String,
196 },
197 #[error("run `{run}` can't resume as it was: {} gone: start it again", gone(machines))]
200 PartlyGone {
201 run: String,
203 machines: Vec<String>,
205 },
206 #[error("run `{run}` isn't running")]
208 NotRunning {
209 run: String,
211 },
212 #[error("not ready: {condition}: {detail}")]
214 NotReady {
215 condition: String,
217 detail: String,
219 },
220 #[error("secret `{name}` is required and has no value")]
222 SecretMissing {
223 name: String,
225 },
226 #[error("{}: not a project to run: {reason}", dir.display())]
228 Project {
229 dir: PathBuf,
231 reason: String,
233 },
234 #[error("input `{name}`: {reason}")]
237 Input {
238 name: String,
240 reason: String,
242 },
243 #[error("run `{run}` has no machine `{machine}`")]
245 MachineUnknown {
246 run: String,
248 machine: String,
250 },
251 #[error("run `{run}` has several {which} ({machines}): name one")]
253 MachineAmbiguous {
254 run: String,
256 which: &'static str,
258 machines: String,
260 },
261 #[error("run `{run}` has no network `{network}`")]
263 NetworkUnknown {
264 run: String,
266 network: String,
268 },
269 #[error("elevation: {reason}")]
271 Elevation {
272 reason: String,
274 },
275 #[error("provider `{provider}`: {reason}")]
277 Provider {
278 provider: String,
280 reason: String,
282 need: Option<ProviderNeed>,
284 },
285 #[error("a Vagrant run needs a provider: pass one of this run's ({providers})")]
287 NoProvider {
288 providers: String,
290 },
291 #[error("run `{run}` can't take the deployment over: {reason}")]
293 Adopt {
294 run: String,
296 reason: String,
298 },
299 #[error(
301 "`{value}` can't name a run: use 1 to 63 lowercase letters, digits, `-` or `_`, starting with a letter or digit, not a Windows device name (`con`, `nul`, `com1`…)"
302 )]
303 RunName {
304 value: String,
306 },
307 #[error("machine `{machine}` has no graphical console: {why}")]
309 NoConsole {
310 machine: String,
312 why: String,
314 },
315 #[error("step `{step}`: {} refused the run's credentials: {reason}", server.as_ref().map_or_else(|| "the server".to_owned(), |s| format!("server `{s}`")))]
318 ServerAuthFailed {
319 step: String,
321 server: Option<String>,
323 reason: String,
325 },
326 #[error("{} holds no {what} this runner reads: {source}", path.display())]
329 Corrupt {
330 path: PathBuf,
332 what: &'static str,
334 #[source]
336 source: serde_json::Error,
337 },
338 #[error("the home's state store couldn't be {action}: {reason}")]
340 Store {
341 action: &'static str,
343 code: ErrorCode,
345 reason: String,
347 },
348 #[error(transparent)]
350 IllegalTransition(#[from] crate::state::IllegalTransition),
351 #[error("run `{run}` was expected {expected} but is {}", found.map_or("not recorded", crate::state::RunState::as_str))]
353 StateConflict {
354 run: String,
356 expected: crate::state::RunState,
358 found: Option<crate::state::RunState>,
360 },
361 #[error("`{scope}` is held by process {pid}")]
363 LeaseHeld {
364 scope: String,
366 pid: u32,
368 },
369}
370
371impl Error {
372 pub(crate) fn step(&self) -> Option<&str> {
374 match self {
375 Error::ToolMissing { step, .. }
376 | Error::ToolFailed { step, .. }
377 | Error::ToolUnanswered { step, .. }
378 | Error::ToolDenied { step, .. }
379 | Error::CredentialsExpired { step, .. }
380 | Error::AddressPoolOverlap { step, .. }
381 | Error::Timeout { step, .. }
382 | Error::DownloadStalled { step, .. }
383 | Error::DownloadFailed { step, .. }
384 | Error::ServerAuthFailed { step, .. } => Some(step),
385 _ => None,
386 }
387 }
388
389 #[cfg(test)]
391 pub(crate) fn timed_out(&self) -> Option<&TimedOut> {
392 match self {
393 Error::Timeout { timed_out, .. } => Some(timed_out),
394 _ => None,
395 }
396 }
397
398 #[must_use]
400 pub fn code(&self) -> ErrorCode {
401 match self {
402 Error::Unsupported { .. } | Error::Declined { .. } => ErrorCode::Unsupported,
403 Error::Project { .. } => ErrorCode::ProjectInvalid,
404 Error::Io { .. } => ErrorCode::Io,
405 Error::Corrupt { .. } => ErrorCode::Corrupt,
406 Error::Locked { .. } => ErrorCode::Locked,
407 Error::ToolMissing { .. } => ErrorCode::ToolMissing,
408 Error::ToolTooOld { .. } => ErrorCode::ToolTooOld,
409 Error::ToolFailed { .. } => ErrorCode::ToolFailed,
410 Error::ToolUnanswered { .. } => ErrorCode::ToolUnanswered,
411 Error::ToolDenied { .. } => ErrorCode::ToolDenied,
412 Error::CredentialsExpired { .. } => ErrorCode::CredentialsExpired,
413 Error::AddressPoolOverlap { .. } => ErrorCode::AddressPoolOverlap,
414 Error::Timeout { .. } => ErrorCode::Timeout,
415 Error::DownloadStalled { .. } => ErrorCode::DownloadStalled,
416 Error::DownloadFailed { .. } => ErrorCode::DownloadFailed,
417 Error::AlreadyRunning { .. } => ErrorCode::AlreadyRunning,
418 Error::RunningElsewhere { .. } => ErrorCode::RunningElsewhere,
419 Error::PortInUse { .. } => ErrorCode::PortInUse,
420 Error::NotParkable { .. } => ErrorCode::NotParkable,
421 Error::NothingToResume { .. } | Error::PartlyGone { .. } => ErrorCode::NothingToResume,
422 Error::NotRunning { .. } => ErrorCode::NotRunning,
423 Error::NotReady { .. } => ErrorCode::NotReady,
424 Error::SecretMissing { .. } => ErrorCode::SecretMissing,
425 Error::Input { .. } => ErrorCode::InputInvalid,
426 Error::RunName { .. } => ErrorCode::RunName,
427 Error::Provider { .. } | Error::NoProvider { .. } => ErrorCode::ProviderRefused,
428 Error::Elevation { .. } => ErrorCode::ElevationFailed,
429 Error::MachineUnknown { .. } => ErrorCode::MachineUnknown,
430 Error::MachineAmbiguous { .. } => ErrorCode::MachineAmbiguous,
431 Error::NetworkUnknown { .. } => ErrorCode::NetworkUnknown,
432 Error::Adopt { .. } => ErrorCode::AdoptRefused,
433 Error::NoConsole { .. } => ErrorCode::NoConsole,
434 Error::ServerAuthFailed { .. } => ErrorCode::ServerAuthFailed,
435 Error::Store { code, .. } => *code,
436 Error::IllegalTransition(_) => ErrorCode::IllegalTransition,
437 Error::StateConflict { .. } => ErrorCode::StateConflict,
438 Error::LeaseHeld { .. } => ErrorCode::LeaseHeld,
439 }
440 }
441
442 pub(crate) fn io(path: impl Into<PathBuf>, action: &'static str) -> impl FnOnce(std::io::Error) -> Error {
444 let path = path.into();
445 move |source| Error::Io { path, action, source }
446 }
447}
448
449fn gone(machines: &[String]) -> String {
451 let quoted: Vec<String> = machines.iter().map(|m| format!("`{m}`")).collect();
452 match quoted.as_slice() {
453 [] => "nothing is".to_owned(),
454 [one] => format!("machine {one} is"),
455 [rest @ .., last] => format!("machines {} and {last} are", rest.join(", ")),
456 }
457}
458
459#[cfg(test)]
460mod tests {
461 use super::*;
462
463 #[test]
464 fn every_code_is_written_as_serde_writes_it() {
465 crate::testing::assert_named_as_serde(ErrorCode::as_str);
466 }
467
468 #[test]
469 fn a_run_partly_gone_names_its_gone_machines_and_has_nothing_to_resume() {
470 let one = Error::PartlyGone {
471 run: "r".into(),
472 machines: vec!["web".into()],
473 };
474 assert_eq!(one.code(), ErrorCode::NothingToResume);
475 assert_eq!(one.to_string(), "run `r` can't resume as it was: machine `web` is gone: start it again");
476 let three = Error::PartlyGone {
477 run: "r".into(),
478 machines: vec!["a".into(), "b".into(), "c".into()],
479 };
480 assert!(three.to_string().contains("machines `a`, `b` and `c` are gone"), "{three}");
481 }
482
483 #[test]
484 fn an_elevation_that_failed_has_its_code_whatever_ended_it() {
485 let lost = Error::Elevation {
486 reason: "the elevated session ended: the broker stopped".into(),
487 };
488 assert_eq!((lost.code(), lost.code().as_str()), (ErrorCode::ElevationFailed, "elevation_failed"));
489 }
490}