diff --git a/test/test_messages.py b/test/test_messages.py index 57ff54b..d3df561 100644 --- a/test/test_messages.py +++ b/test/test_messages.py @@ -379,6 +379,25 @@ class TestProtocolAny(unittest.TestCase): msg = p.execute(Interactions.REGISTER_PRESS_BUTTON, uuid=uuid) self.assertEqual(msg.uuid, uuid) + def test_error_invalid_state(self): + def _cb(request, requires_reply=True, userdata=None, timeout=5): + return NordicData([0xb3, 0x1, 0x1]) + + p = Protocol(self.protocol_version, callback=_cb) + + # a "random" collection of requests that we want to check for + with self.assertRaises(DeviceError) as cm: + p.execute(Interactions.CONNECT, uuid='abcdef123456') + self.assertEqual(cm.exception.errorcode, DeviceError.ErrorCode.GENERAL_ERROR) + + with self.assertRaises(DeviceError) as cm: + p.execute(Interactions.GET_STROKES) + self.assertEqual(cm.exception.errorcode, DeviceError.ErrorCode.GENERAL_ERROR) + + with self.assertRaises(DeviceError) as cm: + p.execute(Interactions.SET_MODE, Mode.PAPER) + self.assertEqual(cm.exception.errorcode, DeviceError.ErrorCode.GENERAL_ERROR) + class TestProtocolSpark(TestProtocolAny): protocol_version = ProtocolVersion.SPARK diff --git a/tuhi/protocol.py b/tuhi/protocol.py index 0485bd6..5c0d982 100644 --- a/tuhi/protocol.py +++ b/tuhi/protocol.py @@ -523,18 +523,16 @@ class Msg(object): self._args = args def _handle_reply(self, reply): - '''Override this in the subclass to handle the reply. + ''' + Override this in the subclass to handle the reply. Note that the + default 0xb3 message is handled automaticaly, this is only for + non-default replies. - This is the default reply handler that deals with the 0xb3 ACK/Error - messages and throws the respective exceptions. + No return value, just throw the appropriate exception on failure. :param reply: A :class:`NordicData` object ''' - if reply.opcode != 0xb3: - raise UnexpectedReply(self) - - if reply[0] != 0x00: - raise DeviceError(reply[0]) + raise NotImplementedError(f'{reply} needs customized handling') def execute(self): ''' @@ -554,7 +552,14 @@ class Msg(object): if self.reply is None: raise MissingReplyError(self.request) try: - self._handle_reply(self.reply) + # 0xb3 is always handled by us, anything else requires a + # custom reply handler + if self.reply.opcode == 0xb3: + if self.reply[0] != 0x00: + raise DeviceError(self.reply[0]) + else: + self._handle_reply(self.reply) + # no exception? we can assume success self.errorcode = DeviceError.ErrorCode.SUCCESS except DeviceError as e: