Skip to content

Commit 6561f57

Browse files
committed
fix(resolve): fix passing error to worker
1 parent 4e93a27 commit 6561f57

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/WorkerPool.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,15 @@ class PoolWorker {
134134
this.writeJson({
135135
type: 'result',
136136
id: questionId,
137-
error,
137+
error: error ? {
138+
message: error.message,
139+
details: error.details,
140+
missing: error.missing,
141+
} : null,
138142
result,
139143
});
140144
});
145+
finalCallback();
141146
break;
142147
}
143148
case 'emitWarning': {

src/worker.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ function toErrorObj(err) {
2626
};
2727
}
2828

29+
function toNativeError(obj) {
30+
if (!obj) return null;
31+
const err = new Error(obj.message);
32+
err.details = obj.details;
33+
err.missing = obj.missing;
34+
return err;
35+
}
36+
2937
function writeJson(data) {
3038
const lengthBuffer = new Buffer(4);
3139
const messageBuffer = new Buffer(JSON.stringify(data), 'utf-8');
@@ -43,14 +51,14 @@ const queue = asyncQueue(({ id, data }, taskCallback) => {
4351
context: {
4452
version: 2,
4553
resolve: (context, request, callback) => {
54+
callbackMap[nextQuestionId] = callback;
4655
writeJson({
4756
type: 'resolve',
4857
id,
4958
questionId: nextQuestionId,
5059
context,
5160
request,
5261
});
53-
callbackMap[nextQuestionId] = callback;
5462
nextQuestionId += 1;
5563
},
5664
emitWarning: (warning) => {
@@ -148,8 +156,12 @@ function onMessage(message) {
148156
const { error, result } = message;
149157
const callback = callbackMap[id];
150158
if (callback) {
151-
callback(error, result);
159+
const nativeError = toNativeError(error);
160+
callback(nativeError, result);
161+
} else {
162+
console.error(`Worker got unexpected result id ${id}`);
152163
}
164+
delete callbackMap[id];
153165
break;
154166
}
155167
default: {

0 commit comments

Comments
 (0)