More descriptive decoder exceptions #2260
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #2259.
Three current tests are relevant for badly encoded
Response
.(1)
test_client_factory_fails()
callsClientDecoder.decode(None)
and expectsNone
.This would cause a
TypeError
, but it's monkey-patched to throw aModbusException
instead. (The monkey patching was never necessary with the catch-allExcept
)(2)
test_server_factory_fails()
callsServerDecoder.decode(None)
and expectsNone
.This would cause a
TypeError
, but it's monkey-patched to throw aModbusException
instead. This is caught normally.I'm assuming that in (1) and (2) we should actually throw the
TypeError
to the caller, so I changed as such. I see no valid use case for decodingNone
...(3)
test_processincomingpacket_not_ok()
callsClientDecoder.decode(b'\x03\x01\x00\n')
and expects aModbusIOException
. The real exception is astruct.error
because the payload has only 3 bytes. The register values must be an even number of bytes i.e. 6 bytes for 3 registers.This bad message is caught by
decode()
, losing information on the error. Later onframeProcessIncomingPacket
notices thatdecode
returned None andraise ModbusIOException
.I think it's better to explicitly raise a descriptive exception in this case.