diff --git a/pymodbus/exceptions.py b/pymodbus/exceptions.py index d4a3f75bd..250056b53 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 8581df007..60079f1a2 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()