Skip to content

Commit b8e445d

Browse files
committed
Human readable error messages
1 parent 2d5e561 commit b8e445d

File tree

6 files changed

+78
-15
lines changed

6 files changed

+78
-15
lines changed

package-lock.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "multithreading",
3-
"version": "0.1.15",
4-
"description": "⚡ Multithreading functions in JavaScript, designed to be as simple and fast as possible.",
3+
"version": "0.1.16",
4+
"description": "⚡ Multithreading functions in JavaScript to speedup heavy workloads, designed to feel like writing vanilla functions.",
55
"author": "Walter van der Giessen <[email protected]>",
66
"homepage": "https://multithreading.io",
77
"license": "MIT",

rollup.config.dev.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default ["cjs"].flatMap((type) => {
1515
replace({
1616
__INLINE_WORKER__: fs
1717
.readFileSync(`.temp/worker.${type}${version}.js`, "utf8")
18+
.replaceAll("\\", "\\\\")
1819
.replaceAll("`", "\\`")
1920
.replaceAll("$", "\\$"),
2021
}),

rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export default ["esm", "cjs"].flatMap((type) => {
3333
replace({
3434
__INLINE_WORKER__: fs
3535
.readFileSync(`.temp/worker.${type}${version}.js`, "utf8")
36+
.replaceAll("\\", "\\\\")
3637
.replaceAll("`", "\\`")
3738
.replaceAll("$", "\\$"),
3839
}),

src/lib/getErrorPreview.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const colorRed = "\x1b[31m";
2+
const colorReset = "\x1b[39m";
3+
4+
export function getErrorPreview(error: Error) {
5+
const [message, ...serializedStackFrames] = error.stack!.split("\n");
6+
7+
const stackFrame = decodeURIComponent(serializedStackFrames[0]);
8+
9+
const [functionPart, ...otherParts] = stackFrame.split(
10+
" (data:text/javascript;charset=utf-8,"
11+
);
12+
13+
const other = otherParts.join();
14+
const codeLines = other.split(/\r?\n/);
15+
const lastLine = codeLines.pop()!;
16+
17+
const [lineNumber, columnNumber] = lastLine
18+
.slice(0, -1)
19+
.split(":")
20+
.slice(-2)
21+
.map((n) => parseInt(n));
22+
23+
const amountOfPreviousLines = Math.min(3, lineNumber - 1);
24+
const amountOfNextLines = 2;
25+
26+
const previewLines = codeLines.slice(
27+
lineNumber - (amountOfPreviousLines + 1),
28+
lineNumber + amountOfNextLines
29+
);
30+
31+
const previousLineLength =
32+
codeLines[lineNumber - 1].trimEnd().length - columnNumber;
33+
34+
previewLines.splice(
35+
amountOfPreviousLines + 1,
36+
0,
37+
colorRed +
38+
" ".repeat(columnNumber - 1) +
39+
"^".repeat(previousLineLength) +
40+
" " +
41+
message +
42+
colorReset
43+
);
44+
45+
const preview =
46+
`${error.name} ${functionPart.trim()}:\n\n` + previewLines.join("\n");
47+
48+
return preview;
49+
}

src/lib/worker.worker.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ThreadEvent,
1212
} from "./types";
1313
import { replaceContents } from "./replaceContents.ts";
14+
import { getErrorPreview } from "./getErrorPreview.ts";
1415

1516
declare var pid: number;
1617

@@ -35,12 +36,17 @@ globalThis.onmessage = async (e: MessageEvent<MainEvent>) => {
3536
}
3637
};
3738

38-
const cyanStart = "\x1b[36m";
39-
const cyanEnd = "\x1b[39m";
39+
const colorCyan = "\x1b[36m";
40+
const colorRed = "\x1b[31m";
41+
const colorReset = "\x1b[39m";
4042

4143
const originalLog = console.log;
4244
console.log = (...args) => {
43-
originalLog(`${cyanStart}[Thread_${pid}]${cyanEnd}`, ...args);
45+
originalLog(`${colorCyan}[Thread_${pid}]${colorReset}`, ...args);
46+
};
47+
const originalError = console.error;
48+
console.error = (...args) => {
49+
originalError(`${colorRed}[Thread_${pid}]${colorReset}`, ...args);
4450
};
4551

4652
const $claim = async function $claim(value: Object) {
@@ -125,11 +131,17 @@ namespace Thread {
125131
): Promise<void> {
126132
const gen = globalThis[GLOBAL_FUNCTION_NAME](...data[$.Args]);
127133

128-
hasYield && gen.next();
129-
const returnValue = await gen.next({
130-
$claim,
131-
$unclaim,
132-
});
134+
let returnValue = { value: undefined };
135+
136+
try {
137+
hasYield && gen.next();
138+
returnValue = await gen.next({
139+
$claim,
140+
$unclaim,
141+
});
142+
} catch (error) {
143+
console.error(getErrorPreview(error));
144+
}
133145

134146
globalThis.postMessage({
135147
[$.EventType]: $.Return,

0 commit comments

Comments
 (0)