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 |
| 10 |
| 11 |
9 class BaseError(Exception): | 12 class BaseError(Exception): |
10 """Base exception for all device and command errors.""" | 13 """Base exception for all device and command errors.""" |
11 pass | 14 pass |
12 | 15 |
13 | 16 |
14 class CommandFailedError(BaseError): | 17 class CommandFailedError(BaseError): |
15 """Exception for command failures.""" | 18 """Exception for command failures.""" |
16 | 19 |
17 def __init__(self, msg, device=None): | 20 def __init__(self, message, device_serial=None): |
18 super(CommandFailedError, self).__init__( | 21 if device_serial is not None: |
19 '%s%s' % ('(device: %s) ' % device if device else '', msg)) | 22 message = '(device: %s) %s' % (device_serial, message) |
| 23 self.device_serial = device_serial |
| 24 super(CommandFailedError, self).__init__(message) |
20 | 25 |
21 | 26 |
22 class AdbCommandFailedError(CommandFailedError): | 27 class AdbCommandFailedError(CommandFailedError): |
23 """Exception for adb command failures.""" | 28 """Exception for adb command failures.""" |
24 | 29 |
25 def __init__(self, cmd, msg, device=None): | 30 def __init__(self, cmd, output, status=None, device_serial=None): |
26 super(AdbCommandFailedError, self).__init__( | 31 self.cmd = cmd |
27 'adb command %r failed with message: %s' % (' '.join(cmd), msg), | |
28 device=device) | |
29 | |
30 | |
31 class AdbShellCommandFailedError(AdbCommandFailedError): | |
32 """Exception for adb shell command failing with non-zero return code.""" | |
33 | |
34 def __init__(self, cmd, return_code, output, device=None): | |
35 super(AdbShellCommandFailedError, self).__init__( | |
36 ['shell'], | |
37 'command %r on device failed with return code %d and output %r' | |
38 % (cmd, return_code, output), | |
39 device=device) | |
40 self.return_code = return_code | |
41 self.output = output | 32 self.output = output |
| 33 self.status = status |
| 34 message = [] |
| 35 if self.cmd[0] == 'shell': |
| 36 assert len(self.cmd) == 2 |
| 37 message.append('adb shell command %r failed with' % self.cmd[1]) |
| 38 else: |
| 39 command = ' '.join(cmd_helper.SingleQuote(arg) for arg in self.cmd) |
| 40 message.append('adb command %r failed with' % command) |
| 41 if status: |
| 42 message.append(' exit status %d and' % self.status) |
| 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) |
42 | 49 |
43 | 50 |
44 class CommandTimeoutError(BaseError): | 51 class CommandTimeoutError(BaseError): |
45 """Exception for command timeouts.""" | 52 """Exception for command timeouts.""" |
46 pass | 53 pass |
47 | 54 |
48 | 55 |
49 class DeviceUnreachableError(BaseError): | 56 class DeviceUnreachableError(BaseError): |
50 """Exception for device unreachable failures.""" | 57 """Exception for device unreachable failures.""" |
51 pass | 58 pass |
52 | 59 |
53 | 60 |
54 class NoDevicesError(BaseError): | 61 class NoDevicesError(BaseError): |
55 """Exception for having no devices attached.""" | 62 """Exception for having no devices attached.""" |
56 | 63 |
57 def __init__(self): | 64 def __init__(self): |
58 super(NoDevicesError, self).__init__('No devices attached.') | 65 super(NoDevicesError, self).__init__('No devices attached.') |
59 | 66 |
OLD | NEW |