From 3dbc83435e4b56281f1d88ff89393c1d368ab62d Mon Sep 17 00:00:00 2001 From: Tao Guo Date: Fri, 20 Oct 2023 16:07:22 +0000 Subject: [PATCH] Better error report for running commands --- src/child.rs | 6 +++--- src/process.rs | 27 ++++++++++++--------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/child.rs b/src/child.rs index 45bd2f6..4b857b0 100644 --- a/src/child.rs +++ b/src/child.rs @@ -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"), )); } } @@ -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:?}"), )) } } @@ -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); } } } diff --git a/src/process.rs b/src/process.rs index 5cbdb0c..48060e0 100644 --- a/src/process.rs +++ b/src/process.rs @@ -181,7 +181,7 @@ impl Cmds { } fn spawn(&mut self, current_dir: &mut PathBuf, with_output: bool) -> Result { - 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} ..."); @@ -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); } @@ -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::>() - .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::>() + .join(" ") } fn gen_command(mut self) -> (bool, Self) {