Skip to content

Commit

Permalink
Handle non-standard leading whitespace on V8 stack frames (occurs whe…
Browse files Browse the repository at this point in the history
…n users

have defined Error.prepareStackTrace).

Also, do not format stack trace if every frame is an unrecognized format.

Fixes issue 7994.
  • Loading branch information
jleyba committed Oct 8, 2014
1 parent 13778d7 commit 85e48ff
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
16 changes: 15 additions & 1 deletion javascript/webdriver/stacktrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ webdriver.stacktrace.V8_LOCATION_PATTERN_ = ' (?:\\((.*)\\)|(.*))';
* @private {!RegExp}
* @const
*/
webdriver.stacktrace.V8_STACK_FRAME_REGEXP_ = new RegExp('^ at' +
webdriver.stacktrace.V8_STACK_FRAME_REGEXP_ = new RegExp('^\\s+at' +
// Prevent intersections with IE10 stack frame regex.
'(?! (?:Anonymous function|Global code|eval code) )' +
'(?:' + webdriver.stacktrace.V8_FUNCTION_CALL_PATTERN_ + ')?' +
webdriver.stacktrace.V8_LOCATION_PATTERN_ + '$');

Expand Down Expand Up @@ -591,6 +593,18 @@ webdriver.stacktrace.format = function(error) {
var stack = webdriver.stacktrace.getStack_(error);
var frames = webdriver.stacktrace.parse_(stack);

// If the original stack is in an unexpected format, our formatted stack
// trace will be a bunch of " at <anonymous>" lines. If this is the case,
// just return the error unmodified to avoid losing information. This is
// necessary since the user may have defined a custom stack formatter in
// V8 via Error.prepareStackTrace. See issue 7994.
var isAnonymousFrame = function(frame) {
return frame.toString() === ' at <anonymous>';
};
if (frames.length && goog.array.every(frames, isAnonymousFrame)) {
return error;
}

// Older versions of IE simply return [object Error] for toString(), so
// only use that as a last resort.
var errorStr = '';
Expand Down
18 changes: 18 additions & 0 deletions javascript/webdriver/test/stacktrace_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ function testParseStackFrameInV8() {
' at Object.assert (http://bar:4000/bar.js?value=(a):150:3)',
new webdriver.stacktrace.Frame('Object', 'assert', '',
'http://bar:4000/bar.js?value=(a):150:3'));

assertStackFrame('Frame with non-standard leading whitespace (issue 7994)',
' at module.exports.runCucumber (/local/dir/path)',
new webdriver.stacktrace.Frame('module.exports', 'runCucumber', '',
'/local/dir/path'));
}

function testParseStackFrameInOpera() {
Expand Down Expand Up @@ -468,6 +473,19 @@ function testFormatsUsingNameAndMessageIfAvailable() {
assertEquals('TypeError: boom\n', ret.stack);
}

function testDoesNotFormatErrorIfOriginalStacktraceIsInAnUnexpectedFormat() {
var error = Error('testing');
var stack = error.stack = [
'Error: testing',
'..> at Color.red (http://x:1234)',
'..> at Foo.bar (http://y:5678)'
].join('\n');

var ret = webdriver.stacktrace.format(error);
assertEquals(ret, error);
assertEquals(stack, error.stack);
}

function testParseStackFrameInIE10() {
assertStackFrame('name and path',
' at foo (http://bar:4000/bar.js:150:3)',
Expand Down

0 comments on commit 85e48ff

Please sign in to comment.