Skip to content

Commit 156f3c8

Browse files
Preserve ValueError context when re-raising invalid type codes
Two ValueType conversion sites in tuples.py — the params-tuple decoder and the row-header decoder — caught ValueError from ValueType(raw) and re-raised as DecodeError with `from None`, suppressing the original exception. That loses the underlying Python enum's error phrasing (e.g., "X is not a valid ValueType"), which is the only diagnostic detail beyond the byte value and index we already include in the message. It also diverges from the rest of the file, which chains with `from e` (struct errors) or lets the raise propagate implicitly. Change both sites to chain with `from e` so the cause is preserved in traceback output and log-format exception chains. Extend the two existing negative tests to assert __cause__ is a ValueError. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 6db31c4 commit 156f3c8

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/dqlitewire/tuples.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ def decode_params_tuple(
185185
raw_type = data[count_size + i]
186186
try:
187187
types.append(ValueType(raw_type))
188-
except ValueError:
189-
raise DecodeError(f"Invalid value type code {raw_type} at param index {i}") from None
188+
except ValueError as e:
189+
raise DecodeError(f"Invalid value type code {raw_type} at param index {i}") from e
190190
offset = padded_header_len
191191

192192
# Read values
@@ -267,8 +267,8 @@ def decode_row_header(
267267
nibble = data[byte_idx] & 0x0F if i % 2 == 0 else (data[byte_idx] >> 4) & 0x0F
268268
try:
269269
types.append(ValueType(nibble))
270-
except ValueError:
271-
raise DecodeError(f"Invalid value type code {nibble} at column index {i}") from None
270+
except ValueError as e:
271+
raise DecodeError(f"Invalid value type code {nibble} at column index {i}") from e
272272

273273
return types, header_size
274274

tests/test_tuples.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,11 @@ def test_decode_invalid_type_code_raises_decode_error(self) -> None:
409409

410410
# Byte 0x00 means both nibbles are 0, which is not a valid ValueType
411411
data = b"\x00" * 8
412-
with pytest.raises(DecodeError, match="Invalid value type"):
412+
with pytest.raises(DecodeError, match="Invalid value type") as exc_info:
413413
decode_row_header(data, 1)
414+
# Chain preserves the underlying ValueError so log backends show
415+
# "the above exception was the direct cause" with the enum name.
416+
assert isinstance(exc_info.value.__cause__, ValueError)
414417

415418

416419
class TestParamsTupleInvalidType:
@@ -422,8 +425,11 @@ def test_decode_invalid_type_code_raises_decode_error(self) -> None:
422425

423426
# count=1, type=0 (invalid), padding to 8 bytes
424427
data = b"\x01\x00\x00\x00\x00\x00\x00\x00" + b"\x00" * 8
425-
with pytest.raises(DecodeError, match="Invalid value type"):
428+
with pytest.raises(DecodeError, match="Invalid value type") as exc_info:
426429
decode_params_tuple(data)
430+
# Chain preserves the underlying ValueError so log backends show
431+
# "the above exception was the direct cause" with the enum name.
432+
assert isinstance(exc_info.value.__cause__, ValueError)
427433

428434

429435
class TestParamsTupleSchemaValidation:

0 commit comments

Comments
 (0)