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
63
64
65
66
67
68
69
70
use std::fmt::Debug;
use std::fmt::Display;

use crate::CommandDisplay;
#[cfg(doc)]
use crate::CommandExt;
#[cfg(doc)]
use crate::OutputError;

/// An error from failing to execute a command. Produced by [`CommandExt`].
///
/// This is a command that fails to start, rather than a command that exits with a non-zero status
/// or similar, like [`OutputError`].
///
/// ```
/// # use pretty_assertions::assert_eq;
/// # use std::process::Command;
/// # use command_error::Utf8ProgramAndArgs;
/// # use command_error::CommandDisplay;
/// # use command_error::ExecError;
/// let mut command = Command::new("echo");
/// command.arg("puppy doggy");
/// let displayed: Utf8ProgramAndArgs = (&command).into();
/// let error = ExecError::new(
///     Box::new(displayed),
///     std::io::Error::new(
///         std::io::ErrorKind::NotFound,
///         "File not found (os error 2)"
///     ),
/// );
/// assert_eq!(
///     error.to_string(),
///     "Failed to execute `echo`: File not found (os error 2)"
/// );
/// ```
pub struct ExecError {
    pub(crate) command: Box<dyn CommandDisplay>,
    pub(crate) inner: std::io::Error,
}

impl ExecError {
    /// Construct a new [`ExecError`].
    pub fn new(command: Box<dyn CommandDisplay>, inner: std::io::Error) -> Self {
        Self { command, inner }
    }
}

impl Debug for ExecError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ExecError")
            .field("program", &self.command.program())
            .field("inner", &self.inner)
            .finish()
    }
}

impl Display for ExecError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // TODO: Should this contain an additional message like
        // "Is `program` installed and present in your `$PATH`?"
        write!(
            f,
            "Failed to execute `{}`: {}",
            self.command.program_quoted(),
            self.inner
        )
    }
}

impl std::error::Error for ExecError {}