Skip to content

Commit

Permalink
Merge pull request #150 from PyAr/better-error-report
Browse files Browse the repository at this point in the history
Better error reporting when executing a subprocess.
  • Loading branch information
gilgamezh committed Dec 20, 2015
2 parents b6dd0b4 + e72bf24 commit c1902e1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions fades/envbuilder.py
Expand Up @@ -76,6 +76,9 @@ def create_with_virtualenv(self, interpreter, virtualenv_options):
logger.error('Virtualenv is not installed. It is needed to create a virtualenv with '
'a different python version than fades (got {})'.format(error))
exit()
except helpers.ExecutionError as error:
error.dump_to_log(logger)
exit()
except Exception as error:
logger.exception("Error creating virtualenv: %s", error)
exit()
Expand Down
21 changes: 19 additions & 2 deletions fades/helpers.py
Expand Up @@ -31,6 +31,23 @@
print(json.dumps(d))
"""

STDOUT_LOG_PREFIX = ":: "


class ExecutionError(Exception):
"""Execution of subprocess ended not in 0."""
def __init__(self, retcode, cmd, collected_stdout):
self._retcode = retcode
self._cmd = cmd
self._collected_stdout = collected_stdout
super().__init__()

def dump_to_log(self, logger):
"""Send the cmd info and collected stdout to logger."""
logger.error("Execution ended in %s for cmd %s", self._retcode, self._cmd)
for line in self._collected_stdout:
logger.error(STDOUT_LOG_PREFIX + line)


def logged_exec(cmd):
"""Execute a command, redirecting the output to the log."""
Expand All @@ -41,10 +58,10 @@ def logged_exec(cmd):
for line in p.stdout:
line = line[:-1].decode("utf8")
stdout.append(line)
logger.debug(":: " + line)
logger.debug(STDOUT_LOG_PREFIX + line)
retcode = p.wait()
if retcode:
raise subprocess.CalledProcessError(retcode, cmd)
raise ExecutionError(retcode, cmd, stdout)
return stdout


Expand Down
3 changes: 3 additions & 0 deletions fades/pipmanager.py
Expand Up @@ -56,6 +56,9 @@ def install(self, dependency):
logger.info("Installing dependency: %s", str_dep)
try:
helpers.logged_exec(args)
except helpers.ExecutionError as error:
error.dump_to_log(logger)
exit()
except Exception as error:
logger.exception("Error installing %s: %s", str_dep, error)
exit()
Expand Down

0 comments on commit c1902e1

Please sign in to comment.