I am currently creating a Sails.js application. I use promises in my controllers. I would like to define a set of custom error objects to make error propagation easier. Where do you define custom error objects so that they are accessible between controllers and service files?
Examples:
CustomErrors.js: (Where is the best place to define custom errors?)
function ErrorNotFound(message) {
this.name = 'Error Not Found';
this.message = message || 'Not Found';
this.status = 404;
this.stack = (new Error()).stack;
}
ErrorNotFound.prototype = Object.create(Error.prototype);
ErrorNotFound.prototype.constructor = ErrorNotFound;
Controller1.js:
Model.find({attr1: 123, name: 'Joe Blogs}).then(function(model){
if(!model) throw new ErrorNotFound("The specified model was not found");
else return Model2.find({uuid: req.uuid, colour: model.favourite_colour});
})then(function(model2){
return res.json(model2);
}).catch(function(err){
//if ErrorNotFound is caught, it will return Not Found 404
//otherwise it will return ServerError 500
res.negotiate(err);
})
source
share