| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """ | 5 """ |
| 6 Exception classes raised by AdbWrapper and DeviceUtils. | 6 Exception classes raised by AdbWrapper and DeviceUtils. |
| 7 """ | 7 """ |
| 8 | 8 |
| 9 from pylib import cmd_helper | 9 from pylib import cmd_helper |
| 10 | 10 |
| 11 | 11 |
| 12 class BaseError(Exception): | 12 class BaseError(Exception): |
| 13 """Base exception for all device and command errors.""" | 13 """Base exception for all device and command errors.""" |
| 14 pass | 14 pass |
| 15 | 15 |
| 16 | 16 |
| 17 class CommandFailedError(BaseError): | 17 class CommandFailedError(BaseError): |
| 18 """Exception for command failures.""" | 18 """Exception for command failures.""" |
| 19 | 19 |
| 20 def __init__(self, message, device_serial=None): | 20 def __init__(self, message, device_serial=None): |
| 21 if device_serial is not None: | 21 if device_serial is not None: |
| 22 message = '(device: %s) %s' % (device_serial, message) | 22 message = '(device: %s) %s' % (device_serial, message) |
| 23 self.device_serial = device_serial | 23 self.device_serial = device_serial |
| 24 super(CommandFailedError, self).__init__(message) | 24 super(CommandFailedError, self).__init__(message) |
| 25 | 25 |
| 26 | 26 |
| 27 class AdbCommandFailedError(CommandFailedError): | 27 class AdbCommandFailedError(CommandFailedError): |
| 28 """Exception for adb command failures.""" | 28 """Exception for adb command failures.""" |
| 29 | 29 |
| 30 def __init__(self, cmd, output, status=None, device_serial=None): | 30 def __init__(self, args, output, status=None, device_serial=None, |
| 31 self.cmd = cmd | 31 message=None): |
| 32 self.args = args |
| 32 self.output = output | 33 self.output = output |
| 33 self.status = status | 34 self.status = status |
| 34 message = [] | 35 if not message: |
| 35 if self.cmd[0] == 'shell': | 36 adb_cmd = ' '.join(cmd_helper.SingleQuote(arg) for arg in self.args) |
| 36 assert len(self.cmd) == 2 | 37 message = ['adb %s: failed ' % adb_cmd] |
| 37 message.append('adb shell command %r failed with' % self.cmd[1]) | 38 if status: |
| 39 message.append('with exit status %s ' % self.status) |
| 40 if output: |
| 41 message.append('and output:\n') |
| 42 message.extend('- %s\n' % line for line in output.splitlines()) |
| 43 else: |
| 44 message.append('and no output.') |
| 45 message = ''.join(message) |
| 46 super(AdbCommandFailedError, self).__init__(message, device_serial) |
| 47 |
| 48 |
| 49 class AdbShellCommandFailedError(AdbCommandFailedError): |
| 50 """Exception for shell command failures run via adb.""" |
| 51 |
| 52 def __init__(self, command, output, status, device_serial=None): |
| 53 self.command = command |
| 54 message = ['shell command run via adb failed on the device:\n', |
| 55 ' command: %s\n' % command] |
| 56 message.append(' exit status: %s\n' % status) |
| 57 if output: |
| 58 message.append(' output:\n') |
| 59 message.extend(' - %s\n' % line for line in output.splitlines()) |
| 38 else: | 60 else: |
| 39 command = ' '.join(cmd_helper.SingleQuote(arg) for arg in self.cmd) | 61 message.append(" output: ''\n") |
| 40 message.append('adb command %r failed with' % command) | 62 message = ''.join(message) |
| 41 if status: | 63 super(AdbShellCommandFailedError, self).__init__( |
| 42 message.append(' exit status %d and' % self.status) | 64 ['shell', command], output, status, device_serial, message) |
| 43 if output: | |
| 44 message.append(' output:\n') | |
| 45 message.extend('> %s\n' % line for line in output.splitlines()) | |
| 46 else: | |
| 47 message.append(' no output') | |
| 48 super(AdbCommandFailedError, self).__init__(''.join(message), device_serial) | |
| 49 | 65 |
| 50 | 66 |
| 51 class CommandTimeoutError(BaseError): | 67 class CommandTimeoutError(BaseError): |
| 52 """Exception for command timeouts.""" | 68 """Exception for command timeouts.""" |
| 53 pass | 69 pass |
| 54 | 70 |
| 55 | 71 |
| 56 class DeviceUnreachableError(BaseError): | 72 class DeviceUnreachableError(BaseError): |
| 57 """Exception for device unreachable failures.""" | 73 """Exception for device unreachable failures.""" |
| 58 pass | 74 pass |
| 59 | 75 |
| 60 | 76 |
| 61 class NoDevicesError(BaseError): | 77 class NoDevicesError(BaseError): |
| 62 """Exception for having no devices attached.""" | 78 """Exception for having no devices attached.""" |
| 63 | 79 |
| 64 def __init__(self): | 80 def __init__(self): |
| 65 super(NoDevicesError, self).__init__('No devices attached.') | 81 super(NoDevicesError, self).__init__('No devices attached.') |
| 66 | 82 |
| OLD | NEW |