Skip to content

Commit 98557f9

Browse files
committed
change: handle headers in request-tracker
1 parent c74c00c commit 98557f9

6 files changed

Lines changed: 16 additions & 36 deletions

File tree

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
*/
55

66
const extractDomain = require('./lib/extract-domain')
7-
const headersToObject = require('./lib/headers-to-object')
7+
// const headersToObject = require('./lib/headers-to-object')
88
const parseQueryParams = require('./lib/parse-query-params')
99
const shouldCaptureStatusCode = require('./lib/should-capture-status-code')
1010
const truncate = require('./lib/truncate')
11-
const xhrResponseHeadersToObject = require('./lib/xhr-response-headers-to-object')
1211
const redactValues = require('./lib/redact-values')
1312
const redactQueryParameters = require('./lib/redact-query-parameters')
1413

@@ -82,11 +81,7 @@ module.exports = (config = {}, global = window) => {
8281

8382
function handleHttpError (startContext, endContext) {
8483
// Check if we should capture this status code
85-
if (!shouldCaptureStatusCode(normalizedStatusCodes, endContext.status)) {
86-
return
87-
}
88-
89-
// console.log({ startContext, endContext })
84+
if (!shouldCaptureStatusCode(normalizedStatusCodes, endContext.status)) return
9085

9186
try {
9287
// Extract request information
@@ -112,12 +107,7 @@ module.exports = (config = {}, global = window) => {
112107
}
113108

114109
// Extract response headers
115-
let responseHeaders = {}
116-
if (endContext.headers) {
117-
responseHeaders = xhrResponseHeadersToObject(endContext.headers) // XHR case
118-
} else if (endContext.response && endContext.response.headers) {
119-
responseHeaders = headersToObject(endContext.response.headers) // Fetch case
120-
}
110+
const responseHeaders = endContext.headers || {}
121111

122112
// Extract response body - XHR only
123113
let responseBody

packages/plugin-http-errors/lib/headers-to-object.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ function createFetchTracker (global, options = {}) {
7070
endTime: Date.now(),
7171
status: response.status,
7272
state: 'success',
73+
headers: headersToObject(response.headers),
7374
response
7475
})
7576
return response

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = function (headers) {
77
if (!headers) return {}
88

99
const obj = {}
10-
if (headers.entries) {
10+
if (typeof headers.entries === 'function') {
1111
for (const [key, value] of headers.entries()) {
1212
obj[key] = value
1313
}

packages/plugin-http-errors/lib/xhr-response-headers-to-object.js renamed to packages/request-tracker/lib/xhr-header-string-to-object.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2+
/**
3+
* Converts an XHR header string to an object
4+
* @param {string} headersString
5+
* @returns {Object}
6+
*/
17
module.exports = function (headersString) {
28
if (!headersString) return {}
39
const arr = headersString.trim().split(/[\r\n]+/)

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const RequestTracker = require('./request-tracker')
2+
const xhrHeaderStringToObject = require('./xhr-header-string-to-object')
23

34
/**
45
* Create XHR request tracker with singleton pattern
@@ -63,12 +64,14 @@ function createXhrTracker (global, options = {}) {
6364

6465
const { onRequestEnd } = tracker.start(context)
6566

67+
const getResponseHeaders = () => xhrHeaderStringToObject(this.getAllResponseHeaders())
68+
6669
const handleLoad = () => {
6770
onRequestEnd({
6871
endTime: Date.now(),
6972
status: this.status,
7073
state: 'success',
71-
headers: this.getAllResponseHeaders(),
74+
headers: getResponseHeaders(),
7275
xhr: this
7376
})
7477
}
@@ -77,7 +80,7 @@ function createXhrTracker (global, options = {}) {
7780
onRequestEnd({
7881
endTime: Date.now(),
7982
state: 'error',
80-
headers: this.getAllResponseHeaders(),
83+
headers: getResponseHeaders(),
8184
xhr: this
8285
})
8386
}

0 commit comments

Comments
 (0)