diff --git a/src/taskSchema.js b/src/taskSchema.js index d71e5b2..04f591e 100644 --- a/src/taskSchema.js +++ b/src/taskSchema.js @@ -150,7 +150,7 @@ taskSchema.statics.execute = async function(task) { task.status = 'succeeded'; task.result = result; await task.save(); - } catch (err) { + } catch (error) { task.status = 'failed'; task.error.message = error.message; task.error.stack = error.stack; diff --git a/test/task.test.js b/test/task.test.js index ae187a6..3632705 100644 --- a/test/task.test.js +++ b/test/task.test.js @@ -160,4 +160,26 @@ describe('Task', function() { assert.equal(task.status, 'succeeded'); assert.strictEqual(task.result, 42); }); + + it('catches errors in task', async function() { + let resolve; + let reject; + const p = new Promise((_resolve, _reject) => { + resolve = _resolve; + reject = _reject; + }); + Task.registerHandler('getQuestion', async () => { + await new Promise(resolve => setTimeout(resolve, 0)); + throw new Error('Sample error message'); + }); + + let task = await Task.schedule('getQuestion', time.now().valueOf() + 100000); + + task = await Task.execute(task); + + task = await Task.findById(task._id); + assert.ok(task); + assert.equal(task.status, 'failed'); + assert.equal(task.error.message, 'Sample error message'); + }); }); \ No newline at end of file