Skip to content

Commit

Permalink
fix(resolve): fix passing error to worker
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed May 27, 2017
1 parent 4e93a27 commit 6561f57
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/WorkerPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,15 @@ class PoolWorker {
this.writeJson({
type: 'result',
id: questionId,
error,
error: error ? {
message: error.message,
details: error.details,
missing: error.missing,
} : null,
result,
});
});
finalCallback();
break;
}
case 'emitWarning': {
Expand Down
16 changes: 14 additions & 2 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ function toErrorObj(err) {
};
}

function toNativeError(obj) {
if (!obj) return null;
const err = new Error(obj.message);
err.details = obj.details;
err.missing = obj.missing;
return err;
}

function writeJson(data) {
const lengthBuffer = new Buffer(4);
const messageBuffer = new Buffer(JSON.stringify(data), 'utf-8');
Expand All @@ -43,14 +51,14 @@ const queue = asyncQueue(({ id, data }, taskCallback) => {
context: {
version: 2,
resolve: (context, request, callback) => {
callbackMap[nextQuestionId] = callback;
writeJson({
type: 'resolve',
id,
questionId: nextQuestionId,
context,
request,
});
callbackMap[nextQuestionId] = callback;
nextQuestionId += 1;
},
emitWarning: (warning) => {
Expand Down Expand Up @@ -148,8 +156,12 @@ function onMessage(message) {
const { error, result } = message;
const callback = callbackMap[id];
if (callback) {
callback(error, result);
const nativeError = toNativeError(error);
callback(nativeError, result);
} else {
console.error(`Worker got unexpected result id ${id}`);
}
delete callbackMap[id];
break;
}
default: {
Expand Down

0 comments on commit 6561f57

Please sign in to comment.