sapling/eden/integration/lib/error.py
Andres Suarez fbdb46f5cb Tidy up license headers
Reviewed By: chadaustin

Differential Revision: D17872966

fbshipit-source-id: cd60a364a2146f0dadbeca693b1d4a5d7c97ff63
2019-10-11 05:28:23 -07:00

42 lines
1.2 KiB
Python

#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2.
import shlex
import subprocess
class CommandError(subprocess.CalledProcessError):
"""
A wrapper around subprocess.CalledProcessError that also includes
includes the process's stderr when converted to a string.
"""
def __init__(self, orig: subprocess.CalledProcessError) -> None:
super().__init__(
orig.returncode, orig.cmd, output=orig.output, stderr=orig.stderr
)
def __str__(self) -> str:
if not self.stderr:
return super().__str__()
cmd_str = " ".join(shlex.quote(arg) for arg in self.cmd)
stderr_str = self.stderr
if isinstance(self.stderr, bytes):
stderr_str = self.stderr.decode("utf-8", errors="replace")
# Indent the stderr output just to help indicate where it starts
# and ends in the test output.
stderr_str = stderr_str.replace("\n", "\n ")
msg = "Command [%s] failed with status %s\nstderr:\n %s" % (
cmd_str,
self.returncode,
stderr_str,
)
return msg