Skip to content

Commit

Permalink
Make the failed message bytes available in a CRCError exception.
Browse files Browse the repository at this point in the history
When a serial CRC validation fails, the exception is constructed with
a descriptive text as its only argument.  To aid in debugging, pass in
the original failing message instead.  Make sure the string
representation of the exception stays the same, by adding a __str__()
method like in the library's other exceptions.
  • Loading branch information
acolomb committed Mar 3, 2021
1 parent 5c02a1a commit 7522a4f
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions umodbus/client/serial/redundancy_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ def validate_crc(msg):
"""
if not struct.unpack('<H', get_crc(msg[:-2])) ==\
struct.unpack('<H', msg[-2:]):
raise CRCError('CRC validation failed.')
raise CRCError(msg)


class CRCError(Exception):
""" Valid error to raise when CRC isn't correct. """
pass
"""CRC validation failed."""

def __str__(self):
return self.__doc__

0 comments on commit 7522a4f

Please sign in to comment.