Skip to content

Commit

Permalink
exceptions: change error message for CommandFailedError
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rishabh-d-dave committed Jun 15, 2021
1 parent 6bd5798 commit 1cc8aa2
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions teuthology/exceptions.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -50,21 +55,30 @@ 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)
if self.node:
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,
)

Expand All @@ -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 }}',
]
Expand Down

0 comments on commit 1cc8aa2

Please sign in to comment.