diff --git a/can/interfaces/systec/exceptions.py b/can/interfaces/systec/exceptions.py index 8768b412a..f4afe4a91 100644 --- a/can/interfaces/systec/exceptions.py +++ b/can/interfaces/systec/exceptions.py @@ -13,10 +13,11 @@ def __init__(self, result, func, arguments): self.func = func self.arguments = arguments - message = self._error_message_mapping.get(result, "unknown") + result_code = result.value + message = self._error_message_mapping.get(result_code, "unknown") super().__init__( message=f"Function {func.__name__} (called with {arguments}): {message}", - error_code=result.value, + error_code=result_code, ) @property diff --git a/doc/changelog.d/2077.fixed.rst b/doc/changelog.d/2077.fixed.rst new file mode 100644 index 000000000..1d562ec56 --- /dev/null +++ b/doc/changelog.d/2077.fixed.rst @@ -0,0 +1 @@ +Fixed Systec errors raising ``TypeError`` instead of reporting the underlying USB-CAN return code. diff --git a/test/test_systec.py b/test/test_systec.py index 86ed31362..c1f9ad1ce 100644 --- a/test/test_systec.py +++ b/test/test_systec.py @@ -5,6 +5,8 @@ import can from can.interfaces.systec import ucan, ucanbus +from can.interfaces.systec.constants import ReturnCode +from can.interfaces.systec.exceptions import UcanError from can.interfaces.systec.ucan import * @@ -46,6 +48,15 @@ def test_bus_creation(self): self.assertTrue(ucan.UcanGetHardwareInfoEx2.called) self.assertTrue(ucan.UcanSetAcceptanceEx.called) + def test_error_preserves_ctypes_return_code_message(self): + def failing_function(): + pass + + error = UcanError(ReturnCode(ReturnCode.ERR_ILLPARAM), failing_function, ()) + + self.assertEqual(error.error_code, ReturnCode.ERR_ILLPARAM) + self.assertIn("wrong parameter handed over to the function", str(error)) + def test_bus_shutdown(self): self.bus.shutdown() self.assertTrue(ucan.UcanDeinitCanEx.called)