Skip to content

Commit 5fd9776

Browse files
chenglouzth
authored andcommitted
Upgrade Test stack trace cleaning function
The stack format change slightly and we were no longer matching on the correct stuff to clean up: "(internal/..." became "(node:internal/..." Took the occasion to swap them out with regex and inline the one-off cleanup logic too (IIRC I kept it separate because not all repos wanted that cleanup. But nobody cares now)
1 parent 81b5d37 commit 5fd9776

File tree

2 files changed

+8
-45
lines changed

2 files changed

+8
-45
lines changed

test/Test.mjs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,6 @@ import * as CodeFrame from "@babel/code-frame";
99

1010
var dirname = (new URL('.', import.meta.url).pathname);
1111

12-
function cleanUpStackTrace(stack) {
13-
var removeInternalLines = function (lines, _i) {
14-
while(true) {
15-
var i = _i;
16-
if (i >= lines.length) {
17-
return lines;
18-
}
19-
if (lines[i].indexOf(" (internal/") >= 0) {
20-
return lines.slice(0, i);
21-
}
22-
_i = i + 1 | 0;
23-
continue ;
24-
};
25-
};
26-
return removeInternalLines(stack.split("\n").slice(2), 0).map(function (line) {
27-
return line.slice(2);
28-
}).join("\n");
29-
}
30-
3112
function print(value) {
3213
var match = typeof value;
3314
if (match === "object" || match === "bigint") {
@@ -62,12 +43,11 @@ function run(loc, left, comparator, right) {
6243
console.log(errorMessage);
6344
var obj = {};
6445
Error.captureStackTrace(obj);
65-
console.log(cleanUpStackTrace(obj.stack));
46+
console.log(obj.stack.replace(/\n /g, "\n ").replace(/^Error\n/, "").replace(/^.+\n/, "").replace(/\n at .+\(node:internal.+\n?/g, ""));
6647
}
6748

6849
export {
6950
dirname ,
70-
cleanUpStackTrace ,
7151
print ,
7252
run ,
7353
}

test/Test.res

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,6 @@ external codeFrameColumns: (string, {..}, {..}) => string = "codeFrameColumns"
1111

1212
let dirname = %raw("new URL('.', import.meta.url).pathname")
1313

14-
let cleanUpStackTrace = stack => {
15-
// Stack format: https://nodejs.org/api/errors.html#errors_error_stack
16-
// Remove the node loader and other lines. No point in showing them
17-
let rec removeInternalLines = (lines, i) => {
18-
if i >= Array.length(lines) {
19-
lines
20-
} else if Array.getUnsafe(lines, i)->String.indexOf(" (internal/") >= 0 {
21-
lines->Array.slice(~start=0, ~end=i)
22-
} else {
23-
removeInternalLines(lines, i + 1)
24-
}
25-
}
26-
27-
stack
28-
->String.split("\n")
29-
// first line is "Error ...". Second line is this frame's stack trace. Ignore the 2
30-
->Array.sliceToEnd(~start=2)
31-
->removeInternalLines(0)
32-
// stack is indented 4 spaces. Remove 2
33-
->Array.map(line => line->String.sliceToEnd(~start=2))
34-
->Array.joinWith("\n")
35-
}
36-
3714
@val @module("util") external inspect: _ => string = "inspect"
3815
let print = value =>
3916
switch Type.typeof(value) {
@@ -64,6 +41,12 @@ ${codeFrame}
6441
// API: https://nodejs.org/api/errors.html#errors_error_capturestacktrace_targetobject_constructoropt
6542
let obj = Object.empty()
6643
captureStackTrace(obj)
67-
Console.log(obj["stack"]->cleanUpStackTrace)
44+
// clean up stack trace! Stack format: https://nodejs.org/api/errors.html#errors_error_stack
45+
obj["stack"]
46+
->String.replaceRegExp(%re("/\n /g"), "\n ") // indent 2 spaces instead of 4, to align with code frame
47+
->String.replaceRegExp(%re("/^Error\n/"), "") // first line is just the word "Error"
48+
->String.replaceRegExp(%re("/^.+\n/"), "") // second line (now first) is this Test module's own stack frame
49+
->String.replaceRegExp(%re("/\n at .+\(node:internal.+\n?/g"), "") // remove internal lines like " at ModuleJob.run (node:internal/modules/esm/module_job:193:25)"
50+
->Console.log
6851
}
6952
}

0 commit comments

Comments
 (0)