Skip to content

Commit

Permalink
Improve error stack frame parser for jsshell
Browse files Browse the repository at this point in the history
  • Loading branch information
anba authored and rwaldron committed May 8, 2020
1 parent 5223069 commit fbaa61e
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions lib/agents/jsshell.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const ConsoleAgent = require('../ConsoleAgent');
const errorRe = /^(.*?)?:(\d+):(\d+)? ([\w\d]+): (.+)?$/m;
const customErrorRe = /^(?:uncaught exception: |:)([\w\d]+): (.+)?$/m;

const stackRe = /^([\s\S]*?)\r?\nStack:\r?\n([\s\S]*)$/;
const stackFrameRe = /^(.*?)?@(.*?):(\d+):(\d+)?$/;

class JSShell extends ConsoleAgent {
evalScript(code, options = {}) {
if (options.module && this.args[0] !== '--module') {
Expand All @@ -21,6 +24,28 @@ class JSShell extends ConsoleAgent {
}

parseError(str) {
const stack = [];
const stackMatch = str.match(stackRe);
if (stackMatch) {
str = stackMatch[1];

const stackStr = stackMatch[2];
for (const line of stackStr.split(/\r?\n/g)) {
const match = line.trim().match(stackFrameRe);
if (!match) {
continue;
}

stack.push({
source: match[0],
functionName: match[1],
fileName: match[2],
lineNumber: match[3],
columnNumber: match[4],
});
}
}

const error = {};
let match = str.match(errorRe);

Expand All @@ -34,19 +59,22 @@ class JSShell extends ConsoleAgent {

error.name = match[1];
error.message = match[2];
error.stack = [];
error.stack = stack;
return error;
}

error.name = match[4];
error.message = match[5];

error.stack = [{
source: match[0],
fileName: match[1],
lineNumber: match[2],
columnNumber: match[3]
}];
if (stack.length === 0) {
stack.push({
source: match[0],
fileName: match[1],
lineNumber: match[2],
columnNumber: match[3]
});
}
error.stack = stack;

return error;
}
Expand Down

0 comments on commit fbaa61e

Please sign in to comment.