Skip to content

Commit 8d7f0b1

Browse files
committed
fix: Put only the reason of the error to the error.reason property when the custom parser is used; not the full message including the error context
1 parent 33f4c4d commit 8d7f0b1

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/custom-parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function parseInternal (input, options) {
113113
++lineNumber
114114
var texts = getTexts(message, input, position, lineNumber, column)
115115
var error = SyntaxError(texts.message)
116-
error.reason = texts.message
116+
error.reason = message
117117
error.exzerpt = texts.exzerpt
118118
error.pointer = texts.pointer
119119
error.location = {

test/parse1.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,13 @@ function checkErrorInformation (error) {
2727
}
2828
}
2929

30-
if (!nativeParser) {
31-
exports['test exported interface'] = function () {
32-
assert.equal(typeof parse, 'function')
33-
}
30+
exports['test exported interface'] = function () {
31+
assert.equal(typeof parse, 'function')
32+
}
3433

35-
exports['test function'] = function () {
36-
var json = '{"foo": "bar"}'
37-
assert.deepEqual(parse(json), { 'foo': 'bar' })
38-
}
34+
exports['test function'] = function () {
35+
var json = '{"foo": "bar"}'
36+
assert.deepEqual(parse(json), { 'foo': 'bar' })
3937
}
4038

4139
exports['test limited error information'] = function () {
@@ -259,19 +257,37 @@ exports['test extra brace'] = function () {
259257
assert['throws'](function () { parse(json) }, 'should throw error')
260258
}
261259

262-
exports['test error location with Windows line breaks'] = function () {
260+
if (!oldNode) {
261+
exports['test error location with Windows line breaks using the native parser'] = function () {
262+
var json = '{\r\n"foo": {\r\n "bar":\r\n }\r\n \r\n }'
263+
try {
264+
exported.parseNative(json)
265+
assert.fail('should throw error')
266+
} catch (error) {
267+
assert.equal(error.exzerpt, '... "bar": } }')
268+
assert.equal(error.pointer, '-------------------^')
269+
assert.equal(error.reason, 'Unexpected token }')
270+
assert.deepEqual(error.location, {
271+
start: { column: 5, line: 4, offset: 31 }
272+
})
273+
assert.equal(error.message, 'Parse error on line 4, column 5:\n... "bar": } }\n-------------------^\nUnexpected token }')
274+
}
275+
}
276+
}
277+
278+
exports['test error location with Windows line breaks using the custom parser'] = function () {
263279
var json = '{\r\n"foo": {\r\n "bar":\r\n }\r\n \r\n }'
264280
try {
265-
parse(json)
281+
exported.parseCustom(json)
266282
assert.fail('should throw error')
267283
} catch (error) {
268284
assert.equal(error.exzerpt, '... "bar": } }')
269285
assert.equal(error.pointer, '-------------------^')
270-
assert.equal(error.reason, 'Unexpected token }')
271-
assert.equal(error.message, 'Parse error on line 4, column 5:\n... "bar": } }\n-------------------^\nUnexpected token }')
286+
assert.equal(error.reason, 'No value found for key "bar"')
272287
assert.deepEqual(error.location, {
273288
start: { column: 5, line: 4, offset: 31 }
274289
})
290+
assert.equal(error.message, 'Parse error on line 4, column 5:\n... "bar": } }\n-------------------^\nNo value found for key "bar"')
275291
}
276292
}
277293

0 commit comments

Comments
 (0)