//! Executor errors. They preserve stage, operation, command, exit status and //! captured output, and never contain secrets. use std::path::PathBuf; use phoebus_core::CoreError; pub type Result = std::result::Result; /// Heap-allocated command failure details. Kept on the heap so `ExecError` /// stays small enough for `Result` to pass `result_large_err` /// (128-byte threshold) without boxing at every call site. #[derive(Debug, Clone, PartialEq, Eq)] pub struct CommandFailure { pub stage: String, pub operation: String, pub program: String, pub status: i32, pub stdout: String, pub stderr: String, } impl std::fmt::Display for CommandFailure { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "[{}] {} failed (exit {}): {}", self.stage, self.operation, self.status, self.program ) } } #[derive(Debug, thiserror::Error)] pub enum ExecError { #[error("{0}")] Command(Box), #[error("[{stage}] {operation}: {message}")] Operation { stage: String, operation: String, message: String, }, #[error("command could not be started: {program}: {source}")] Spawn { program: String, #[source] source: std::io::Error, }, #[error("I/O error for {path}: {source}")] Io { path: PathBuf, #[source] source: std::io::Error, }, #[error("missing required tool: {0}")] MissingTool(String), #[error("destructive operation not authorized: {0}")] NotAuthorized(String), #[error("core error: {0}")] Core(#[from] CoreError), } impl ExecError { pub fn command(details: CommandFailure) -> Self { ExecError::Command(Box::new(details)) } pub fn command_details(&self) -> Option<&CommandFailure> { match self { ExecError::Command(details) => Some(details), _ => None, } } pub fn io(path: impl Into, source: std::io::Error) -> Self { ExecError::Io { path: path.into(), source, } } pub fn operation(stage: &str, operation: &str, message: impl Into) -> Self { ExecError::Operation { stage: stage.to_string(), operation: operation.to_string(), message: message.into(), } } } #[cfg(test)] mod tests { use super::*; fn sample_failure() -> CommandFailure { CommandFailure { stage: "storage_prepare".into(), operation: "create GPT".into(), program: "sgdisk".into(), status: 2, stdout: "partial output".into(), stderr: "line1\nline2\nline3\nline4\nline5\nline6".into(), } } #[test] fn command_error_text_preserves_stage_operation_status_and_program() { let err = ExecError::command(sample_failure()); assert_eq!( err.to_string(), "[storage_prepare] create GPT failed (exit 2): sgdisk" ); } #[test] fn command_error_preserves_captured_stdout_and_stderr() { let err = ExecError::command(sample_failure()); let details = err.command_details().expect("command details"); assert_eq!(details.stdout, "partial output"); assert_eq!(details.stderr, "line1\nline2\nline3\nline4\nline5\nline6"); assert_eq!(details.status, 2); } #[test] fn command_error_stays_small_enough_for_result_large_err() { // Clippy's `result_large_err` fires at 128 bytes. The boxed details // must keep the whole enum comfortably below that. assert!( std::mem::size_of::() <= 96, "ExecError is {} bytes", std::mem::size_of::() ); } #[test] fn non_command_errors_have_no_command_details() { let err = ExecError::operation("stage", "op", "message"); assert!(err.command_details().is_none()); } }