Skip to content

Commit 8e45670

Browse files
authored
Merge pull request #2648 from bugsnag/http-errors-suggestions-and-fixes
Http errors suggestions and fixes
2 parents 8b97f5a + 646ae6f commit 8e45670

12 files changed

Lines changed: 54 additions & 36 deletions

File tree

packages/plugin-http-errors/README.md

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,20 @@ yarn add @bugsnag/plugin-http-errors
1414

1515
```js
1616
import Bugsnag from '@bugsnag/js'
17-
import createHttpErrorPlugin from '@bugsnag/plugin-http-errors'
18-
19-
const plugin = createHttpErrorPlugin({
20-
httpErrorCodes: [401, { min: 404, max: 499 }], // handle individual error codes or ranges of error codes
21-
maxRequestSize: 5_000, // don't capture requests over 5kb
22-
onHttpError: ({ request, response }) => {
23-
// Only handle 5xx errors
24-
if (response.statusCode < 500 || response.statusCode > 599) return false
25-
26-
// Exclude specific domains
27-
if (request.url.indexOf('redacted.domain.com') === 0) return false
28-
29-
// Update properties on the reported request and response
30-
request.url = '[REDACTED]'
31-
response.statusCode = 418
32-
33-
return true // return value will determine whether the error is reported
34-
}
35-
})
17+
import BugsnagPluginHttpErrors from '@bugsnag/plugin-http-errors'
3618

3719
Bugsnag.start({
38-
apiKey: 'YOUR_API_KEY',
39-
plugins: [plugin]
20+
apiKey: 'YOUR_API_KEY_HERE',
21+
plugins: [BugsnagPluginHttpErrors({
22+
httpErrorCodes = [400, 401, { min: 450: max 499 }], // Status codes to report as errors
23+
maxRequestSize = 20_000, // Truncate the request and response body over this size (in kb)
24+
onHttpError: ({ request, response }) => {
25+
request.headers['x-custom-header'] = 'value' // Modify any request values before sending
26+
response.body = 'custom body' // Modify any response values before sending
27+
28+
return false // Don't report this request an as error
29+
}
30+
})]
4031
})
4132
```
4233

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
const redactValues = require('./redact-values')
2-
const defaultBaseURL = 'http://invalid-base.com'
2+
3+
function isAbsoluteURL (url) {
4+
try {
5+
// eslint-disable-next-line no-new
6+
new URL(url)
7+
return true
8+
} catch (e) {
9+
return false
10+
}
11+
}
312

413
module.exports = function (url, redactedKeys) {
5-
const urlObj = new URL(url, defaultBaseURL) // base needed for relative URLs
14+
const isAbsolute = isAbsoluteURL(url)
15+
const base = isAbsolute ? undefined : 'http://localhost'
16+
17+
// Parse the URL - use a base only for relative URLs
18+
const urlObj = new URL(url, base)
619
const params = new URLSearchParams(urlObj.search)
720

821
// Convert URLSearchParams to object without using Object.fromEntries()
@@ -13,8 +26,13 @@ module.exports = function (url, redactedKeys) {
1326

1427
const redactedParams = redactValues(paramsObject, redactedKeys)
1528
urlObj.search = new URLSearchParams(redactedParams).toString()
16-
const urlString = decodeURI(urlObj.toString())
17-
return urlString.startsWith(defaultBaseURL)
18-
? urlString.slice(defaultBaseURL.length)
19-
: urlString
29+
30+
// Return appropriate format based on original URL type
31+
if (isAbsolute) {
32+
return decodeURI(urlObj.toString())
33+
}
34+
35+
// For relative URLs, return only the path + search + hash components
36+
const relativePart = urlObj.pathname + urlObj.search + urlObj.hash
37+
return decodeURI(relativePart)
2038
}

packages/plugin-network-breadcrumbs/test/network-breadcrumbs.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class XMLHttpRequest {
2929
this?._listeners[evt].push(listener)
3030
}
3131

32+
getAllResponseHeaders () {
33+
return ''
34+
}
35+
3236
removeEventListener (evt: 'load'| 'error', listener: () => void) {
3337
for (let i = this?._listeners?.[evt]?.length ?? 0 - 1; i >= 0; i--) {
3438
if (listener.name === this?._listeners?.[evt]?.[i]?.name) delete this?._listeners[evt][i]

packages/request-tracker/lib/fetch-tracker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function createFetchTracker (global, options = {}) {
5858
type: 'fetch',
5959
input: urlOrRequest,
6060
headers: requestHeaders,
61-
body: options.body
61+
body: options ? options.body : undefined
6262
}
6363

6464
const { onRequestEnd } = tracker.start(context)

packages/request-tracker/lib/headers-to-object.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ module.exports = function (headers) {
88

99
const obj = {}
1010
if (typeof headers.entries === 'function') {
11-
for (const [key, value] of headers.entries()) {
11+
var iterator = headers.entries()
12+
var entry = iterator.next()
13+
while (!entry.done) {
14+
var key = entry.value[0]
15+
var value = entry.value[1]
1216
obj[key] = value
17+
entry = iterator.next()
1318
}
1419
} else if (headers.forEach) {
1520
headers.forEach((value, key) => {

packages/request-tracker/lib/xhr-tracker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ function createXhrTracker (global, options = {}) {
105105
tracker._restore = () => {
106106
global.XMLHttpRequest.prototype.open = originalOpen
107107
global.XMLHttpRequest.prototype.send = originalSend
108+
global.XMLHttpRequest.prototype.setRequestHeader = originalSetRequestHeader
108109
delete global.__bugsnag_xhr_tracker__
109110
}
110111
}

test/browser/features/fixtures/handled/webpack3/webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = {
1414
filename: '[name].js'
1515
},
1616
plugins: [
17-
new es3ifyPlugin(),
18-
new webpack.optimize.UglifyJsPlugin({ compress: false, mangle: false, ie8: true })
17+
new es3ifyPlugin()
18+
// UglifyJs plugin disabled due to ES6 compatibility issues
1919
]
2020
}

test/browser/features/fixtures/handled/webpack4/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ module.exports = {
1515
},
1616
plugins: [
1717
new es3ifyPlugin(),
18-
new UglifyJsPlugin({ sourceMap: true, uglifyOptions: { compress: false, mangle: false, ie8: true } })
18+
new UglifyJsPlugin({ sourceMap: true, uglifyOptions: { compress: false, mangle: false } })
1919
]
2020
}

test/browser/features/fixtures/http_errors/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ module.exports = {
3232
]
3333
},
3434
plugins: [
35-
new UglifyJsPlugin({ sourceMap: true, uglifyOptions: { compress: false, mangle: false, ie8: true } })
35+
new UglifyJsPlugin({ sourceMap: true, uglifyOptions: { compress: false, mangle: false } })
3636
]
3737
}

test/browser/features/fixtures/plugin_react/webpack4/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ module.exports = {
2424
]
2525
},
2626
plugins: [
27-
new UglifyJsPlugin({ sourceMap: true, uglifyOptions: { compress: false, mangle: false, ie8: true } })
27+
new UglifyJsPlugin({ sourceMap: true, uglifyOptions: { compress: false, mangle: false } })
2828
]
2929
}

0 commit comments

Comments
 (0)