Skip to content

Commit 80dda4f

Browse files
sendilkumarnmichael-ciniawsky
authored andcommitted
fix(WorkerPool): trace stacks to avoid duplicated err.messages from workers (#13)
1 parent 7ab2d8c commit 80dda4f

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/WorkerError.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const stack = (err, worker, workerId) => {
2+
const originError = err.stack
3+
.split('\n')
4+
.filter(line => line.trim().startsWith('at'));
5+
6+
const workerError = worker
7+
.split('\n')
8+
.filter(line => line.trim().startsWith('at'));
9+
10+
const diff = workerError.slice(0, workerError.length - originError.length).join('\n');
11+
12+
originError.unshift(diff);
13+
originError.unshift(err.message);
14+
originError.unshift(`Thread Loader (Worker ${workerId})`);
15+
16+
return originError.join('\n');
17+
};
18+
19+
class WorkerError extends Error {
20+
constructor(err, workerId) {
21+
super(err);
22+
this.name = err.name;
23+
this.message = err.message;
24+
25+
Error.captureStackTrace(this, this.constructor);
26+
27+
this.stack = stack(err, this.stack, workerId);
28+
}
29+
}
30+
31+
export default WorkerError;

src/WorkerPool.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import childProcess from 'child_process';
44
import asyncQueue from 'async/queue';
55
import asyncMapSeries from 'async/mapSeries';
66
import readBuffer from './readBuffer';
7+
import WorkerError from './WorkerError';
78

89
const workerPath = require.resolve('./worker');
910

@@ -183,14 +184,7 @@ class PoolWorker {
183184
} else {
184185
obj = arg;
185186
}
186-
const err = new Error(obj.message);
187-
err.message = obj.message;
188-
if (obj.stack) {
189-
err.stack = `${obj.stack}\n\tfrom thread-loader (worker ${this.id})\n${err.stack}`;
190-
}
191-
err.hideStack = obj.hideStack;
192-
err.details = obj.details;
193-
return err;
187+
return new WorkerError(obj, this.id);
194188
}
195189

196190
readBuffer(length, callback) {
@@ -272,4 +266,3 @@ export default class WorkerPool {
272266
}
273267
}
274268
}
275-

0 commit comments

Comments
 (0)