Skip to content

Commit

Permalink
Errors: Remove certain file names from stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
nicjansma committed Apr 4, 2018
1 parent a5f3789 commit f66eb82
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
47 changes: 43 additions & 4 deletions plugins/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@
"BOOMR_plugins_errors_"
];

// functions to strip if they match a STACK_FILENAME_MATCH
var STACK_FUNCTIONS_REMOVE_IF_FILENAME_MATCH = [
"Object.send",
"b.send",
"wrap",
"Anonymous function"
];

// files that will match for STACK_FUNCTIONS_REMOVE_IF_FILENAME_MATCH
var STACK_FILENAME_MATCH = [
"/boomerang"
];

/**
* Maximum size, in characters, of stack to capture
*/
Expand Down Expand Up @@ -389,8 +402,8 @@
* @returns {BoomerangError} Error
*/
BoomerangError.fromError = function(error, via, source) {
var frame, frames, lastFrame, forceUpdate = false, i, j,
now = BOOMR.now();
var frame, frames, lastFrame, forceUpdate = false, i, j, k,
now = BOOMR.now(), skipThis, thisFrame, thisFn;

if (!error) {
return null;
Expand Down Expand Up @@ -446,17 +459,43 @@

// remove our error wrappers from the stack
for (i = 0; i < frames.length; i++) {
if (frames[i].functionName) {
thisFrame = frames[i];
thisFn = thisFrame.functionName;
skipThis = false;

// strip boomerang function names
if (thisFn) {
for (j = 0; j < STACK_FUNCTIONS_REMOVE.length; j++) {
if (frames[i].functionName.indexOf(STACK_FUNCTIONS_REMOVE[j]) !== -1) {
if (thisFn.indexOf(STACK_FUNCTIONS_REMOVE[j]) !== -1) {
frames.splice(i, 1);
forceUpdate = true;

// outloop continues with the next element
i--;
skipThis = true;
break;
}
}

// strip additional functions if they also match a file
if (!skipThis && thisFrame.fileName) {
for (j = 0; j < STACK_FILENAME_MATCH.length; j++) {
if (thisFrame.fileName.indexOf(STACK_FILENAME_MATCH[j]) !== -1) {
// this file name matches, see if any of the matching functions also do
for (k = 0; k < STACK_FUNCTIONS_REMOVE_IF_FILENAME_MATCH.length; k++) {
if (thisFn.indexOf(STACK_FUNCTIONS_REMOVE_IF_FILENAME_MATCH[k]) !== -1) {
frames.splice(i, 1);
forceUpdate = true;

// outloop continues with the next element
i--;
skipThis = true;
break;
}
}
}
}
}
}
}

Expand Down
46 changes: 46 additions & 0 deletions tests/unit/04-plugins-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,52 @@ describe("BOOMR.plugins.Errors", function() {
assert.include(be.stack, "Test");
}
});

it("Should strip Boomerang functions from an Error", function() {
var stack = "Error: Test\n" +
" at okFunction1 (okFile1:1:1)\n" + // keep
" at okFunction2 (okFile2:2:2)\n" + // keep
" at createStackForSend (boomr:1)\n" +
" at BOOMR.window.console.error (boomr:1)\n" +
" at BOOMR.plugins.Errors.init (a.html:1)\n" +
" at BOOMR.window.onerror (boomr:1)\n" +
" at BOOMR_plugins_errors_console (boomr:1)\n" +
" at okFunction3 (okFile3:3:3)\n" + // keep
" at BOOMR_plugins_errors_console (boomr:1)\n" +
" at okFunction4 (okFile4:4:4)\n" + // keep
" at Object.send (/a/boomerang/b/:1:2)\n" +
" at wrap/< (/a/boomerang/b/:1:2)\n" +
" at Anonymous function (/a/boomerang/b/:1:2)\n" +
" at Object.send (/a/noboomr/b/:1:2)\n" + // keep
" at wrap/< (/a/noboomr/b/:1:2)"; // keep
var err = {
stack: stack
};

var parsed = BOOMR.plugins.Errors.BoomerangError.fromError(err);

// only 4 frames should be left
assert.equal(parsed.frames.length, 6);

// first 4 matches are all okFunctionN at okFileN:N:n
for (var i = 1; i <= 4; i++) {
assert.equal(parsed.frames[i - 1].functionName, "okFunction" + i);
assert.equal(parsed.frames[i - 1].fileName, "okFile" + i);
assert.equal(parsed.frames[i - 1].lineNumber, i);
assert.equal(parsed.frames[i - 1].columnNumber, i);
}

// then Object.send and wrap match
assert.equal(parsed.frames[4].functionName, "Object.send");
assert.equal(parsed.frames[4].fileName, "/a/noboomr/b/");
assert.equal(parsed.frames[4].lineNumber, 1);
assert.equal(parsed.frames[4].columnNumber, 2);

assert.equal(parsed.frames[5].functionName, "wrap/<");
assert.equal(parsed.frames[5].fileName, "/a/noboomr/b/");
assert.equal(parsed.frames[5].lineNumber, 1);
assert.equal(parsed.frames[5].columnNumber, 2);
});
});
});
});

0 comments on commit f66eb82

Please sign in to comment.