-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathWorkerError.js
More file actions
46 lines (37 loc) · 1.31 KB
/
WorkerError.js
File metadata and controls
46 lines (37 loc) · 1.31 KB
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
// Utility function to safely build a merged stack trace
const buildStackTrace = (err, workerStack = '', workerId = 'unknown') => {
const originStackLines = (err?.stack || '')
.split('\n')
.filter((line) => line.trim().startsWith('at'));
const workerStackLines = (workerStack || '')
.split('\n')
.filter((line) => line.trim().startsWith('at'));
// Compute the difference between the worker's stack and the error's stack
const extraWorkerLines = workerStackLines.slice(
0,
Math.max(0, workerStackLines.length - originStackLines.length)
);
// Build a readable, merged stack trace
const mergedStack = [
`Thread Loader (Worker ${workerId})`,
err?.message || 'Unknown error',
extraWorkerLines.join('\n'),
...originStackLines
]
.filter(Boolean)
.join('\n');
return mergedStack;
};
class WorkerError extends Error {
constructor(err, workerId) {
// Pass a safe message to the base Error class
super(err?.message || 'Unknown worker error');
this.name = err?.name || 'WorkerError';
this.originalError = err;
// Capture the current stack before modifying it
Error.captureStackTrace?.(this, WorkerError);
// Replace the default stack with a combined one
this.stack = buildStackTrace(err, this.stack, workerId);
}
}
export default WorkerError;