Improve exception messages

This commit is contained in:
Andrew Kay
2020-05-12 07:47:51 -05:00
parent 62a054ae12
commit 3aacf2477b
2 changed files with 16 additions and 15 deletions

View File

@@ -66,7 +66,7 @@ class PowerOnResetCompletePollResponse(PollResponse):
def __init__(self, value):
if not PollResponse.is_power_on_reset_complete(value):
raise ValueError('Invalid POR poll response')
raise ValueError(f'Invalid POR poll response: {value}')
super().__init__(value)
@@ -75,7 +75,7 @@ class KeystrokePollResponse(PollResponse):
def __init__(self, value):
if not PollResponse.is_keystroke(value):
raise ValueError('Invalid keystroke poll response')
raise ValueError(f'Invalid keystroke poll response: {value}')
super().__init__(value)
@@ -122,7 +122,7 @@ class TerminalId:
model = (value & 0x0e) >> 1
if model not in TerminalId._MODEL_MAP:
raise ValueError('Invalid model')
raise ValueError(f'Invalid model: {model}')
self.model = TerminalId._MODEL_MAP[model]
self.keyboard = (value & 0xf0) >> 4
@@ -131,7 +131,7 @@ class TerminalId:
self.model = None
self.keyboard = None
else:
raise ValueError('Invalid terminal identifier')
raise ValueError(f'Invalid terminal identifier: {value}')
def __repr__(self):
return (f'<TerminalId type={self.type.name}, model={self.model}, '
@@ -337,7 +337,7 @@ def is_command_word(word):
def unpack_command_word(word):
"""Unpack a 10-bit command word."""
if not is_command_word(word):
raise ProtocolError('Word does not have command bit set')
raise ProtocolError(f'Word does not have command bit set: {word}')
command = (word >> 2) & 0x1f
@@ -356,13 +356,13 @@ def is_data_word(word):
def unpack_data_word(word, check_parity=False):
"""Unpack the data byte from a 10-bit data word."""
if not is_data_word(word):
raise ProtocolError('Word does not have data bit set')
raise ProtocolError(f'Word does not have data bit set: {word}')
byte = (word >> 2) & 0xff
parity = (word >> 1) & 0x1
if check_parity and parity != odd_parity(byte):
raise ProtocolError('Parity error')
raise ProtocolError(f'Parity error: {word}')
return byte
@@ -387,7 +387,8 @@ def _execute_read_command(interface, command_word, response_length=1,
if validate_response_length and len(response) != response_length:
command = unpack_command_word(command_word)
raise ProtocolError(f'Expected {response_length} word {command.name} response')
raise ProtocolError((f'Expected {response_length} word {command.name} '
f'response: {response}'))
return unpack_data_words(response) if unpack else response
@@ -409,7 +410,7 @@ def _execute_write_command(interface, command_word, data=None, **kwargs):
if len(response) != 1:
command = unpack_command_word(command_word)
raise ProtocolError(f'Expected 1 word {command.name} response')
raise ProtocolError(f'Expected 1 word {command.name} response: {response}')
if response[0] != 0:
raise ProtocolError('Expected TR/TA response')
raise ProtocolError(f'Expected TR/TA response: {response}')

View File

@@ -36,7 +36,7 @@ class SerialInterface(Interface):
raise _convert_error(message)
if len(message) != 4:
raise InterfaceError('Invalid reset response')
raise InterfaceError(f'Invalid reset response: {message}')
(major, minor, patch) = struct.unpack('BBB', message[1:])
@@ -109,7 +109,7 @@ class SerialInterface(Interface):
raise InterfaceError('SLIP protocol error')
if len(message) < 4:
raise InterfaceError('Invalid response message')
raise InterfaceError(f'Invalid response message: {message}')
(length,) = struct.unpack('>H', message[:2])
@@ -156,15 +156,15 @@ ERROR_MAP = {
def _convert_error(message):
if message[0] != 0x02:
return InterfaceError('Invalid response')
return InterfaceError(f'Invalid response: {message}')
if len(message) < 2:
return InterfaceError('Invalid error response')
return InterfaceError(f'Invalid error response: {message}')
if message[1] in ERROR_MAP:
return ERROR_MAP[message[1]]
return InterfaceError('Unknown error')
return InterfaceError(f'Unknown error: {message[1]}')
class SlipSerial(SlipWrapper):
"""sliplib wrapper for pySerial."""