From af41a83b76849f714e6cc256483418bde7d5de69 Mon Sep 17 00:00:00 2001 From: Rishabh Dave Date: Wed, 9 Jun 2021 14:07:28 +0530 Subject: [PATCH] exceptions: change error message for CommandFailedError For the convenience of user, print the command that led to CommandFailedError in error message as a str than as a list or a tuple. This makes it more readable and enables the user to copy and use the command without any hassles. This commit also uses the opportunity to rename the variable "command" to "cmd" and "self.command" to "self.cmd" in CommandFailedError for convenience and so that it's easy to keep statements under 80 characters. Signed-off-by: Rishabh Dave --- teuthology/exceptions.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/teuthology/exceptions.py b/teuthology/exceptions.py index a33cec0415..40be08dc0d 100644 --- a/teuthology/exceptions.py +++ b/teuthology/exceptions.py @@ -1,3 +1,8 @@ +from re import sub + +from teuthology.orchestra.run_helper import Raw + + class BranchNotFoundError(ValueError): def __init__(self, branch, repo=None): self.branch = branch @@ -50,13 +55,22 @@ class CommandFailedError(Exception): """ Exception thrown on command failure """ - def __init__(self, command, exitstatus, node=None, label=None): - self.command = command + def __init__(self, cmd, exitstatus, node=None, label=None): + self.cmd = cmd self.exitstatus = exitstatus self.node = node self.label = label def __str__(self): + if isinstance(self.cmd, (list, tuple)): + cmd = [str(Raw) if isinstance(x, Raw) else x + for x in self.cmd] + cmd = ' '.join(self.cmd) + elif isinstance(self.cmd, str): + cmd = sub(r'Raw\([\'"](.*)[\'"]\)', r'\1', self.cmd) + else: + raise RuntimeError('variable "self.cmd" is not str, list or tuple') + prefix = "Command failed" if self.label: prefix += " ({label})".format(label=self.label) @@ -64,7 +78,7 @@ def __str__(self): prefix += " on {node}".format(node=self.node) return "{prefix} with status {status}: {cmd!r}".format( status=self.exitstatus, - cmd=self.command, + cmd=cmd, prefix=prefix, ) @@ -74,7 +88,7 @@ def fingerprint(self): Used by sentry instead of grouping by backtrace. """ return [ - self.label or self.command, + self.label or self.cmd, 'exit status {}'.format(self.exitstatus), '{{ type }}', ]