Skip to content

Commit af98955

Browse files
committed
refactor: reorder http errors operations
1 parent 1e0a150 commit af98955

1 file changed

Lines changed: 26 additions & 37 deletions

File tree

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

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
*/
55

66
const extractDomain = require('./lib/extract-domain')
7-
// const headersToObject = require('./lib/headers-to-object')
87
const parseQueryParams = require('./lib/parse-query-params')
8+
const redactQueryParameters = require('./lib/redact-query-parameters')
99
const shouldCaptureStatusCode = require('./lib/should-capture-status-code')
1010
const truncate = require('./lib/truncate')
11-
const redactValues = require('./lib/redact-values')
12-
const redactQueryParameters = require('./lib/redact-query-parameters')
1311

1412
const DEFAULT_HTTP_ERROR_CODES = [{ min: 400, max: 599 }]
1513
const DEFAULT_MAX_REQUEST_SIZE = 5000
@@ -39,7 +37,6 @@ module.exports = (config = {}, global = window) => {
3937
load: (client) => {
4038
// Try to get existing request tracker
4139
let requestTrackerPlugin = client.getPlugin('requestTracker')
42-
const redactedKeys = client._config.redactedKeys || []
4340

4441
// Auto-load request tracker if not present
4542
if (!requestTrackerPlugin) {
@@ -86,47 +83,24 @@ module.exports = (config = {}, global = window) => {
8683
try {
8784
// Extract request information
8885
const url = startContext.url
86+
const requestParams = parseQueryParams(url)
8987
const method = startContext.method
9088
const domain = extractDomain(url)
9189

92-
// Redact query parameters in URL
93-
const redactedUrl = redactQueryParameters(url, redactedKeys)
94-
const redactedQueryParams = parseQueryParams(redactedUrl)
95-
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
106-
}
107-
108-
// Truncate response body - XHR only
109-
let responseBody
110-
let responseBodyLength
111-
if (endContext.body) {
112-
responseBody = truncate(endContext.body, maxRequestSize)
113-
responseBodyLength = endContext.body.length // Report the original length before truncating
114-
}
115-
11690
// Create request and response objects for callback
11791
const requestObj = {
118-
url: redactedUrl,
119-
httpMethod: method,
120-
headers: requestHeaders,
121-
params: redactedQueryParams,
122-
body: requestBody,
123-
bodyLength: requestBodyLength
92+
url: startContext.url,
93+
httpMethod: startContext.method,
94+
headers: startContext.headers,
95+
params: requestParams,
96+
body: startContext.body,
97+
bodyLength: startContext.body ? startContext.body.length : undefined
12498
}
12599
const responseObj = {
126100
statusCode: endContext.status,
127-
headers: responseHeaders,
128-
body: responseBody,
129-
bodyLength: responseBodyLength
101+
headers: endContext.headers,
102+
body: endContext.body,
103+
bodyLength: endContext.body ? endContext.body.length : undefined
130104
}
131105

132106
// Call onHttpError callback if provided
@@ -139,6 +113,21 @@ module.exports = (config = {}, global = window) => {
139113
}
140114
}
141115

116+
// Truncate request body
117+
if (requestObj.body) {
118+
requestObj.body = truncate(requestObj.body, maxRequestSize)
119+
}
120+
121+
// Truncate response body - XHR only
122+
if (responseObj.body) {
123+
responseObj.body = truncate(responseObj.body, maxRequestSize)
124+
}
125+
126+
// Strip query parameters from URL
127+
if (requestObj.url !== '[REDACTED]') {
128+
requestObj.url = redactQueryParameters(requestObj.url, client._config.redactedKeys)
129+
}
130+
142131
// Create error and notify
143132
const error = new Error(`${responseObj.statusCode}: ${requestObj.url}`)
144133
error.name = 'HTTPError'

0 commit comments

Comments
 (0)