From: Michael Tremer Date: Tue, 25 Oct 2022 09:33:07 +0000 (+0000) Subject: backend: command: Evaluate return code and raise errors X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=10e8378ba846aa320394e58c5c992a382c8106e8;p=pbs.git backend: command: Evaluate return code and raise errors Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/__init__.py b/src/buildservice/__init__.py index d570e263..cfbd6fcc 100644 --- a/src/buildservice/__init__.py +++ b/src/buildservice/__init__.py @@ -194,7 +194,7 @@ class Backend(object): *args, stdin=asyncio.subprocess.DEVNULL, stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.STDOUT, + stderr=asyncio.subprocess.PIPE, **kwargs, ) @@ -218,8 +218,22 @@ class Backend(object): stdout.append(line) # Wait until the process has finished - await process.wait() + returncode = await process.wait() + # Check the return code + if returncode: + # Fetch any output from the standard error output + stderr = await process.stderr.read() + stderr = stderr.decode() + + # Log the error + log.error("Error running command: %s (code=%s)" % (" ".join(args), returncode)) + if stderr: + log.error(stderr) + + raise CommandExecutionError(returncode, stderr) + + # Return output if requested if return_output: return "\n".join(stdout) diff --git a/src/buildservice/errors.py b/src/buildservice/errors.py index 944939f2..66314ea9 100644 --- a/src/buildservice/errors.py +++ b/src/buildservice/errors.py @@ -1,5 +1,14 @@ +class CommandExecutionError(Exception): + """ + Raised when the executed command at Backend.command() returned an error + """ + def __init__(self, returncode, stderr=None): + self.returncode = returncode + self.stderr = stderr + + class NoSuchDistroError(Exception): """ Raised when a certain distribution could not be found