Skip to content

Commit

Permalink
Better error report for running commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tao-guo committed Oct 20, 2023
1 parent e089e56 commit 3dbc834
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl CmdChildHandle {
if !status.success() {
return Err(Self::status_to_io_error(
status,
&format!("Running {} exited with error", cmd),
&format!("Running [{cmd}] exited with error"),
));
}
}
Expand All @@ -236,7 +236,7 @@ impl CmdChildHandle {
Err(e) => {
return Err(Error::new(
ErrorKind::Other,
format!("Running {} thread joined with error: {:?}", cmd, e),
format!("Running [{cmd}] thread joined with error: {e:?}"),
))
}
}
Expand Down Expand Up @@ -308,7 +308,7 @@ impl Drop for StderrLogging {
if let Some(thread) = self.thread.take() {
if let Err(e) = thread.join() {
let _ = try_init_default_logger();
warn!("{} logging thread exited with error: {:?}", self.cmd, e);
warn!("[{}] logging thread exited with error: {:?}", self.cmd, e);
}
}
}
Expand Down
27 changes: 12 additions & 15 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Cmds {
}

fn spawn(&mut self, current_dir: &mut PathBuf, with_output: bool) -> Result<CmdChildren> {
let full_cmds = &self.full_cmds;
let full_cmds = format!("[{}]", self.full_cmds);
if debug_enabled() {
let _ = try_init_default_logger();
debug!("Running {full_cmds} ...");
Expand All @@ -196,17 +196,17 @@ impl Cmds {
if i != len - 1 {
// not the last, update redirects
let (pipe_reader, pipe_writer) =
os_pipe::pipe().map_err(|e| new_cmd_io_error(&e, full_cmds))?;
os_pipe::pipe().map_err(|e| new_cmd_io_error(&e, &full_cmds))?;
cmd.setup_redirects(&mut prev_pipe_in, Some(pipe_writer), with_output)
.map_err(|e| new_cmd_io_error(&e, full_cmds))?;
.map_err(|e| new_cmd_io_error(&e, &full_cmds))?;
prev_pipe_in = Some(pipe_reader);
} else {
cmd.setup_redirects(&mut prev_pipe_in, None, with_output)
.map_err(|e| new_cmd_io_error(&e, full_cmds))?;
.map_err(|e| new_cmd_io_error(&e, &full_cmds))?;
}
let child = cmd
.spawn(current_dir, with_output)
.map_err(|e| new_cmd_io_error(&e, full_cmds))?;
.map_err(|e| new_cmd_io_error(&e, &full_cmds))?;
children.push(child);
}

Expand Down Expand Up @@ -336,16 +336,13 @@ impl Cmd {
}

fn cmd_str(&self) -> String {
format!(
"[{}]",
self.vars
.iter()
.map(|(k, v)| format!("{k}={v:?}"))
.chain(self.args.iter().map(|s| format!("{s:?}")))
.chain(self.redirects.iter().map(|r| format!("{r:?}")))
.collect::<Vec<String>>()
.join(" ")
)
self.vars
.iter()
.map(|(k, v)| format!("{k}={v:?}"))
.chain(self.args.iter().map(|s| format!("{s:?}")))
.chain(self.redirects.iter().map(|r| format!("{r:?}")))
.collect::<Vec<String>>()
.join(" ")
}

fn gen_command(mut self) -> (bool, Self) {
Expand Down

0 comments on commit 3dbc834

Please sign in to comment.