From c6c73f6dd2caabd342dff0225c9dc96fb76c8f52 Mon Sep 17 00:00:00 2001 From: Joe Fleming Date: Tue, 28 Feb 2017 15:49:30 -0700 Subject: [PATCH] fix code and tests, use custom errors --- src/worker.js | 14 +++++++++++--- test/src/worker.js | 4 ++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/worker.js b/src/worker.js index c928915..b353ed1 100644 --- a/src/worker.js +++ b/src/worker.js @@ -178,7 +178,7 @@ export default class Worker extends events.EventEmitter { if (isResolved) return; this.debug(`Timeout processing job ${job._id}`); - reject(new WorkerTimeoutError({ + reject(new WorkerTimeoutError(`Worker timed out, timeout = ${job._source.timeout}`, { timeout: job._source.timeout, jobId: job._id, })); @@ -220,16 +220,24 @@ export default class Worker extends events.EventEmitter { }); }, (jobErr) => { if (!jobErr) { - jobErr = new UnspecifiedWorkerError({ + jobErr = new UnspecifiedWorkerError('Unspecified worker error', { jobId: job._id, }); } // job execution failed - if (jobErr.type === 'WorkerTimeoutError') { + if (jobErr.name === 'WorkerTimeoutError') { this.debug(`Timeout on job ${job._id}`); this.emit(constants.EVENT_WORKER_JOB_TIMEOUT, this._formatErrorParams(jobErr, job)); return; + + // append the jobId to the error + } else { + try { + Object.assign(jobErr, { jobId: job._id }); + } catch (e) { + // do nothing if jobId can not be appended + } } this.debug(`Failure occurred on job ${job._id}`, jobErr); diff --git a/test/src/worker.js b/test/src/worker.js index bfb7b02..90a5db8 100644 --- a/test/src/worker.js +++ b/test/src/worker.js @@ -719,7 +719,7 @@ describe('Worker class', function () { expect(err).to.have.property('error'); expect(err).to.have.property('job'); expect(err).to.have.property('worker'); - expect(err.error).to.have.property('type', 'UnspecifiedWorkerError'); + expect(err.error).to.have.property('name', 'UnspecifiedWorkerError'); done(); } catch (e) { done(e); @@ -776,7 +776,7 @@ describe('Worker class', function () { expect(err).to.have.property('error'); expect(err).to.have.property('job'); expect(err).to.have.property('worker'); - expect(err.error).to.have.property('type', 'WorkerTimeoutError'); + expect(err.error).to.have.property('name', 'WorkerTimeoutError'); done(); } catch (e) { done(e);