I'm quite confused on how I should manage error validations from waterline, I need some clarification on good practices. Usually I have chains of promises like this:
sails.models.user.findOne(...)
.then(function(){
//...
//...
return sails.models.user.update(...);
})
.then(....)
.then(....)
.catch(function(err){
})
one problem that arises is when waterline returns a validation error. In this case, I usually need to know when the problem is generated by a wrong input by the client, or a bug in the code.
What I eventually do is wrap the waterline promise in a promise which handle the validation error properly. So the final code would be:
...
.then(function(){
//...
//...
return new Promise(function(resolve,reject){
sails.models.user.update(...)
.then(resolve)
.catch(function(err){
//the error is a bug, return the error object inside the waterline WLError
reject(err._e);
//the error is caused by wrong input, return the waterline WLError
reject(err);
})
})
})
.then(function(){
//second example: we are sure that a validation error can't be caused by a wrong input
return wrapPromise(sails.models.user.find());
})
.then(....)
.catch(function(err){
//WLError ---> res.send(400);
//Error object --> res.send(500);
})
function wrapPromise(action){
//return an error object on validation error
return new Promise(function(resolve,reject){
action
.then(resolve)
.catch(function(err){
reject(err._e || err);
})
})
}
am I doing things correctly? are there better methods to handle errors properly? thanks