1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! The crate's [`Error`].
use std::process::ExitStatus;
/// What can go wrong driving a [`ComposeEngine`](crate::ComposeEngine).
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// The engine can't be reached: the `docker` binary is missing, or its daemon isn't
/// running. Distinct from "nothing is running", which is an empty listing.
#[error("Docker is not available: {reason}")]
Unavailable {
/// Why, as the CLI or the OS put it.
reason: String,
},
/// A command ran and failed.
#[error("`{command}` failed ({status}){}", stderr_suffix(.stderr))]
Failed {
/// The command line, without the program (e.g. `compose up -d --wait`).
command: String,
/// Its exit status.
status: ExitStatus,
/// What it printed on stderr, when it was captured.
stderr: String,
},
/// A command's output didn't parse as expected.
#[error("parsing the output of `{command}`: {source}")]
Parse {
/// The command line, without the program.
command: String,
/// The JSON error.
#[source]
source: serde_json::Error,
},
/// Spawning or talking to a command failed for another reason.
#[error("running `{command}`: {source}")]
Io {
/// The command line, without the program.
command: String,
/// The I/O error.
#[source]
source: std::io::Error,
},
}
fn stderr_suffix(stderr: &str) -> String {
let stderr = stderr.trim();
if stderr.is_empty() {
String::new()
} else {
format!(": {stderr}")
}
}
impl Error {
/// Whether this is [`Error::Unavailable`].
pub fn is_unavailable(&self) -> bool {
matches!(self, Error::Unavailable { .. })
}
}
/// The crate's result type.
pub type Result<T, E = Error> = std::result::Result<T, E>;