Skip to content

Commit 777fae9

Browse files
committed
Use a custom Exception instead of broad KeyError
1 parent f2d40de commit 777fae9

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

pymodbus/datastore/store.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from collections.abc import Iterable
5252
from typing import Any, Generic, TypeVar
5353

54-
from pymodbus.exceptions import ParameterException
54+
from pymodbus.exceptions import NoSuchAddressException, ParameterException
5555

5656

5757
# ---------------------------------------------------------------------------#
@@ -259,8 +259,12 @@ def getValues(self, address, count=1):
259259
:param address: The starting address
260260
:param count: The number of values to retrieve
261261
:returns: The requested values from a:a+c
262+
:raises: NoSuchAddressException
262263
"""
263-
return [self.values[i] for i in range(address, address + count)]
264+
try:
265+
return [self.values[i] for i in range(address, address + count)]
266+
except KeyError as e:
267+
raise NoSuchAddressException(str(e)) from e
264268

265269
def _process_values(self, values):
266270
"""Process values."""

pymodbus/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ def __init__(self, string=""):
6969
message = f"[No Such id] {string}"
7070
ModbusException.__init__(self, message)
7171

72+
class NoSuchAddressException(ModbusException):
73+
"""Error resulting from making a request for a register that does not exist."""
74+
75+
def __init__(self, string=""):
76+
"""Initialize."""
77+
message = f"[No such register address] {string}"
78+
ModbusException.__init__(self, message)
79+
7280

7381
class NotImplementedException(ModbusException):
7482
"""Error resulting from not implemented function."""

pymodbus/server/requesthandler.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import asyncio
55
import traceback
66

7-
from pymodbus.exceptions import ModbusIOException, NoSuchIdException
7+
from pymodbus.exceptions import (
8+
ModbusIOException,
9+
NoSuchAddressException,
10+
NoSuchIdException,
11+
)
812
from pymodbus.logging import Log
913
from pymodbus.pdu.pdu import ExceptionResponse
1014
from pymodbus.transaction import TransactionManager
@@ -105,7 +109,7 @@ async def handle_request(self):
105109
if self.server.ignore_missing_devices:
106110
return # the client will simply timeout waiting for a response
107111
response = ExceptionResponse(self.last_pdu.function_code, ExceptionResponse.GATEWAY_NO_RESPONSE)
108-
except KeyError:
112+
except NoSuchAddressException:
109113
Log.error("Requested register address does not exist: {}", self.last_pdu.address)
110114
response = ExceptionResponse(self.last_pdu.function_code, ExceptionResponse.ILLEGAL_ADDRESS)
111115
except Exception as exc: # pylint: disable=broad-except

0 commit comments

Comments
 (0)