Skip to content

Commit

Permalink
Change error handling logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
garg3133 committed Feb 1, 2024
1 parent 9ba6df4 commit 0bb8272
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions lib/runner/concurrency/worker-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@ class WorkerTask extends EventEmitter {

this.printLog('Running ' + Logger.colors[colorPair[0]][colorPair[1]](` ${this.task_label} `));

let resolvePromise;
let rejectPromise;
const promise = new Promise((resolve, reject) => {
resolvePromise = resolve;
rejectPromise = reject;
});

const {port1, port2} = new MessageChannel();
port2.onmessage = ({data: result}) => {
if (isString(result)) {
Expand All @@ -99,12 +92,9 @@ class WorkerTask extends EventEmitter {
}

const handleError = (err, errType) => {
this.writeToStdOut(Logger.colors.red.bold(`\n${errType}:`));
this.writeToStdOut(Logger.colors.red(err.stack));
console.error(Logger.colors.red.bold(`\nEncountered an ${errType} while running "${this.label}":\n`));

this.emitWorkerOutput(err);

rejectPromise(err);
throw err;
};

switch (result.type) {
Expand All @@ -118,31 +108,31 @@ class WorkerTask extends EventEmitter {
break;

case 'unhandledRejection':
handleError(result.err, 'Unhandled rejection');
handleError(result.err, 'unhandled rejection');
break;

case 'uncaughtException':
handleError(result.err, 'Uncaught exception');
handleError(result.err, 'uncaught exception');
break;
}
};
port2.unref();

setTimeout(() => {
this.piscina.run({argv: this.argv, port1}, {transferList: [port1]})
.catch(err => err)
.then(failures => {
this.emitWorkerOutput(failures);

//throw error to mark exit-code of the process
if (failures) {
return rejectPromise(new Error());
}
resolvePromise();
});
}, this.index * this.startDelay);

return promise;
return new Promise((resolve, reject) => {
setTimeout(() => {
this.piscina.run({argv: this.argv, port1}, {transferList: [port1]})
.catch(err => err)
.then(failures => {
this.emitWorkerOutput(failures);

//throw error to mark exit-code of the process
if (failures) {
return reject(new Error());
}
resolve();
});
}, this.index * this.startDelay);
});
}
}

Expand Down

0 comments on commit 0bb8272

Please sign in to comment.