Skip to content

Commit

Permalink
chore: highlight exact place of failure in compiled functions
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Nov 16, 2021
1 parent 4e5a985 commit 3fca9a6
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/core/src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,23 @@ export class Utils {
return fn(...args);
} catch (e: any) {
if ([SyntaxError, TypeError, EvalError, ReferenceError].some(t => e instanceof t)) {
const position = e.stack.match(/<anonymous>:(\d+):(\d+)/);
let code = fn.toString();

if (position) {
const lines = code.split('\n').map((line, idx) => {
if (idx === +position[1] - 4) {
return '> ' + line;
}

return ' ' + line;
});
lines.splice(+position[1] - 3, 0, ' '.repeat(+position[2] - 4) + '^');
code = lines.join('\n');
}

// eslint-disable-next-line no-console
console.error(`JIT runtime error: ${e.message}\n\n${fn.toString()}`);
console.error(`JIT runtime error: ${e.message}\n\n${code}`);
}

throw e;
Expand Down

0 comments on commit 3fca9a6

Please sign in to comment.