Skip to content

Commit 871140a

Browse files
committed
Parse Node stacktraces correctly for filenames with spaces
1 parent 2d84c60 commit 871140a

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

error-stack-parser.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,18 @@
5858
// Throw away eval information until we implement stacktrace.js/stackframe#8
5959
line = line.replace(/eval code/g, 'eval').replace(/(\(eval at [^\()]*)|(\)\,.*$)/g, '');
6060
}
61-
var tokens = line.replace(/^\s+/, '').replace(/\(eval code/g, '(').split(/\s+/).slice(1);
62-
var locationParts = this.extractLocation(tokens.pop());
61+
var sanitizedLine = line.replace(/^\s+/, '').replace(/\(eval code/g, '(');
62+
63+
// capture and preseve the parenthesized location "(/foo/my bar.js:12:87)" in
64+
// case it has spaces in it, as the string is split on \s+ later on
65+
var location = sanitizedLine.match(/ (\((.+):(\d+):(\d+)\)$)/);
66+
67+
// remove the parenthesized location from the line, if it was matched
68+
sanitizedLine = location ? sanitizedLine.replace(location[0], '') : sanitizedLine;
69+
70+
var tokens = sanitizedLine.split(/\s+/).slice(1);
71+
// if a location was matched, pass it to extractLocation() otherwise pop the last token
72+
var locationParts = this.extractLocation(location ? location[1] : tokens.pop());
6373
var functionName = tokens.join(' ') || undefined;
6474
var fileName = ['eval', '<anonymous>'].indexOf(locationParts[0]) > -1 ? undefined : locationParts[0];
6575

spec/error-stack-parser-spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,13 @@ describe('ErrorStackParser', function() {
234234
expect(stackframes[0].lineNumber).toBeUndefined();
235235
expect(stackframes[0].columnNumber).toBeUndefined();
236236
});
237+
238+
it('should handle spaces in Node.js stacks', function() {
239+
var stackframes = unit.parse(CapturedExceptions.NODE_WITH_SPACES);
240+
expect(stackframes.length).toBe(7);
241+
expect(stackframes[0].fileName).toEqual('/var/app/scratch/my project/index.js');
242+
expect(stackframes[0].lineNumber).toBe(2);
243+
expect(stackframes[0].columnNumber).toBe(9);
244+
});
237245
});
238246
});

spec/fixtures/captured-errors.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,17 @@ CapturedExceptions.EDGE_20_NESTED_EVAL = {
383383
' at speak (http://localhost:8080/file.js:25:17)\n' +
384384
' at Global code (http://localhost:8080/file.js:32:9)'
385385
};
386+
387+
CapturedExceptions.NODE_WITH_SPACES = {
388+
name: 'Error',
389+
message: '',
390+
stack: 'Error\n at Object.<anonymous> ' +
391+
'(/var/app/scratch/my ' +
392+
'project/index.js:2:9)\n at Module._compile ' +
393+
'(internal/modules/cjs/loader.js:774:30)\n at ' +
394+
'Object.Module._extensions..js (internal/modules/cjs/loader.js:785:10)\n ' +
395+
' at Module.load (internal/modules/cjs/loader.js:641:32)\n at ' +
396+
'Function.Module._load (internal/modules/cjs/loader.js:556:12)\n at ' +
397+
'Function.Module.runMain (internal/modules/cjs/loader.js:837:10)\n at ' +
398+
'internal/main/run_main_module.js:17:11'
399+
};

0 commit comments

Comments
 (0)