Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin-express: fetch request body in onError #872

Merged
merged 5 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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