Skip to content

Commit 961e058

Browse files
committed
simplify return values from request tracker plugin
1 parent 8f496ca commit 961e058

4 files changed

Lines changed: 23 additions & 26 deletions

File tree

packages/plugin-http-errors/http-errors.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,42 +93,38 @@ module.exports = (config = {}, global = window) => {
9393
const redactedUrl = redactQueryParameters(url, redactedKeys)
9494
const redactedQueryParams = parseQueryParams(redactedUrl)
9595

96-
// Extract request headers
97-
const requestHeaders = startContext.headers || {}
98-
99-
// Extract request body
100-
let requestBody = ''
101-
let requestBodyLength = 0
102-
const initialBody = startContext.body
103-
if (initialBody) {
104-
const bodyStr = String(initialBody)
105-
requestBody = truncate(initialBody, maxRequestSize)
106-
requestBodyLength = bodyStr.length // Report the original length before truncating
96+
// Redact request and response headers
97+
const requestHeaders = redactValues(startContext.headers || {}, redactedKeys)
98+
const responseHeaders = redactValues(endContext.headers || {}, redactedKeys)
99+
100+
// Truncate request body
101+
let requestBody
102+
let requestBodyLength
103+
if (startContext.body) {
104+
requestBody = truncate(startContext.body, maxRequestSize)
105+
requestBodyLength = startContext.body.length // Report the original length before truncating
107106
}
108107

109-
// Extract response headers
110-
const responseHeaders = endContext.headers || {}
111-
112-
// Extract response body - XHR only
108+
// Truncate response body - XHR only
113109
let responseBody
114110
let responseBodyLength
115-
if (endContext.xhr && endContext.xhr.responseText) {
116-
responseBody = truncate(endContext.xhr.responseText, maxRequestSize)
117-
responseBodyLength = endContext.xhr.responseText.length
111+
if (endContext.body) {
112+
responseBody = truncate(endContext.body, maxRequestSize)
113+
responseBodyLength = endContext.body.length // Report the original length before truncating
118114
}
119115

120116
// Create request and response objects for callback
121117
const requestObj = {
122118
url: redactedUrl,
123119
httpMethod: method,
124-
headers: redactValues(requestHeaders, redactedKeys),
120+
headers: requestHeaders,
125121
params: redactedQueryParams,
126122
body: requestBody,
127123
bodyLength: requestBodyLength
128124
}
129125
const responseObj = {
130126
statusCode: endContext.status,
131-
headers: redactValues(responseHeaders, redactedKeys),
127+
headers: responseHeaders,
132128
body: responseBody,
133129
bodyLength: responseBodyLength
134130
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ function createFetchTracker (global, options = {}) {
7070
endTime: Date.now(),
7171
status: response.status,
7272
state: 'success',
73-
headers: headersToObject(response.headers),
74-
response
73+
headers: headersToObject(response.headers)
7574
})
7675
return response
7776
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function createXhrTracker (global, options = {}) {
7272
status: this.status,
7373
state: 'success',
7474
headers: getResponseHeaders(),
75-
xhr: this
75+
body: this.responseText
7676
})
7777
}
7878

@@ -81,7 +81,7 @@ function createXhrTracker (global, options = {}) {
8181
endTime: Date.now(),
8282
state: 'error',
8383
headers: getResponseHeaders(),
84-
xhr: this
84+
body: this.responseText
8585
})
8686
}
8787

packages/request-tracker/test/request-tracker.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ describe('@bugsnag/request-tracker', () => {
268268
endTime: expect.any(Number),
269269
status: 200,
270270
state: 'success',
271-
xhr: xhr
271+
headers: {},
272+
body: undefined
272273
}))
273274
})
274275

@@ -288,7 +289,8 @@ describe('@bugsnag/request-tracker', () => {
288289
expect(onRequestEnd).toHaveBeenCalledWith(expect.objectContaining({
289290
endTime: expect.any(Number),
290291
state: 'error',
291-
xhr: xhr
292+
headers: {},
293+
body: undefined
292294
}))
293295
})
294296

0 commit comments

Comments
 (0)