From 06a5182e6b1e51eb6723531b36d4ec173ec41268 Mon Sep 17 00:00:00 2001 From: Douglas Christopher Wilson Date: Mon, 7 Sep 2015 01:15:08 -0400 Subject: [PATCH] Fix JSON strict parse error to match syntax errors fixes #119 --- HISTORY.md | 1 + lib/types/json.js | 2 +- test/json.js | 8 ++++---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 0df81a53..4eeaf446 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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 diff --git a/lib/types/json.js b/lib/types/json.js index d736c79d..1a70e37c 100644 --- a/lib/types/json.js +++ b/lib/types/json.js @@ -80,7 +80,7 @@ function json(options) { if (first !== '{' && first !== '[') { debug('strict violation') - throw new Error('invalid json') + throw new SyntaxError('Unexpected token ' + first) } } diff --git a/test/json.js b/test/json.js index c0f2c1a8..e1c4e56f 100644 --- a/test/json.js +++ b/test/json.js @@ -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){ @@ -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){ @@ -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); }) })