Skip to content

Commit

Permalink
Merge pull request #872 from bugsnag/fix-express-request-body
Browse files Browse the repository at this point in the history
plugin-express: fetch request body in onError
  • Loading branch information
bengourley authored Jun 15, 2020
2 parents a92275d + 4811928 commit 127ffdc
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 38 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## TBD

### Fixed
- (plugin-express): Ensure `req.body` is always present in metadata by collecting it at the last possible moment [#872](https://github.com/bugsnag/bugsnag-js/pull/872)

## 7.1.1 (2020-05-26)

Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-express/src/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,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 { metadata, request } = getRequestAndMetadataFromReq(req)
event.request = { ...event.request, ...request }
requestClient.addMetadata('request', metadata)
}, true)

// unhandled errors caused by this request
Expand Down
13 changes: 13 additions & 0 deletions test/node/features/express.feature
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,16 @@ Scenario: a handled error passed to req.bugsnag.notify()
And the event "request.url" equals "http://express/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://express/bodytest"
And I wait to receive a request
Then the request 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 "metaData.request.body.data" equals "in_request_body"
And the event "request.httpMethod" equals "POST"
200 changes: 164 additions & 36 deletions test/node/features/fixtures/express/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/node/features/fixtures/express/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "bugsnag-test",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.16.3"
}
}
6 changes: 6 additions & 0 deletions test/node/features/fixtures/express/scenarios/app.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
var Bugsnag = require('@bugsnag/node')
var bugsnagExpress = require('@bugsnag/plugin-express')
var express = require('express')
var bodyParser = require('body-parser')

Bugsnag.start({
apiKey: process.env.BUGSNAG_API_KEY,
endpoints: {
notify: process.env.BUGSNAG_NOTIFY_ENDPOINT,
sessions: process.env.BUGSNAG_SESSIONS_ENDPOINT
},
autoTrackSessions: false,
plugins: [bugsnagExpress]
})

Expand Down Expand Up @@ -69,6 +71,10 @@ app.get('/handled', function (req, res, next) {
res.end('OK')
})

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

app.use(middleware.errorHandler)

app.listen(80)
9 changes: 9 additions & 0 deletions test/node/features/steps/server_fixture_request_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'net/http'

# Attempts to POST a string of urlencoded data to a server.
#
# @step_input reqbody [String] urlencoded data to send.
# @step_input url [String] The URL to post data to.
When("I POST the data {string} to the URL {string}") do |reqbody, url|
Net::HTTP.post(URI(url), reqbody)
end

0 comments on commit 127ffdc

Please sign in to comment.