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

Commit

Permalink
fix(utils): updated regex match and made failed matches safer (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnathaniel authored Jun 21, 2022
1 parent 25bfc0c commit 4d13676
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ module.exports.setConfig = function setConfig(configData) {
config.disableHttpResponseBodyCapture = configData.disableHttpResponseBodyCapture;
}

if (configData.allowErrMessageStatus) {
config.allowErrMessageStatus = configData.allowErrMessageStatus;
}

// Keys to automatically capture and label
if (configData.keysToAllow) {
config.keysToAllow = module.exports.prepareMatchingKeys(configData.keysToAllow);
Expand Down
29 changes: 17 additions & 12 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,24 @@ function distinct(arr) {
* Attempt to extract an HTTP Status Code from a String.
* Matches and returns only 3-digit numbers in range 2XX-5XX.
* @param {String} message A String containing a Status Code.
* @return {Number} extracted Status Code. If not found -> 0, the only falsy Number.
*/
function extractStatusCode(message) {
// matches a [Non-Digit], [2xx - 5xx], and a [Non-Digit]. globally & multiline.
// \D ([2-5]\d{2}) \D /g m
const statusCodeMatch = /\D([2-5]\d{2})\D/gm;
const statusCodes = message
.match(statusCodeMatch)
.map(m => m.replace(statusCodeMatch, '$1'));
if (statusCodes && statusCodes.length) {
return Number(statusCodes[0]);
* @param {Number} defaultCode The defaulting Status Code if none are found.
* 0 is chosen as the only falsy Number to avoid being added.
* @return {Number} extracted Status Code.
*/
function extractStatusCode(message, defaultCode = 0) {
// matches a [Non-Digit or SOL], [2xx - 5xx], and a [Non-Digit or EOL]. globally & multiline.
// (?:^|\D) ([2-5]\d{2}) (?:$|\D) /g m
const statusCodeMatch = /(?:^|\D)([2-5]\d{2})(?:$|\D)/gm;
const matches = message
.match(statusCodeMatch);
if (matches === null || !matches.length) {
debugLog('Could not extract Status Code from Message:', message);
debugLog('Defaulting Status to', defaultCode);
return defaultCode;
}
return 0;
const statusCodes = matches
.map(m => m.replace(statusCodeMatch, '$1'));
return Number(statusCodes[0]);
}

module.exports.createTimestampFromTime = createTimestampFromTime;
Expand Down
42 changes: 42 additions & 0 deletions test/unit_tests/test_config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { expect } = require('chai');
const consts = require('../../src/consts.js');
const config = require('../../src/config.js');
const utils = require('../../src/utils.js');


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

it('setConfig: set allowErrMessageStatus to true', () => {
config.setConfig({ allowErrMessageStatus: true });
expect(config.getConfig()).to.contain({ allowErrMessageStatus: true });
});

it('setConfig: set allowErrMessageStatus and match error message', () => {
config.setConfig({ allowErrMessageStatus: true });

const testCode = 324;
[
`{errorMessage: TEST-MESSAGE, statusCode: ${testCode}}`,
`errorMessage: TEST-MESSAGE, statusCode: ${testCode}`,
`{statusCode:${testCode}}`,
`{statusCode:${testCode}}`,
`Error - ${testCode}`,
`${testCode}`,
`${testCode}: errorMessage`,
`${testCode} - error has a message`,
]
.forEach((m) => {
const statusCode = utils.extractStatusCode(m);
expect(statusCode).to.be.equal(testCode);
});
});

it('setConfig: set allowErrMessageStatus without matches', () => {
config.setConfig({ allowErrMessageStatus: true });

const defaultStatusCode = 900;
[
'test message',
'No Status Code Found',
'4123 is not a Status Code',
'22 is also not one.',
]
.forEach((m) => {
const statusCode = utils.extractStatusCode(m, defaultStatusCode);
expect(statusCode).to.be.equal(defaultStatusCode);
});
});

it('setConfig: set addReturnValue to false', () => {
config.setConfig({ addReturnValue: false });
expect(config.getConfig()).to.contain({ addReturnValue: false });
Expand Down

0 comments on commit 4d13676

Please sign in to comment.