Skip to content

Commit 9757213

Browse files
committed
fix: Ensure error location by custom parsing
The native JSON.parse does not always provide the error location at the end of the error message. These two invalid inputs either include or not include the error location and it is different with Node.js 18 and Node.js 20: { "foo": \"baz } { "foo": baz } If the locatoin is missing, let us parse the input once more by the custom parser, which always provides the error location.
1 parent 30f611a commit 9757213

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/native-parser.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ function parseNative (input, reviver) {
155155
try {
156156
return JSON.parse(input, reviver)
157157
} catch (error) {
158-
throw improveNativeError(input, error)
158+
const newError = improveNativeError(input, error)
159+
if (error.location) throw newError
160+
// If the native error didn't contain location, parse once more
161+
// by the custom parser, which always provides the error location.
162+
return parseCustom (input, reviver)
159163
}
160164
}

0 commit comments

Comments
 (0)