Skip to content

Commit 1d0f658

Browse files
authored
test(json): improve json testing (#5075)
1 parent 7e683c1 commit 1d0f658

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

json/_common.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ import type { JsonValue } from "./common.ts";
55
export function parse(text: string): JsonValue {
66
try {
77
return JSON.parse(text);
8-
} catch (error: unknown) {
9-
if (error instanceof Error) {
10-
// Truncate the string so that it is within 30 lengths.
11-
const truncatedText = 30 < text.length ? `${text.slice(0, 30)}...` : text;
12-
throw new (error.constructor as ErrorConstructor)(
13-
`${error.message} (parsing: '${truncatedText}')`,
14-
);
15-
}
16-
throw error;
8+
} catch (error) {
9+
// Truncate the string so that it is within 30 lengths.
10+
const truncatedText = 30 < text.length ? `${text.slice(0, 30)}...` : text;
11+
throw new ((error as Error).constructor as ErrorConstructor)(
12+
`${(error as Error).message} (parsing: '${truncatedText}')`,
13+
);
1714
}
1815
}

json/concatenated_json_parse_stream_test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,14 @@ Deno.test({
268268
["tr", 'ue{"foo": "bar"}'],
269269
[true, { foo: "bar" }],
270270
);
271+
// Invalid primitive which share some leading characters with the valid primitive
272+
await assertInvalidParse(
273+
ConcatenatedJsonParseStream,
274+
["truu"],
275+
{},
276+
SyntaxError,
277+
`Unexpected token 'u', \"truu\" is not valid JSON (parsing: 'truu')`,
278+
);
271279
},
272280
});
273281

0 commit comments

Comments
 (0)