python-betterproto icon indicating copy to clipboard operation
python-betterproto copied to clipboard

Better exception messages

Open Gobot1234 opened this issue 3 years ago • 0 comments

I think anyone who has used the library for a while will eventually run into an issue that is made hard to debug by the error messages when serialising and deserialising fails. Consider an error like:

  File "betterproto/__init__.py", line 767, in __bytes__
    output += _serialize_single(
  File "betterproto/__init__.py", line 359, in _serialize_single
    value = _preprocess_single(proto_type, wraps, value)
  File "betterproto/__init__.py", line 325, in _preprocess_single
    return struct.pack(_pack_fmt(proto_type), value)
struct.error: argument out of range

The message is relatively unclear as to what has gone wrong especially as it does not include the field which was out of the range and the value that caused the error.

Ideally the error would tell you the name of the field that errored, the value and the value the range has to be in.

  File "betterproto/__init__.py", line 767, in __bytes__
    output += _serialize_single(
  File "betterproto/__init__.py", line 359, in _serialize_single
    value = _preprocess_single(proto_type, wraps, value)
  File "betterproto/__init__.py", line 325, in _preprocess_single
    return struct.pack(_pack_fmt(proto_type), value)
struct.error: argument out of range

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  ...
  File "betterproto/__init__.py", line 775, in __bytes__
    raise SerialisationError(field, value) from e
SerialisationError: Failed to serialize field 'rtime32_start_time', with value 123456789123456789, value should be in [0, 2**32)

This obviously could be adapted for more errors than just the struct error, but it would be a good easy starting point and would incure no runtime cost in newer python versions.

Gobot1234 avatar Jun 02 '22 23:06 Gobot1234