Skip to content

Commit

Permalink
Merge pull request #66 from Aleksey28/parse-file-name-for-node
Browse files Browse the repository at this point in the history
fix: fixed parsing for node
  • Loading branch information
niftylettuce authored Feb 2, 2022
2 parents 802ff4e + 2bd8aa1 commit 1c9261a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
14 changes: 7 additions & 7 deletions error-stack-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@
return filtered.map(function(line) {
if (line.indexOf('(eval ') > -1) {
// Throw away eval information until we implement stacktrace.js/stackframe#8
line = line.replace(/eval code/g, 'eval').replace(/(\(eval at [^()]*)|(\),.*$)/g, '');
line = line.replace(/eval code/g, 'eval').replace(/(\(eval at [^()]*)|(,.*$)/g, '');
}
var sanitizedLine = line.replace(/^\s+/, '').replace(/\(eval code/g, '(');
var sanitizedLine = line.replace(/^\s+/, '').replace(/\(eval code/g, '(').replace(/^.*?\s+/, '');

// capture and preseve the parenthesized location "(/foo/my bar.js:12:87)" in
// case it has spaces in it, as the string is split on \s+ later on
var location = sanitizedLine.match(/ (\((.+):(\d+):(\d+)\)$)/);
var location = sanitizedLine.match(/ (\(.+\)$)/);

// remove the parenthesized location from the line, if it was matched
sanitizedLine = location ? sanitizedLine.replace(location[0], '') : sanitizedLine;

var tokens = sanitizedLine.split(/\s+/).slice(1);
// if a location was matched, pass it to extractLocation() otherwise pop the last token
var locationParts = this.extractLocation(location ? location[1] : tokens.pop());
var functionName = tokens.join(' ') || undefined;
// if a location was matched, pass it to extractLocation() otherwise pass all sanitizedLine
// because this line doesn't have function name
var locationParts = this.extractLocation(location ? location[1] : sanitizedLine);
var functionName = location && sanitizedLine || undefined;
var fileName = ['eval', '<anonymous>'].indexOf(locationParts[0]) > -1 ? undefined : locationParts[0];

return new StackFrame({
Expand Down
5 changes: 4 additions & 1 deletion spec/error-stack-parser-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,13 @@ describe('ErrorStackParser', function() {

it('should handle spaces in Node.js stacks', function() {
var stackframes = unit.parse(CapturedExceptions.NODE_WITH_SPACES);
expect(stackframes.length).toBe(7);
expect(stackframes.length).toBe(8);
expect(stackframes[0].fileName).toEqual('/var/app/scratch/my project/index.js');
expect(stackframes[0].lineNumber).toBe(2);
expect(stackframes[0].columnNumber).toBe(9);
expect(stackframes[1].fileName).toEqual('/var/app/scratch/my project/index.js');
expect(stackframes[1].lineNumber).toBe(2);
expect(stackframes[1].columnNumber).toBe(9);
});
});
});
3 changes: 2 additions & 1 deletion spec/fixtures/captured-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,8 @@ CapturedExceptions.EDGE_20_NESTED_EVAL = {
CapturedExceptions.NODE_WITH_SPACES = {
name: 'Error',
message: '',
stack: 'Error\n at Object.<anonymous> ' +
stack: 'Error\n at /var/app/scratch/my '+
'project/index.js:2:9\n at Object.<anonymous> ' +
'(/var/app/scratch/my ' +
'project/index.js:2:9)\n at Module._compile ' +
'(internal/modules/cjs/loader.js:774:30)\n at ' +
Expand Down

0 comments on commit 1c9261a

Please sign in to comment.