use std::fmt::Write;
use std::path::PathBuf;
use thiserror::Error;
use crate::RawPid;
#[derive(Debug, Error)]
pub enum Error {
#[error("{runtime} runtime not found: '{tool}' command is not available")]
RuntimeNotFound {
runtime: &'static str,
tool: &'static str,
},
#[error("failed to execute command: {command}")]
CommandFailedToRun {
command: String,
#[source]
source: std::io::Error,
},
#[error("{command} failed (exit status {status}): {stderr}")]
CommandFailed {
command: String,
status: String,
stderr: String,
},
#[error("unexpected output from {command}: {message}")]
UnexpectedOutput { command: String, message: String },
#[error("invalid PID '{pid}' reported by {runtime} for container '{container}'")]
InvalidPid {
pid: String,
runtime: &'static str,
container: String,
#[source]
source: std::num::ParseIntError,
},
#[error("container '{0}' is not running")]
NotRunning(String),
#[error("container '{container}' not found: {message}")]
ContainerNotFound { container: String, message: String },
#[error("failed to read {path}")]
Io {
path: PathBuf,
#[source]
source: std::io::Error,
},
#[error("'{0}' is not a valid PID (process ID)")]
InvalidProcessId(String, #[source] std::num::ParseIntError),
#[error("no process with PID {0} found")]
NoSuchProcess(RawPid),
#[error("failed to find container '{container}' - tried the following runtimes:{tried}")]
NoRuntimeMatched { container: String, tried: String },
}
pub(crate) fn format_chain(err: &dyn std::error::Error) -> String {
let mut msg = err.to_string();
let mut source = err.source();
while let Some(cause) = source {
let _ = write!(msg, ": {}", cause);
source = cause.source();
}
msg
}