Skip to content

Commit

Permalink
Include the "body" property on verify errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Sep 8, 2017
1 parent bcece12 commit a7e7937
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ unreleased
==========

* Fix JSON strict violation error to match native parse error
* Include the `body` property on verify errors
* Use `http-errors` to set status code on errors
* deps: [email protected]
* deps: [email protected]
Expand Down
15 changes: 7 additions & 8 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,25 @@ function read (req, res, next, parse, debug, options) {
debug('verify body')
verify(req, res, body, encoding)
} catch (err) {
next(createError(403, err))
next(createError(403, err, {
body: body
}))
return
}
}

// parse
var str
var str = body
try {
debug('parse body')
str = typeof body !== 'string' && encoding !== null
? iconv.decode(body, encoding)
: body
req.body = parse(str)
} catch (err) {
// istanbul ignore next
err.body = str === undefined
? body
: str

next(createError(400, err))
next(createError(400, err, {
body: str
}))
return
}

Expand Down
13 changes: 13 additions & 0 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@ describe('bodyParser.json()', function () {
.expect(400, 'no arrays', done)
})

it('should include original body on error object', function (done) {
var server = createServer({verify: function (req, res, buf) {
if (buf[0] === 0x5b) throw new Error('no arrays')
}})

request(server)
.post('/')
.set('Content-Type', 'application/json')
.set('X-Error-Property', 'body')
.send('["tobi"]')
.expect(403, '["tobi"]', done)
})

it('should allow pass-through', function (done) {
var server = createServer({verify: function (req, res, buf) {
if (buf[0] === 0x5b) throw new Error('no arrays')
Expand Down

0 comments on commit a7e7937

Please sign in to comment.