Skip to content

Commit

Permalink
Clearer AdbCommandFailedError messages
Browse files Browse the repository at this point in the history
Makes error messages clearer by making it more explicit that
the failure happened when running a specific command on a
device; and, although adb is the means by which commands are
run, it is itself not suspected of causing the failure.

BUG=442252

Review URL: https://codereview.chromium.org/800383002

Cr-Commit-Position: refs/heads/master@{#309838}
  • Loading branch information
perezju authored and Commit bot committed Jan 2, 2015
1 parent 1254c64 commit e07ca57
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
8 changes: 4 additions & 4 deletions build/android/pylib/device/adb_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ def Shell(self, command, expect_status=0, timeout=_DEFAULT_TIMEOUT,
status = int(output[output_end+1:])
except ValueError:
logging.warning('exit status of shell command %r missing.', command)
raise device_errors.AdbCommandFailedError(
args, output, device_serial=self._device_serial)
raise device_errors.AdbShellCommandFailedError(
command, output, status=None, device_serial=self._device_serial)
output = output[:output_end]
if status != expect_status:
raise device_errors.AdbCommandFailedError(
args, output, status, self._device_serial)
raise device_errors.AdbShellCommandFailedError(
command, output, status=status, device_serial=self._device_serial)
return output

def Ls(self, path, timeout=_DEFAULT_TIMEOUT, retries=_DEFAULT_RETRIES):
Expand Down
46 changes: 31 additions & 15 deletions build/android/pylib/device/device_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,41 @@ def __init__(self, message, device_serial=None):
class AdbCommandFailedError(CommandFailedError):
"""Exception for adb command failures."""

def __init__(self, cmd, output, status=None, device_serial=None):
self.cmd = cmd
def __init__(self, args, output, status=None, device_serial=None,
message=None):
self.args = args
self.output = output
self.status = status
message = []
if self.cmd[0] == 'shell':
assert len(self.cmd) == 2
message.append('adb shell command %r failed with' % self.cmd[1])
else:
command = ' '.join(cmd_helper.SingleQuote(arg) for arg in self.cmd)
message.append('adb command %r failed with' % command)
if status:
message.append(' exit status %d and' % self.status)
if not message:
adb_cmd = ' '.join(cmd_helper.SingleQuote(arg) for arg in self.args)
message = ['adb %s: failed ' % adb_cmd]
if status:
message.append('with exit status %s ' % self.status)
if output:
message.append('and output:\n')
message.extend('- %s\n' % line for line in output.splitlines())
else:
message.append('and no output.')
message = ''.join(message)
super(AdbCommandFailedError, self).__init__(message, device_serial)


class AdbShellCommandFailedError(AdbCommandFailedError):
"""Exception for shell command failures run via adb."""

def __init__(self, command, output, status, device_serial=None):
self.command = command
message = ['shell command run via adb failed on the device:\n',
' command: %s\n' % command]
message.append(' exit status: %s\n' % status)
if output:
message.append(' output:\n')
message.extend('> %s\n' % line for line in output.splitlines())
message.append(' output:\n')
message.extend(' - %s\n' % line for line in output.splitlines())
else:
message.append(' no output')
super(AdbCommandFailedError, self).__init__(''.join(message), device_serial)
message.append(" output: ''\n")
message = ''.join(message)
super(AdbShellCommandFailedError, self).__init__(
['shell', command], output, status, device_serial, message)


class CommandTimeoutError(BaseError):
Expand Down
6 changes: 3 additions & 3 deletions build/android/pylib/device/device_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,10 @@ def setUp(self):
self.adb, default_timeout=10, default_retries=0)
self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial'])

def ShellError(self, output=None, exit_code=1):
def ShellError(self, output=None, status=1):
def action(cmd, *args, **kwargs):
raise device_errors.AdbCommandFailedError(
cmd, output, exit_code, str(self.device))
raise device_errors.AdbShellCommandFailedError(
cmd, output, status, str(self.device))
if output is None:
output = 'Permission denied\n'
return action
Expand Down

0 comments on commit e07ca57

Please sign in to comment.