Skip to content

Commit

Permalink
refactor: improve way of handling command errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bra-i-am committed Apr 2, 2024
1 parent 7cddc34 commit 9d7cc1b
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions tutordistro/distro/extra_commands/infrastructure/tutor_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,15 @@ def run_command(self, command: str):
command (str): Tutor command.
"""
try:
with subprocess.Popen(
process = subprocess.run(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
capture_output=True,
executable="/bin/bash",
) as process:
output, error = process.communicate()
# If a command failed
if process.returncode != 0:
raise CommandError(
f'Error running command "{command}".\n{error.decode()}'
)
# This print is left on purpose to show the command output
print(output.decode())

except subprocess.SubprocessError as error:
raise CommandError(f"Error running command {command}: {error}") from error
)
# This print is left on purpose to show the command output
print(process.stdout.decode())

except subprocess.CalledProcessError as error:
raise CommandError(f"Error running command '{error.cmd}':\n{error.stderr.decode()}") from error

0 comments on commit 9d7cc1b

Please sign in to comment.