From 57c9e9dc906da0dd4b5d34faeec2feee392a8b9c Mon Sep 17 00:00:00 2001 From: Michael Schwarz Date: Mon, 7 Sep 2015 00:53:22 +0200 Subject: Prevent Python stack trace when an external command failed. This catches the OSError thrown by the subprocess module and wraps it so that in the end only an error message is printed, explaining which command failed. --- support/lib/util.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'support/lib') diff --git a/support/lib/util.py b/support/lib/util.py index 3a852f4..915e33c 100644 --- a/support/lib/util.py +++ b/support/lib/util.py @@ -2,7 +2,8 @@ import contextlib, subprocess, tempfile, shutil, re, os class UserError(Exception): - pass + def __init__(self, message, *args): + super(UserError, self).__init__(message.format(*args)) def rename_atomic(source_path, target_path): @@ -43,11 +44,14 @@ def command(args, remove_env = [], set_env = { }): for k, v in set_env.items(): env[k] = v - process = subprocess.Popen(args, env = env) - process.wait() + try: + process = subprocess.Popen(args, env = env) + process.wait() + except OSError as e: + raise UserError('Error running {}: {}', args[0], e) if process.returncode: - raise UserError('Command failed: {}'.format(' '.join(args))) + raise UserError('Command failed: {}', ' '.join(args)) def bash_escape_string(string): -- cgit