Skip to content

Commit 85e48ff

Browse files
committed
Handle non-standard leading whitespace on V8 stack frames (occurs when users
have defined Error.prepareStackTrace). Also, do not format stack trace if every frame is an unrecognized format. Fixes issue 7994.
1 parent 13778d7 commit 85e48ff

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

javascript/webdriver/stacktrace.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ webdriver.stacktrace.V8_LOCATION_PATTERN_ = ' (?:\\((.*)\\)|(.*))';
353353
* @private {!RegExp}
354354
* @const
355355
*/
356-
webdriver.stacktrace.V8_STACK_FRAME_REGEXP_ = new RegExp('^ at' +
356+
webdriver.stacktrace.V8_STACK_FRAME_REGEXP_ = new RegExp('^\\s+at' +
357+
// Prevent intersections with IE10 stack frame regex.
358+
'(?! (?:Anonymous function|Global code|eval code) )' +
357359
'(?:' + webdriver.stacktrace.V8_FUNCTION_CALL_PATTERN_ + ')?' +
358360
webdriver.stacktrace.V8_LOCATION_PATTERN_ + '$');
359361

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

596+
// If the original stack is in an unexpected format, our formatted stack
597+
// trace will be a bunch of " at <anonymous>" lines. If this is the case,
598+
// just return the error unmodified to avoid losing information. This is
599+
// necessary since the user may have defined a custom stack formatter in
600+
// V8 via Error.prepareStackTrace. See issue 7994.
601+
var isAnonymousFrame = function(frame) {
602+
return frame.toString() === ' at <anonymous>';
603+
};
604+
if (frames.length && goog.array.every(frames, isAnonymousFrame)) {
605+
return error;
606+
}
607+
594608
// Older versions of IE simply return [object Error] for toString(), so
595609
// only use that as a last resort.
596610
var errorStr = '';

javascript/webdriver/test/stacktrace_test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ function testParseStackFrameInV8() {
165165
' at Object.assert (http://bar:4000/bar.js?value=(a):150:3)',
166166
new webdriver.stacktrace.Frame('Object', 'assert', '',
167167
'http://bar:4000/bar.js?value=(a):150:3'));
168+
169+
assertStackFrame('Frame with non-standard leading whitespace (issue 7994)',
170+
' at module.exports.runCucumber (/local/dir/path)',
171+
new webdriver.stacktrace.Frame('module.exports', 'runCucumber', '',
172+
'/local/dir/path'));
168173
}
169174

170175
function testParseStackFrameInOpera() {
@@ -468,6 +473,19 @@ function testFormatsUsingNameAndMessageIfAvailable() {
468473
assertEquals('TypeError: boom\n', ret.stack);
469474
}
470475

476+
function testDoesNotFormatErrorIfOriginalStacktraceIsInAnUnexpectedFormat() {
477+
var error = Error('testing');
478+
var stack = error.stack = [
479+
'Error: testing',
480+
'..> at Color.red (http://x:1234)',
481+
'..> at Foo.bar (http://y:5678)'
482+
].join('\n');
483+
484+
var ret = webdriver.stacktrace.format(error);
485+
assertEquals(ret, error);
486+
assertEquals(stack, error.stack);
487+
}
488+
471489
function testParseStackFrameInIE10() {
472490
assertStackFrame('name and path',
473491
' at foo (http://bar:4000/bar.js:150:3)',

0 commit comments

Comments
 (0)