Skip to content

Commit

Permalink
chore(plugin-restify): send body in event.request
Browse files Browse the repository at this point in the history
  • Loading branch information
djskinner committed Mar 10, 2022
1 parent 58c0ade commit e5a4e42
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/plugin-restify/src/restify.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ module.exports = {
req.bugsnag = requestClient

// extract request info and pass it to the relevant bugsnag properties
const { request, metadata } = getRequestAndMetadataFromReq(req)
requestClient.addMetadata('request', metadata)
requestClient.addOnError((event) => {
const { request, metadata } = getRequestAndMetadataFromReq(req)
event.request = { ...event.request, ...request }
requestClient.addMetadata('request', metadata)
}, true)

if (!client._config.autoDetectErrors) return next()
Expand Down Expand Up @@ -77,15 +77,16 @@ module.exports = {
}

const getRequestAndMetadataFromReq = req => {
const requestInfo = extractRequestInfo(req)
const { body, ...requestInfo } = extractRequestInfo(req)
return {
metadata: requestInfo,
request: {
body,
clientIp: requestInfo.clientIp,
headers: requestInfo.headers,
httpMethod: requestInfo.httpMethod,
url: requestInfo.url,
referer: requestInfo.referer
referer: requestInfo.referer // Not part of the notifier spec for request but leaving for backwards compatibility
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/node/features/fixtures/restify/scenarios/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var server = restify.createServer()

server.pre(middleware.requestHandler)

server.use(restify.plugins.bodyParser());

// If the server hasn't started sending something within 2 seconds
// it probably won't. So end the request and hurry the failing test
// along.
Expand Down Expand Up @@ -79,6 +81,10 @@ server.get('/handled', function (req, res, next) {
res.end('OK')
})

server.post('/bodytest', function (req, res, next) {
throw new Error('request body')
})

server.on('restifyError', middleware.errorHandler)

server.listen(80)
13 changes: 13 additions & 0 deletions test/node/features/restify.feature
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,16 @@ Scenario: a handled error passed to req.bugsnag.notify()
And the event "request.url" equals "http://restify/handled"
And the event "request.httpMethod" equals "GET"
And the event "request.clientIp" is not null

Scenario: adding body to request metadata
When I POST the data "data=in_request_body" to the URL "http://restify/bodytest"
And I wait to receive an error
Then the error is valid for the error reporting API version "4" for the "Bugsnag Node" notifier
And the event "unhandled" is true
And the event "severity" equals "error"
And the exception "errorClass" equals "Error"
And the exception "message" equals "request body"
And the exception "type" equals "nodejs"
And the "file" of stack frame 0 equals "scenarios/app.js"
And the event "request.body.data" equals "in_request_body"
And the event "request.httpMethod" equals "POST"

0 comments on commit e5a4e42

Please sign in to comment.