Skip to content
This repository was archived by the owner on Jun 13, 2023. It is now read-only.

Commit 4d13676

Browse files
authored
fix(utils): updated regex match and made failed matches safer (#599)
1 parent 25bfc0c commit 4d13676

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

src/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ module.exports.setConfig = function setConfig(configData) {
241241
config.disableHttpResponseBodyCapture = configData.disableHttpResponseBodyCapture;
242242
}
243243

244+
if (configData.allowErrMessageStatus) {
245+
config.allowErrMessageStatus = configData.allowErrMessageStatus;
246+
}
247+
244248
// Keys to automatically capture and label
245249
if (configData.keysToAllow) {
246250
config.keysToAllow = module.exports.prepareMatchingKeys(configData.keysToAllow);

src/utils.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,24 @@ function distinct(arr) {
208208
* Attempt to extract an HTTP Status Code from a String.
209209
* Matches and returns only 3-digit numbers in range 2XX-5XX.
210210
* @param {String} message A String containing a Status Code.
211-
* @return {Number} extracted Status Code. If not found -> 0, the only falsy Number.
212-
*/
213-
function extractStatusCode(message) {
214-
// matches a [Non-Digit], [2xx - 5xx], and a [Non-Digit]. globally & multiline.
215-
// \D ([2-5]\d{2}) \D /g m
216-
const statusCodeMatch = /\D([2-5]\d{2})\D/gm;
217-
const statusCodes = message
218-
.match(statusCodeMatch)
219-
.map(m => m.replace(statusCodeMatch, '$1'));
220-
if (statusCodes && statusCodes.length) {
221-
return Number(statusCodes[0]);
211+
* @param {Number} defaultCode The defaulting Status Code if none are found.
212+
* 0 is chosen as the only falsy Number to avoid being added.
213+
* @return {Number} extracted Status Code.
214+
*/
215+
function extractStatusCode(message, defaultCode = 0) {
216+
// matches a [Non-Digit or SOL], [2xx - 5xx], and a [Non-Digit or EOL]. globally & multiline.
217+
// (?:^|\D) ([2-5]\d{2}) (?:$|\D) /g m
218+
const statusCodeMatch = /(?:^|\D)([2-5]\d{2})(?:$|\D)/gm;
219+
const matches = message
220+
.match(statusCodeMatch);
221+
if (matches === null || !matches.length) {
222+
debugLog('Could not extract Status Code from Message:', message);
223+
debugLog('Defaulting Status to', defaultCode);
224+
return defaultCode;
222225
}
223-
return 0;
226+
const statusCodes = matches
227+
.map(m => m.replace(statusCodeMatch, '$1'));
228+
return Number(statusCodes[0]);
224229
}
225230

226231
module.exports.createTimestampFromTime = createTimestampFromTime;

test/unit_tests/test_config.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { expect } = require('chai');
22
const consts = require('../../src/consts.js');
33
const config = require('../../src/config.js');
4+
const utils = require('../../src/utils.js');
45

56

67
describe('tracer config tests', () => {
@@ -117,6 +118,47 @@ describe('tracer config tests', () => {
117118
expect(config.getConfig()).to.contain({ decodeHTTP: false });
118119
});
119120

121+
it('setConfig: set allowErrMessageStatus to true', () => {
122+
config.setConfig({ allowErrMessageStatus: true });
123+
expect(config.getConfig()).to.contain({ allowErrMessageStatus: true });
124+
});
125+
126+
it('setConfig: set allowErrMessageStatus and match error message', () => {
127+
config.setConfig({ allowErrMessageStatus: true });
128+
129+
const testCode = 324;
130+
[
131+
`{errorMessage: TEST-MESSAGE, statusCode: ${testCode}}`,
132+
`errorMessage: TEST-MESSAGE, statusCode: ${testCode}`,
133+
`{statusCode:${testCode}}`,
134+
`{statusCode:${testCode}}`,
135+
`Error - ${testCode}`,
136+
`${testCode}`,
137+
`${testCode}: errorMessage`,
138+
`${testCode} - error has a message`,
139+
]
140+
.forEach((m) => {
141+
const statusCode = utils.extractStatusCode(m);
142+
expect(statusCode).to.be.equal(testCode);
143+
});
144+
});
145+
146+
it('setConfig: set allowErrMessageStatus without matches', () => {
147+
config.setConfig({ allowErrMessageStatus: true });
148+
149+
const defaultStatusCode = 900;
150+
[
151+
'test message',
152+
'No Status Code Found',
153+
'4123 is not a Status Code',
154+
'22 is also not one.',
155+
]
156+
.forEach((m) => {
157+
const statusCode = utils.extractStatusCode(m, defaultStatusCode);
158+
expect(statusCode).to.be.equal(defaultStatusCode);
159+
});
160+
});
161+
120162
it('setConfig: set addReturnValue to false', () => {
121163
config.setConfig({ addReturnValue: false });
122164
expect(config.getConfig()).to.contain({ addReturnValue: false });

0 commit comments

Comments
 (0)