I'm working on a simple task manager in SenecaJS, and I'm trying to understand the correct way to throw errors from an action. Say I've got an action which loads a task from a datastore, and the task doesn't exist...
Below is the method from my plugin:
/**
* Retrieves a task based on a task ID.
*
* @param {number} args.taskID The UUID of the task to be retrieved.
* @param {function} done A callback function which is called upon retrieval. The
* first argument is an error response; the second is the task.
* @returns {Task|undefined} A task if one exists; otherwise undefined.
*/
this.get = function(args, reply) {
var task = this.make('task')
task.load$( args.taskID, function(err, task)
if(!task || err) {
reply( {code:404, message:`Task ${args.taskID} not found.`} );
} else {
reply( task );
}
});
};
And this is the unit test:
test('Returns "undefined" for non-existant tasks', (fin) => {
var seneca = testSeneca(fin);
var id = "I-don't-exist";
seneca
.act(
{ domain: 'task', action: 'get', taskID: id },
function(err, result) {
expect(result).not.toBeNull();
expect(result.code).toBe(404);
fin();
}
);
});
The code below works, but as you can see I am really ignoring the err callback and evaluating the result to see if it's an error or not. In the documentation, Seneca uses the following to throw an error:
reply( new Error(404) );
or
throw new Error(404);
But when I do that, it ends the process and the unit test fails. Furthermore, the err callback always seems to be null, even if I reply with two objects.
Is there a more correct way to return errors that utilizes the err callback?