Box the CommandFailure payload so ExecError shrinks 128 -> 96 bytes and Result<T, ExecError> passes result_large_err without per-site boxing. Preserve the display text, redacted stdout/stderr capture, and the stderr tail used for update failures; update the constructor and every match site. Add regression tests for the error text, captured output, and the 96-byte size bound. Resolve the remaining Clippy warnings without blanket allows: a type alias for the snap-entry tuple in actions.rs, a CommandRecord params struct for Logger::command in log.rs, and DiskPhase variant renames in gui_bridge.rs. cargo clippy --workspace --all-targets -- -D warnings is clean; cargo test --workspace passes (692 tests).
146 lines
4 KiB
Rust
146 lines
4 KiB
Rust
//! 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<T> = std::result::Result<T, ExecError>;
|
|
|
|
/// Heap-allocated command failure details. Kept on the heap so `ExecError`
|
|
/// stays small enough for `Result<T, ExecError>` 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<CommandFailure>),
|
|
|
|
#[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<PathBuf>, source: std::io::Error) -> Self {
|
|
ExecError::Io {
|
|
path: path.into(),
|
|
source,
|
|
}
|
|
}
|
|
|
|
pub fn operation(stage: &str, operation: &str, message: impl Into<String>) -> 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::<ExecError>() <= 96,
|
|
"ExecError is {} bytes",
|
|
std::mem::size_of::<ExecError>()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn non_command_errors_have_no_command_details() {
|
|
let err = ExecError::operation("stage", "op", "message");
|
|
assert!(err.command_details().is_none());
|
|
}
|
|
}
|