Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/logs/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export class AutoCollectExceptions {
// regex to match stack frames from ie/chrome/ff
// methodName=$2, fileName=$4, lineNo=$5, column=$6
const stackFramesRegex = /^(\s+at)?(.*?)(\@|\s\(|\s)([^\(\n]+):(\d+):(\d+)(\)?)$/;
// regex to match native/built-in frames with no file location, e.g. " at Array.forEach ()" or " at Array.forEach (<anonymous>)"
const nativeFrameRegex = /^\s+at\s+(.+?)\s+\((<anonymous>|native)?\)$/;

export class _StackFrame {
public sizeInBytes = 0;
Expand All @@ -111,6 +113,13 @@ export class _StackFrame {
this.method = Util.getInstance().trim(matches[2]) || this.method;
this.fileName = Util.getInstance().trim(matches[4]) || "<no_filename>";
this.line = parseInt(matches[5]) || 0;
} else {
const nativeMatches = frame.match(nativeFrameRegex);
if (nativeMatches) {
this.method = Util.getInstance().trim(nativeMatches[1]) || this.method;
this.fileName = "<no_filename>";
this.line = 0;
}
}

this.sizeInBytes += this.method.length;
Expand All @@ -134,7 +143,7 @@ export function parseStack(stack: any): _StackFrame[] {
let totalSizeInBytes = 0;
for (let i = 0; i <= frames.length; i++) {
const frame = frames[i];
if (stackFramesRegex.test(frame)) {
if (stackFramesRegex.test(frame) || nativeFrameRegex.test(frame)) {
const parsedFrame = new _StackFrame(frames[i], level++);
totalSizeInBytes += parsedFrame.sizeInBytes;
parsedStack.push(parsedFrame);
Expand Down
48 changes: 47 additions & 1 deletion test/unitTests/logs/exceptions.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import assert from "assert";
import sinon from "sinon";

import { AutoCollectExceptions } from "../../../src/logs/exceptions";
import { AutoCollectExceptions, _StackFrame, parseStack } from "../../../src/logs/exceptions";

describe("AutoCollection/Exceptions", () => {
let sandbox: sinon.SinonSandbox;
Expand Down Expand Up @@ -41,3 +41,49 @@ describe("AutoCollection/Exceptions", () => {
assert.equal(processRemoveListenerSpy.getCall(1).args[0], "unhandledRejection");
});
});

describe("StackFrame Parsing", () => {
it("should parse standard stack frames", () => {
const frame = new _StackFrame(" at Object.<anonymous> (/path/to/file.js:10:5)", 0);
assert.equal(frame.method, "Object.<anonymous>");
assert.equal(frame.fileName, "/path/to/file.js");
assert.equal(frame.line, 10);
});

it("should parse native frames with empty parens (issue #698)", () => {
const frame = new _StackFrame(" at Array.forEach ()", 0);
assert.equal(frame.method, "Array.forEach");
assert.equal(frame.fileName, "<no_filename>");
assert.equal(frame.line, 0);
});

it("should parse native frames with <anonymous>", () => {
const frame = new _StackFrame(" at Array.forEach (<anonymous>)", 0);
assert.equal(frame.method, "Array.forEach");
assert.equal(frame.fileName, "<no_filename>");
assert.equal(frame.line, 0);
});

it("should parse native frames with native keyword", () => {
const frame = new _StackFrame(" at Array.forEach (native)", 0);
assert.equal(frame.method, "Array.forEach");
assert.equal(frame.fileName, "<no_filename>");
assert.equal(frame.line, 0);
});

it("parseStack should include native frames in parsed output", () => {
const stack = [
"Error: test",
" at Object.<anonymous> (/path/to/file.js:10:5)",
" at Array.forEach ()",
" at Module._compile (internal/modules/cjs/loader.js:778:30)",
].join("\n");

const parsed = parseStack(stack);
assert.equal(parsed.length, 3);
assert.equal(parsed[0].method, "Object.<anonymous>");
assert.equal(parsed[1].method, "Array.forEach");
assert.equal(parsed[1].fileName, "<no_filename>");
assert.equal(parsed[2].method, "Module._compile");
});
});
Loading