43 lines
1.2 KiB
Rust
43 lines
1.2 KiB
Rust
use std::{os::unix::process::ExitStatusExt, process::Output};
|
|
|
|
use log::error;
|
|
|
|
use crate::hlwm::command::CommandError;
|
|
|
|
pub fn check_status(output: &Output) -> Result<(), CommandError> {
|
|
let exit_status = output.status;
|
|
if let Some(code) = exit_status.code() {
|
|
if code == 0 {
|
|
return Ok(());
|
|
} else {
|
|
let output = String::from_utf8(output.stdout.clone())
|
|
.unwrap_or_default()
|
|
.trim()
|
|
.to_string();
|
|
error!("command failed with error code [{code}]");
|
|
if !output.is_empty() {
|
|
error!("command output: {output}");
|
|
}
|
|
return Err(CommandError::StatusCode(
|
|
code,
|
|
if output.is_empty() {
|
|
None
|
|
} else {
|
|
Some(output)
|
|
},
|
|
));
|
|
}
|
|
}
|
|
if let Some(signal) = exit_status.signal() {
|
|
return Err(CommandError::KilledBySignal {
|
|
signal,
|
|
core_dumped: exit_status.core_dumped(),
|
|
});
|
|
}
|
|
if let Some(signal) = exit_status.stopped_signal() {
|
|
return Err(CommandError::StoppedBySignal(signal));
|
|
}
|
|
|
|
Err(CommandError::OtherExitStatus(exit_status))
|
|
}
|