From f66eb82d830f037ef0a804caea7b89d5185f221f Mon Sep 17 00:00:00 2001 From: Nic Jansma Date: Thu, 10 Aug 2017 11:35:15 -0400 Subject: [PATCH] Errors: Remove certain file names from stacks --- plugins/errors.js | 47 ++++++++++++++++++++++++++++++--- tests/unit/04-plugins-errors.js | 46 ++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/plugins/errors.js b/plugins/errors.js index 8bead73d3..a9177373b 100644 --- a/plugins/errors.js +++ b/plugins/errors.js @@ -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 */ @@ -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; @@ -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; + } + } + } + } + } } } diff --git a/tests/unit/04-plugins-errors.js b/tests/unit/04-plugins-errors.js index 90197a62e..9b13d29f6 100644 --- a/tests/unit/04-plugins-errors.js +++ b/tests/unit/04-plugins-errors.js @@ -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); + }); }); }); });