Skip to content

Commit

Permalink
Fix JSON strict violation error to match native parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 8, 2017
1 parent 87df7e6 commit 448dc57
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
unreleased
==========

* Fix JSON strict violation error to match native parse error
* deps: bytes@3.0.0
* deps: debug@2.6.8
* deps: depd@~1.1.1
Expand Down
23 changes: 22 additions & 1 deletion lib/types/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function json (options) {

if (first !== '{' && first !== '[') {
debug('strict violation')
throw new SyntaxError('Unexpected token ' + first)
throw createStrictSyntaxError(body, first)
}
}

Expand Down Expand Up @@ -133,6 +133,27 @@ function json (options) {
}
}

/**
* Create strict violation syntax error matching native error.
*
* @param {string} str
* @param {string} char
* @return {Error}
* @private
*/

function createStrictSyntaxError (str, char) {
var index = str.indexOf(char)
var partial = str.substring(0, index) + '#'

try {
JSON.parse(partial); /* istanbul ignore next */ throw new SyntaxError('strict violation')
} catch (e) {
e.message = e.message.replace('#', char)
return e
}
}

/**
* Get the first non-whitespace character in a string.
*
Expand Down
18 changes: 13 additions & 5 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send('{"user"')
.expect(400, /unexpected end/i, done)
.expect(400, parseError('{"user"'), done)
})

it('should handle Content-Length: 0', function (done) {
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send('{:')
.expect(400, /unexpected token/i, done)
.expect(400, parseError('{:'), done)
})

it('should 400 when invalid content-length', function (done) {
Expand Down Expand Up @@ -106,15 +106,15 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send('true')
.expect(400, /unexpected token/i, done)
.expect(400, parseError('#rue').replace('#', 't'), done)
})

it('should not parse primitives with leading whitespaces', function (done) {
request(server)
.post('/')
.set('Content-Type', 'application/json')
.send(' true')
.expect(400, /unexpected token/i, done)
.expect(400, parseError(' #rue').replace('#', 't'), done)
})

it('should allow leading whitespaces in JSON', function (done) {
Expand All @@ -132,7 +132,7 @@ describe('bodyParser.json()', function () {
.post('/')
.set('Content-Type', 'application/json')
.send('true')
.expect(400, /unexpected token/i, done)
.expect(400, parseError('#rue').replace('#', 't'), done)
})
})

Expand Down Expand Up @@ -487,3 +487,11 @@ function createServer (opts) {
})
})
}

function parseError (str) {
try {
JSON.parse(str); throw new SyntaxError('strict violation')
} catch (e) {
return e.message
}
}

0 comments on commit 448dc57

Please sign in to comment.