Skip to content

Commit 298a773

Browse files
committed
Make unicode decode error clearer
1 parent 52db5af commit 298a773

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

accel.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,15 @@ static PyObject *read_row_from_packet(
15331533
if (!py_str) goto error;
15341534
} else {
15351535
py_str = PyUnicode_Decode(out, out_l, py_state->encodings[i], py_state->encoding_errors);
1536+
if (PyErr_Occurred()) {
1537+
PyErr_Clear();
1538+
PyErr_Format(
1539+
PyExc_UnicodeDecodeError,
1540+
"failed to decode string value in column '%S' using encoding '%s'; "
1541+
"use the 'encoding_errors' option on the connection to specify how to handle this error",
1542+
py_state->py_names[i], py_state->encodings[i]
1543+
);
1544+
}
15361545
if (!py_str) goto error;
15371546
}
15381547
if (py_state->py_converters[i] == Py_None) {
@@ -1740,6 +1749,15 @@ static PyObject *read_row_from_packet(
17401749
}
17411750

17421751
py_item = PyUnicode_Decode(out, out_l, py_state->encodings[i], py_state->encoding_errors);
1752+
if (PyErr_Occurred()) {
1753+
PyErr_Clear();
1754+
PyErr_Format(
1755+
PyExc_UnicodeDecodeError,
1756+
"failed to decode string value in column '%S' using encoding '%s'; "
1757+
"use the 'encoding_errors' option on the connection to specify how to handle this error",
1758+
py_state->py_names[i], py_state->encodings[i]
1759+
);
1760+
}
17431761
if (!py_item) goto error;
17441762

17451763
// Parse JSON string.

singlestoredb/mysql/connection.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,7 @@ def _read_rowdata_packet(self):
18411841

18421842
def _read_row_from_packet(self, packet):
18431843
row = []
1844-
for encoding, converter in self.converters:
1844+
for i, (encoding, converter) in enumerate(self.converters):
18451845
try:
18461846
data = packet.read_length_coded_string()
18471847
except IndexError:
@@ -1850,7 +1850,15 @@ def _read_row_from_packet(self, packet):
18501850
break
18511851
if data is not None:
18521852
if encoding is not None:
1853-
data = data.decode(encoding, errors=self.encoding_errors)
1853+
try:
1854+
data = data.decode(encoding, errors=self.encoding_errors)
1855+
except UnicodeDecodeError:
1856+
raise UnicodeDecodeError(
1857+
'failed to decode string value in column '
1858+
f"'{self.fields[i].name}' using encoding '{encoding}'; " +
1859+
"use the 'encoding_errors' option on the connection " +
1860+
'to specify how to handle this error',
1861+
)
18541862
if DEBUG:
18551863
print('DEBUG: DATA = ', data)
18561864
if converter is not None:

0 commit comments

Comments
 (0)