Coverage for tropicsquare / error_mapping.py: 100%
18 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 21:24 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-03-27 21:24 +0000
1"""
2Error code to exception mapping for TropicSquare
3"""
5from tropicsquare.exceptions import *
6from tropicsquare.constants.cmd_result import *
7from tropicsquare.constants.rsp_status import *
10def map_cmd_result_to_exception(cmd_result):
11 """Map command result code to appropriate exception"""
12 error_map = {
13 CMD_RESULT_FAIL: (TropicSquareCommandError, "Command execution failed"),
14 CMD_RESULT_UNAUTHORIZED: (TropicSquareUnauthorizedError, "Command not authorized"),
15 CMD_RESULT_INVALID_CMD: (TropicSquareInvalidCommandError, "Invalid command"),
16 CMD_RESULT_MEM_WRITE_FAIL: (TropicSquareMemoryWriteError, "Memory write operation failed"),
17 CMD_RESULT_MEM_SLOT_EXPIRED: (TropicSquareMemorySlotExpiredError, "Memory slot has expired"),
18 CMD_RESULT_ECC_INVALID_KEY: (TropicSquareECCInvalidKeyError, "ECC key is invalid or not found"),
19 CMD_RESULT_MCOUNTER_UPDATE_ERROR: (TropicSquareCounterUpdateError, "Monotonic counter update failed"),
20 CMD_RESULT_MCOUNTER_COUNTER_INVALID: (TropicSquareCounterInvalidError, "Invalid monotonic counter"),
21 CMD_RESULT_PAIRING_KEY_EMPTY: (TropicSquarePairingKeyEmptyError, "Pairing key slot is empty"),
22 CMD_RESULT_PAIRING_KEY_INVALID: (TropicSquarePairingKeyInvalidError, "Invalid pairing key"),
23 }
25 exception_class, message = error_map.get(cmd_result, (TropicSquareCommandError, "Command failed"))
26 return exception_class(f"{message} (result: {hex(cmd_result)})", error_code=cmd_result)
29def map_response_status_to_exception(rsp_status):
30 """Map response status code to appropriate exception"""
31 error_map = {
32 RSP_STATUS_RESP_DISABLED: (TropicSquareResponseError, "Response disabled"),
33 RSP_STATUS_HSK_ERROR: (TropicSquareHandshakeError, "Handshake error"),
34 RSP_STATUS_NO_SESSION: (TropicSquareNoSession, "No secure session established"),
35 RSP_STATUS_TAG_ERROR: (TropicSquareTagError, "Authentication tag error"),
36 RSP_STATUS_CRC_ERROR: (TropicSquareCRCError, "CRC validation failed"),
37 RSP_STATUS_UNKNOWN_REQ: (TropicSquareInvalidCommandError, "Unknown request"),
38 RSP_STATUS_GEN_ERROR: (TropicSquareError, "General error"),
39 RSP_STATUS_NO_RESPONSE: (TropicSquareTimeoutError, "No response from chip"),
40 }
42 exception_class, message = error_map.get(rsp_status, (TropicSquareError, "Unknown response error"))
43 return exception_class(f"{message} (status: {hex(rsp_status)})", error_code=rsp_status)
46def raise_for_cmd_result(cmd_result):
47 """Raise exception if command result indicates error"""
48 if cmd_result != CMD_RESULT_OK:
49 raise map_cmd_result_to_exception(cmd_result)
52def raise_for_response_status(rsp_status):
53 """Raise exception if response status indicates error"""
54 valid_statuses = [RSP_STATUS_REQ_OK, RSP_STATUS_RES_OK, RSP_STATUS_RES_CONT, RSP_STATUS_REQ_CONT]
55 if rsp_status not in valid_statuses:
56 raise map_response_status_to_exception(rsp_status)