Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #96 - add more information to errors in subprocess execution #197

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub fn safe_command(
stderr_result += &stderr;
break Some(CmdError::Failed(format_failure(
command,
"Unexpected output on stderr",
&stdout_result,
&stderr_result,
)));
Expand All @@ -73,6 +74,7 @@ pub fn safe_command(
Ok((_, _)) => {
break Some(CmdError::InternalError(format_failure(
command,
"Unknown internal failure",
&stdout_result,
&stderr_result,
)))
Expand All @@ -83,13 +85,15 @@ pub fn safe_command(
Ok(_) => {
break Some(CmdError::Hung(format_failure(
command,
"Timed out and had to be killed",
&stdout_result,
&stderr_result,
)))
}
Err(_) => {
Err(e) => {
break Some(CmdError::InternalError(format_failure(
command,
format!("Unknown internal error {:?}", e).as_str(),
&stdout_result,
&stderr_result,
)))
Expand All @@ -98,6 +102,7 @@ pub fn safe_command(
}
break Some(CmdError::InternalError(format_failure(
command,
format!("Unknown internal failure after error {:?}", e).as_str(),
&stdout_result,
&stderr_result,
)));
Expand All @@ -114,53 +119,55 @@ pub fn safe_command(
}
}
Ok(ExitStatus::Exited(126)) => {
// 126 == "Command cannot execute"
Err(CmdError::CouldNotStart(format_failure(
command,
"Command cannot execute",
&stdout_result,
&stderr_result,
)))
}
Ok(ExitStatus::Exited(127)) => {
// 127 == "Command not found"
Err(CmdError::CouldNotStart(format_failure(
command,
"Command not found",
&stdout_result,
&stderr_result,
)))
}
Ok(ExitStatus::Signaled(15)) => {
// Signal 15 == SIGTERM
Err(CmdError::Hung(format_failure(
command,
"Killed by SIGTERM",
&stdout_result,
&stderr_result,
)))
}
Ok(_) => Err(CmdError::Failed(format_failure(
Ok(x) => Err(CmdError::Failed(format_failure(
command,
format!("Unspecified other exit status {:?}", x).as_str(),
&stdout_result,
&stderr_result,
))),
Err(_) => Err(CmdError::InternalError(format_failure(
Err(e) => Err(CmdError::InternalError(format_failure(
command,
format!("Internal error {:?}", e).as_str(),
&stdout_result,
&stderr_result,
))),
}
}

fn format_failure(command: &str, stdout: &str, stderr: &str) -> String {
fn format_failure(command: &str, root_cause: &str, stdout: &str, stderr: &str) -> String {
if !stdout.is_empty() {
if !stderr.is_empty() {
format!("COMMAND:\n{command}\nSTDOUT:\n{stdout}\nSTDERR:\n{stderr}")
format!("COMMAND:\n{command}\nROOT CAUSE:\n{root_cause}\nSTDOUT:\n{stdout}\nSTDERR:\n{stderr}")
} else {
format!("COMMAND:\n{command}\nSTDOUT:\n{stdout}")
format!("COMMAND:\n{command}\nROOT CAUSE:\n{root_cause}\nSTDOUT:\n{stdout}")
}
} else if !stderr.is_empty() {
format!("COMMAND:\n{command}\nSTDERR:\n{stderr}")
format!("COMMAND:\n{command}\nROOT CAUSE:\n{root_cause}\nSTDERR:\n{stderr}")
} else {
format!("COMMAND:\n{command}")
format!("COMMAND:\n{command}\nROOT CAUSE:\n{root_cause}")
}
}

Expand Down
Loading