-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.js
66 lines (61 loc) · 2.21 KB
/
errors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Map from hash-based code fragment tracker to the latest offset for that
// code fragment.
let offsets = {};
// Receive the latest offsets for each known code fragment. Overwrites old
// offset values for existing code fragments and adds offsets for any new
// code fragments.
const updateOffsets = (offsetsMap) => {
offsets = Object.assign(offsets, offsetsMap);
};
// A regular expression that allows us to extract the path, tracker hash, lineNo
// and column from a raw stack line.
const rawStackLineRegex = /\((.+?)\|([a-f,0-9]+):(\d+):(\d+)\)/;
// Takes a raw stack trace, rewrites all stack lines with a hash-based tracker
// to file:line:column with the latest correct source line number, and removes
// stack lines corresponding to the hakk evaluator.
const reformatStack = (stack) => {
const lines = stack.split('\n');
const newLines = [];
let pendingLine;
for (let line of lines) {
if (line.includes('hakk/evaluator.js') && line.includes('<anonymous>')) {
newLines.push(' at hakk repl input');
break;
}
if (line.includes('hakk/evaluator.js') && line.includes('at generator')) {
break;
}
// Collapse the stack lines for wrapper functions.
if (pendingLine !== undefined) {
try {
const [pendingFragment] = pendingLine.match(rawStackLineRegex);
const [lineFragment] = line.match(rawStackLineRegex);
line = line.replace(lineFragment, pendingFragment);
} catch (e) {
// Ignore this error
}
pendingLine = undefined;
}
// We have a stack line for a wrapper function.
if (line.includes('_hakk_')) {
pendingLine = line;
continue;
}
try {
const [fragment, path, hash, lineNo, column] = line.match(rawStackLineRegex);
const trueLineNo = parseInt(lineNo) + offsets[hash] - 1;
const finalForm = `(${path}:${trueLineNo}:${column})`;
newLines.push(line.replaceAll(fragment, finalForm));
} catch (e) {
newLines.push(line);
}
}
return newLines.join('\n');
};
// Enable stack rewriting for all errors.
const setupStackTraces = () => {
Error.prepareStackTrace = (err, callsites) => {
return reformatStack(err.stack);
};
};
module.exports = { setupStackTraces, updateOffsets };