Skip to content

Commit c885bd2

Browse files
goffrieConvex, Inc.
authored andcommitted
Truncate values displayed by stringifyValueForError (#42023)
This can otherwise create very long error messages, which is presumably not helpful. GitOrigin-RevId: 1574b89728022d4438390350fc856f3dcaa8e84c
1 parent a701721 commit c885bd2

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/values/value.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,10 @@ export function jsonToConvex(value: JSONValue): Value {
254254
return out;
255255
}
256256

257+
const MAX_VALUE_FOR_ERROR_LEN = 16384;
258+
257259
export function stringifyValueForError(value: any) {
258-
return JSON.stringify(value, (_key, value) => {
260+
const str = JSON.stringify(value, (_key, value) => {
259261
if (value === undefined) {
260262
// By default `JSON.stringify` converts undefined, functions, symbols,
261263
// Infinity, and NaN to null which produces a confusing error message.
@@ -270,6 +272,17 @@ export function stringifyValueForError(value: any) {
270272
}
271273
return value;
272274
});
275+
if (str.length > MAX_VALUE_FOR_ERROR_LEN) {
276+
const rest = "[...truncated]";
277+
let truncateAt = MAX_VALUE_FOR_ERROR_LEN - rest.length;
278+
const codePoint = str.codePointAt(truncateAt - 1);
279+
if (codePoint !== undefined && codePoint > 0xffff) {
280+
// don't split a surrogate pair in half
281+
truncateAt -= 1;
282+
}
283+
return str.substring(0, truncateAt) + rest;
284+
}
285+
return str;
273286
}
274287

275288
function convexToJsonInternal(

0 commit comments

Comments
 (0)