command_error/exec_error.rs
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
use std::fmt::Debug;
use std::fmt::Display;
use crate::CommandDisplay;
#[cfg(doc)]
use crate::CommandExt;
#[cfg(doc)]
use crate::OutputError;
#[cfg(feature = "miette")]
use miette::Diagnostic;
/// 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 + Send + Sync>,
pub(crate) inner: std::io::Error,
}
impl ExecError {
/// Construct a new [`ExecError`].
pub fn new(command: Box<dyn CommandDisplay + Send + Sync>, 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 {
write!(
f,
"Failed to execute `{}`: {}",
self.command.program_quoted(),
self.inner
)
}
}
impl std::error::Error for ExecError {}
#[cfg(feature = "miette")]
impl Diagnostic for ExecError {
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
Some(Box::new(format!(
"Is {} installed and present on your $PATH?",
self.command.program_quoted()
)))
}
}
#[cfg(test)]
mod tests {
use super::*;
use static_assertions::assert_impl_all;
assert_impl_all!(ExecError: Send, Sync);
}