Skip to content

Commit

Permalink
feat: throw one type error for json parse strict = true (#74)
Browse files Browse the repository at this point in the history
Co-authored-by: dmytro.fedotov <[email protected]>
  • Loading branch information
fedotov and fedotov authored Aug 12, 2020
1 parent a847bfd commit 960986e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = async function(req, opts) {
if (!str) return {};
// strict JSON test
if (!strictJSONReg.test(str)) {
throw new Error('invalid JSON, only supports object and array');
throw new SyntaxError('invalid JSON, only supports object and array');
}
return JSON.parse(str);
}
Expand Down
2 changes: 2 additions & 0 deletions test/json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ describe('parse.json(req, opts)', function() {
try {
yield parse.json(this);
} catch (err) {
err.should.be.an.instanceOf(SyntaxError);
err.status.should.equal(400);
err.body.should.equal('{"foo": "bar');
done();
Expand Down Expand Up @@ -124,6 +125,7 @@ describe('parse.json(req, opts)', function() {
try {
yield parse.json(this, { strict: true });
} catch (err) {
err.should.be.an.instanceOf(SyntaxError);
err.status.should.equal(400);
err.body.should.equal('"foo"');
err.message.should.equal('invalid JSON, only supports object and array');
Expand Down

0 comments on commit 960986e

Please sign in to comment.