Skip to content

Commit

Permalink
fix: correctly handle errors when executing task
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Nov 1, 2023
1 parent 7faffd2 commit 95b9355
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/taskSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions test/task.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit 95b9355

Please sign in to comment.