AdvancedClimateSystems / uModbus

Python implementation of the Modbus protocol.
Mozilla Public License 2.0
211 stars 81 forks source link

Explicitly check for missing endpoints #100

Closed rgov closed 4 years ago

rgov commented 4 years ago

The ModbusFunction.execute implementations invoke the callable returned by route_map.match(). The code assumes that if no match is found, then when calling the resulting None object, a TypeError will be thrown, and so it catches the exception and throws a IllegalDataAddressError instead.

However, by handling all TypeError exceptions the code is disguising TypeErrors thrown in other conditions. This is too general an exception when the code is really trying to handle one specific case. For instance, it hides the fact that a route's endpoint function might be declared with the wrong number of arguments.

This patch changes those exception handlers to simply an if statement. As a bonus it reduces indentation and reduces the (cyclomatic) complexity of the code; there is no longer the potential for a sudden jump to an exception handler out of a loop.

coveralls commented 4 years ago

Coverage Status

Coverage decreased (-0.04%) to 95.824% when pulling 7527a566a071ef2f698c1f0713e0acc91f12cbfa on rgov:rzg/fix-exceptions into dcf35a1f9881d0d5ffa4a640d1242fdcb3491bd7 on AdvancedClimateSystems:master.