Skip to content

Commit

Permalink
Fix JSON strict parse error to match syntax errors
Browse files Browse the repository at this point in the history
fixes #119
  • Loading branch information
dougwilson committed Sep 7, 2015
1 parent 393fe31 commit 06a5182
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 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 parse error to match syntax errors
* Provide static `require` analysis in `urlencoded` parser
* deps: qs@5.0.0
* deps: type-is@~1.6.8
Expand Down
2 changes: 1 addition & 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 Error('invalid json')
throw new SyntaxError('Unexpected token ' + first)
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ describe('bodyParser.json()', function(){
request(server)
.post('/')
.set('Content-Type', 'application/json')
.send('{"foo')
.expect(400, done);
.send('{:')
.expect(400, 'Unexpected token :', done);
})

it('should 400 when invalid content-length', function(done){
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('bodyParser.json()', function(){
.post('/')
.set('Content-Type', 'application/json')
.send('true')
.expect(400, 'invalid json', done)
.expect(400, 'Unexpected token t', done)
})

it('should allow leading whitespaces in JSON', function(done){
Expand All @@ -135,7 +135,7 @@ describe('bodyParser.json()', function(){
.post('/')
.set('Content-Type', 'application/json')
.send('true')
.expect(400, done);
.expect(400, 'Unexpected token t', done);
})
})

Expand Down

0 comments on commit 06a5182

Please sign in to comment.