From dbf037acaaca903db8eb101831662d696393d6b4 Mon Sep 17 00:00:00 2001 From: Harsh Dave <107102025+daveharsh38@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:38:05 -0400 Subject: [PATCH] Pull up the exception constructors into ModbusException. ParameterException, NoSuchIdException, NotImplementedException, ConnectionException and MessageRegisterException each repeated the same __init__, differing only in the "[...]" text put in front of the message. Pull the constructor up into ModbusException, with the text as a class field "category" that each subclass sets. ModbusIOException uses the same field and keeps its message attribute. The new tests pin the exact text of every exception (they pass before and after). --- pymodbus/exceptions.py | 50 ++++++++++------------------------ test/global/test_exceptions.py | 28 +++++++++++++++++++ 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/pymodbus/exceptions.py b/pymodbus/exceptions.py index d4a3f75bd7..250056b538 100644 --- a/pymodbus/exceptions.py +++ b/pymodbus/exceptions.py @@ -16,11 +16,16 @@ class ModbusException(Exception): """Base modbus exception.""" - def __init__(self, string: str) -> None: + #: Category shown as "[category]" in front of the message, set by subclasses. + category: str = "" + + def __init__(self, string: str = "") -> None: """Initialize the exception. :param string: The message to append to the error """ + if self.category: + string = f"[{self.category}] {string}" self.string = string super().__init__(string) @@ -36,6 +41,8 @@ def isError(self) -> bool: class ModbusIOException(ModbusException): """Error resulting from data i/o.""" + category = "Input/Output" + def __init__( self, string: str = "", @@ -62,62 +69,35 @@ def __init__( self.transaction_id = transaction_id self.dev_id = dev_id self.used_len = used_len - self.message = f"[Input/Output] {string}" - ModbusException.__init__(self, self.message) + super().__init__(string) + self.message = self.string class ParameterException(ModbusException): """Error resulting from invalid parameter.""" - def __init__(self, string: str = "") -> None: - """Initialize the exception. - - :param string: The message to append to the error - """ - message = f"[Invalid Parameter] {string}" - ModbusException.__init__(self, message) + category = "Invalid Parameter" class NoSuchIdException(ModbusException): """Error resulting from making a request to a id that does not exist.""" - def __init__(self, string: str = "") -> None: - """Initialize the exception. - - :param string: The message to append to the error - """ - message = f"[No Such id] {string}" - ModbusException.__init__(self, message) + category = "No Such id" class NotImplementedException(ModbusException): """Error resulting from not implemented function.""" - def __init__(self, string: str = "") -> None: - """Initialize the exception. - - :param string: The message to append to the error - """ - message = f"[Not Implemented] {string}" - ModbusException.__init__(self, message) + category = "Not Implemented" class ConnectionException(ModbusException): """Error resulting from a bad connection.""" - def __init__(self, string: str = "") -> None: - """Initialize the exception. - - :param string: The message to append to the error - """ - message = f"[Connection] {string}" - ModbusException.__init__(self, message) + category = "Connection" class MessageRegisterException(ModbusException): """Error resulting from failing to register a custom message request/response.""" - def __init__(self, string: str = "") -> None: - """Initialize.""" - message = f"[Error registering message] {string}" - ModbusException.__init__(self, message) + category = "Error registering message" diff --git a/test/global/test_exceptions.py b/test/global/test_exceptions.py index 8581df0072..60079f1a26 100644 --- a/test/global/test_exceptions.py +++ b/test/global/test_exceptions.py @@ -32,6 +32,34 @@ def test_exceptions(self): with pytest.raises(ModbusException, match="Modbus Error:"): raise exc + @pytest.mark.parametrize( + ("exc", "text"), + [ + (ModbusException("bad base"), "bad base"), + (ModbusIOException("bad register"), "[Input/Output] bad register"), + (ParameterException("bad parameter"), "[Invalid Parameter] bad parameter"), + (NotImplementedException("bad function"), "[Not Implemented] bad function"), + (ConnectionException("bad connection"), "[Connection] bad connection"), + (NoSuchIdException("no id"), "[No Such id] no id"), + ( + MessageRegisterException("Wrong message"), + "[Error registering message] Wrong message", + ), + (ParameterException(), "[Invalid Parameter] "), + ], + ) + def test_exception_text(self, exc, text): + """Test the text of each exception, including its category.""" + assert exc.string == text + assert exc.args == (text,) + assert str(exc) == f"Modbus Error: {text}" + + def test_io_exception_message(self): + """Test ModbusIOException keeps its message attribute.""" + assert ( + ModbusIOException("bad register").message == "[Input/Output] bad register" + ) + def test_is_error(self): """Test is_error().""" assert self.exceptions[0].isError()