Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better exception messages for InvalidCommandError #122

Merged
merged 2 commits into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions adb_shell/adb_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

from . import constants
from . import exceptions
from .adb_message import AdbMessage, checksum, unpack
from .adb_message import AdbMessage, checksum, int_to_cmd, unpack
from .transport.base_transport import BaseTransport
from .transport.tcp_transport import TcpTransport
from .hidden_helpers import DeviceFile, _AdbTransactionInfo, _FileSyncTransactionInfo, get_banner, get_files_to_push
Expand Down Expand Up @@ -755,13 +755,13 @@ def _read(self, expected_cmds, adb_info):
command = constants.WIRE_TO_ID.get(cmd)

if not command:
raise exceptions.InvalidCommandError('Unknown command: %x' % cmd, cmd, (arg0, arg1))
raise exceptions.InvalidCommandError("Unknown command: %d = '%s' (arg0 = %d, arg1 = %d, msg = '%s')" % (cmd, int_to_cmd(cmd), arg0, arg1, msg))

if command in expected_cmds:
break

if time.time() - start > adb_info.read_timeout_s:
raise exceptions.InvalidCommandError('Never got one of the expected responses (%s)' % expected_cmds, cmd, (adb_info.transport_timeout_s, adb_info.read_timeout_s))
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %d, read_timeout_s = %d" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))

if data_length > 0:
data = bytearray()
Expand Down Expand Up @@ -825,7 +825,7 @@ def _read_until(self, expected_cmds, adb_info):
break

if time.time() - start > adb_info.read_timeout_s:
raise exceptions.InvalidCommandError('Never got one of the expected responses (%s)' % expected_cmds, cmd, (adb_info.transport_timeout_s, adb_info.read_timeout_s))
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %d, read_timeout_s = %d" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))

# Ignore CLSE responses to previous commands
# https://github.com/JeffLIrion/adb_shell/pull/14
Expand Down
8 changes: 4 additions & 4 deletions adb_shell/adb_device_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

from . import constants
from . import exceptions
from .adb_message import AdbMessage, checksum, unpack
from .adb_message import AdbMessage, checksum, int_to_cmd, unpack
from .transport.base_transport_async import BaseTransportAsync
from .transport.tcp_transport_async import TcpTransportAsync
from .hidden_helpers import DeviceFile, _AdbTransactionInfo, _FileSyncTransactionInfo, get_banner, get_files_to_push
Expand Down Expand Up @@ -750,13 +750,13 @@ async def _read(self, expected_cmds, adb_info):
command = constants.WIRE_TO_ID.get(cmd)

if not command:
raise exceptions.InvalidCommandError('Unknown command: %x' % cmd, cmd, (arg0, arg1))
raise exceptions.InvalidCommandError("Unknown command: %d = '%s' (arg0 = %d, arg1 = %d, msg = '%s')" % (cmd, int_to_cmd(cmd), arg0, arg1, msg))

if command in expected_cmds:
break

if time.time() - start > adb_info.read_timeout_s:
raise exceptions.InvalidCommandError('Never got one of the expected responses (%s)' % expected_cmds, cmd, (adb_info.transport_timeout_s, adb_info.read_timeout_s))
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %d, read_timeout_s = %d" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))

if data_length > 0:
data = bytearray()
Expand Down Expand Up @@ -820,7 +820,7 @@ async def _read_until(self, expected_cmds, adb_info):
break

if time.time() - start > adb_info.read_timeout_s:
raise exceptions.InvalidCommandError('Never got one of the expected responses (%s)' % expected_cmds, cmd, (adb_info.transport_timeout_s, adb_info.read_timeout_s))
raise exceptions.InvalidCommandError("Never got one of the expected responses: %s (transport_timeout_s = %d, read_timeout_s = %d" % (expected_cmds, adb_info.transport_timeout_s, adb_info.read_timeout_s))

# Ignore CLSE responses to previous commands
# https://github.com/JeffLIrion/adb_shell/pull/14
Expand Down
18 changes: 18 additions & 0 deletions adb_shell/adb_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* :meth:`AdbMessage.pack`

* :func:`checksum`
* :func:`int_to_cmd`
* :func:`unpack`

"""
Expand Down Expand Up @@ -71,6 +72,23 @@ def checksum(data):
return total & 0xFFFFFFFF


def int_to_cmd(n):
"""Convert from an integer (4 bytes) to an ADB command.

Parameters
----------
n : int
The integer that will be converted to an ADB command

Returns
-------
str
The ADB command (e.g., ``'CNXN'``)

"""
return ''.join(chr((n >> (i * 8)) % 256) for i in range(4)).encode('utf-8')


def unpack(message):
"""Unpack a received ADB message.

Expand Down
7 changes: 0 additions & 7 deletions adb_shell/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
"""


from . import constants


class AdbCommandFailureException(Exception):
"""A ``b'FAIL'`` packet was received.

Expand Down Expand Up @@ -69,10 +66,6 @@ class InvalidCommandError(Exception):
"""Got an invalid command.

"""
def __init__(self, message, response_header, response_data):
if response_header == constants.FAIL:
message = 'Command failed, device said so. (%s)' % message
super(InvalidCommandError, self).__init__(message, response_header, response_data)


class InvalidTransportError(Exception):
Expand Down
8 changes: 2 additions & 6 deletions tests/test_adb_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@

from adb_shell import constants
from adb_shell.adb_device import AdbDevice
from adb_shell.adb_message import AdbMessage, checksum, unpack


def from_int(n):
return ''.join(chr((n >> (i * 8)) % 256) for i in range(4)).encode('utf-8')
from adb_shell.adb_message import AdbMessage, checksum, int_to_cmd, unpack


class TestAdbMessage(unittest.TestCase):
Expand All @@ -31,4 +27,4 @@ def test_unpack_error(self):

def test_constants(self):
for key, val in constants.ID_TO_WIRE.items():
self.assertEqual(key, from_int(val))
self.assertEqual(key, int_to_cmd(val))
13 changes: 0 additions & 13 deletions tests/test_exceptions.py

This file was deleted.