Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 15 additions & 35 deletions pymodbus/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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 = "",
Expand All @@ -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"
28 changes: 28 additions & 0 deletions test/global/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading