Skip to content

Commit 914c16e

Browse files
authored
Provide more contextual error message for PyCoder (#36825)
* Provide more contextual error message for coder * Fix formatting issue * Fix lint error
1 parent 3721c17 commit 914c16e

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

sdks/python/apache_beam/coders/coder_impl.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,14 @@ class VarIntCoderImpl(StreamCoderImpl):
10141014
A coder for int objects."""
10151015
def encode_to_stream(self, value, out, nested):
10161016
# type: (int, create_OutputStream, bool) -> None
1017-
out.write_var_int64(value)
1017+
try:
1018+
out.write_var_int64(value)
1019+
except OverflowError as e:
1020+
raise OverflowError(
1021+
f"Integer value '{value}' is out of the encodable range for "
1022+
f"VarIntCoder. This coder is limited to values that fit "
1023+
f"within a 64-bit signed integer (-(2**63) to 2**63 - 1). "
1024+
f"Original error: {e}") from e
10181025

10191026
def decode_from_stream(self, in_stream, nested):
10201027
# type: (create_InputStream, bool) -> int
@@ -1036,7 +1043,13 @@ def decode(self, encoded):
10361043
def estimate_size(self, value, nested=False):
10371044
# type: (Any, bool) -> int
10381045
# Note that VarInts are encoded the same way regardless of nesting.
1039-
return get_varint_size(value)
1046+
try:
1047+
return get_varint_size(value)
1048+
except OverflowError as e:
1049+
raise OverflowError(
1050+
f"Cannot estimate size for integer value '{value}'. "
1051+
f"Value is out of the range for VarIntCoder (64-bit signed integer). "
1052+
f"Original error: {e}") from e
10401053

10411054

10421055
class VarInt32CoderImpl(StreamCoderImpl):

0 commit comments

Comments
 (0)