Skip to content

Commit f9c7216

Browse files
committed
Handle "not supported" errors in the core Transaction function.
If the Notecard's response contains an error with "{not-supported}", do not retry the transaction.
1 parent 3ecb772 commit f9c7216

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

notecard/notecard.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,22 @@
4141
use_periphery = False
4242
use_serial_lock = False
4343

44-
if sys.implementation.name == 'cpython' and (sys.platform == 'linux' or sys.platform == 'linux2'):
44+
if sys.implementation.name == 'cpython' and (sys.platform == 'linux'
45+
or sys.platform == 'linux2'):
4546
use_periphery = True
4647
from periphery import I2C
4748

4849
use_serial_lock = True
4950
from filelock import FileLock
5051
from filelock import Timeout as SerialLockTimeout
5152
else:
53+
5254
class SerialLockTimeout(Exception):
5355
"""A null SerialLockTimeout for when use_serial_lock is False."""
5456

5557
pass
5658

59+
5760
use_i2c_lock = not use_periphery and sys.implementation.name != 'micropython'
5861

5962
NOTECARD_I2C_ADDRESS = 0x17
@@ -310,9 +313,9 @@ def Transaction(self, req, lock=True):
310313
if rsp_expected:
311314
while retries_left > 0:
312315
try:
313-
rsp_bytes = self._transact(
314-
req_bytes, rsp_expected=True,
315-
timeout_secs=timeout_secs)
316+
rsp_bytes = self._transact(req_bytes,
317+
rsp_expected=True,
318+
timeout_secs=timeout_secs)
316319
except Exception as e:
317320
if self._debug:
318321
print(e)
@@ -344,7 +347,7 @@ def Transaction(self, req, lock=True):
344347
continue
345348

346349
if 'err' in rsp_json:
347-
if '{io}' in rsp_json['err']:
350+
if '{io}' in rsp_json['err'] and '{not-supported}' not in rsp_json['err']:
348351
if self._debug:
349352
print('Response has error field indicating ' + \
350353
f'I/O error: {rsp_json}')
@@ -366,7 +369,8 @@ def Transaction(self, req, lock=True):
366369
break
367370
else:
368371
try:
369-
self._transact(req_bytes, rsp_expected=False,
372+
self._transact(req_bytes,
373+
rsp_expected=False,
370374
timeout_secs=timeout_secs)
371375
except Exception as e:
372376
error = True
@@ -423,7 +427,9 @@ def SetTransactionPins(self, rtx_pin, ctx_pin):
423427
class OpenSerial(Notecard):
424428
"""Notecard class for Serial communication."""
425429

426-
def _transact(self, req_bytes, rsp_expected,
430+
def _transact(self,
431+
req_bytes,
432+
rsp_expected,
427433
timeout_secs=CARD_INTER_TRANSACTION_TIMEOUT_SEC):
428434
self.transmit(req_bytes)
429435

@@ -441,7 +447,8 @@ def _transact(self, req_bytes, rsp_expected,
441447

442448
return self.receive()
443449

444-
def receive(self, timeout_secs=CARD_INTRA_TRANSACTION_TIMEOUT_SEC,
450+
def receive(self,
451+
timeout_secs=CARD_INTRA_TRANSACTION_TIMEOUT_SEC,
445452
delay=True):
446453
"""Read a newline-terminated batch of data from the Notecard."""
447454
data = bytearray()
@@ -638,7 +645,8 @@ def _write(self, data):
638645
"""Perform a serial-over-I2C write."""
639646
self._platform_write(bytearray([len(data)]), data)
640647

641-
def receive(self, timeout_secs=CARD_INTRA_TRANSACTION_TIMEOUT_SEC,
648+
def receive(self,
649+
timeout_secs=CARD_INTRA_TRANSACTION_TIMEOUT_SEC,
642650
delay=True):
643651
"""Read a newline-terminated batch of data from the Notecard."""
644652
read_len = 0
@@ -709,7 +717,9 @@ def transmit(self, data, delay=True):
709717
if delay:
710718
time.sleep(CARD_REQUEST_I2C_CHUNK_DELAY_MS / 1000)
711719

712-
def _transact(self, req_bytes, rsp_expected,
720+
def _transact(self,
721+
req_bytes,
722+
rsp_expected,
713723
timeout_secs=CARD_INTER_TRANSACTION_TIMEOUT_SEC):
714724
self.transmit(req_bytes)
715725

test/test_notecard.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,16 @@ def test_transaction_retries_on_io_error_in_response(
294294
assert card._transact.call_count == \
295295
notecard.CARD_TRANSACTION_RETRIES
296296

297+
def test_transaction_does_not_retry_on_not_supported_error_in_response(
298+
self, arrange_transaction_test):
299+
card = arrange_transaction_test()
300+
req = {"req": "hub.status"}
301+
302+
with patch('notecard.notecard.json.loads',
303+
return_value={'err': 'some error {io} {not-supported}'}):
304+
card.Transaction(req)
305+
assert card._transact.call_count == 1
306+
297307
def test_transaction_does_not_retry_on_bad_bin_error_in_response(
298308
self, arrange_transaction_test):
299309
card = arrange_transaction_test()

0 commit comments

Comments
 (0)