fix: Move try/except into run_interactive_subprocess and print tool_error on non-zero exit status

This commit is contained in:
Paul Gauthier (aider) 2024-08-21 15:32:43 -07:00
parent 26edd52d9d
commit f5a546834d

View file

@ -28,13 +28,22 @@ class EditBlockCoder(Coder):
return edits return edits
def run_interactive_subprocess(self, command): def run_interactive_subprocess(self, command):
return subprocess.run( try:
command, result = subprocess.run(
text=True, command,
shell=True, text=True,
encoding=self.io.encoding, shell=True,
errors="replace", encoding=self.io.encoding,
) errors="replace",
capture_output=True,
)
if result.returncode != 0:
self.io.tool_error(f"Command '{command}' exited with status {result.returncode}")
self.io.tool_error(result.stderr)
return result
except Exception as e:
self.io.tool_error(f"Error running command '{command}': {str(e)}")
return None
def handle_shell_commands(self, commands_str): def handle_shell_commands(self, commands_str):
commands = commands_str.strip().splitlines() commands = commands_str.strip().splitlines()
@ -54,10 +63,9 @@ class EditBlockCoder(Coder):
self.io.tool_output(f"Running {command}") self.io.tool_output(f"Running {command}")
# Add the command to input history # Add the command to input history
self.io.add_to_input_history(f"/run {command.strip()}") self.io.add_to_input_history(f"/run {command.strip()}")
try: result = self.run_interactive_subprocess(command)
self.run_interactive_subprocess(command) if result and result.stdout:
except Exception as e: self.io.tool_output(result.stdout)
self.io.tool_error(f"Error running command '{command}': {str(e)}")
def apply_edits(self, edits): def apply_edits(self, edits):
failed = [] failed = []