node.js - sails.js/waterline validation error handlig -
i'm quite confused on how should manage error validations waterline, need clarification on practices. have chains of promises this:
sails.models.user.findone(...) .then(function(){ //... //... return sails.models.user.update(...); }) .then(....) .then(....) .catch(function(err){ })
one problem arises when waterline returns validation error. in case, need know when problem generated wrong input client, or bug in code.
what wrap waterline promise in promise handle validation error properly. final code be:
... .then(function(){ //... //... return new promise(function(resolve,reject){ sails.models.user.update(...) .then(resolve) .catch(function(err){ //the error bug, return error object inside waterline wlerror reject(err._e); //the error caused wrong input, return waterline wlerror reject(err); }) }) }) .then(function(){ //second example: sure validation error can't caused 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 error object on validation error return new promise(function(resolve,reject){ action .then(resolve) .catch(function(err){ reject(err._e || err); }) }) }
am doing things correctly? there better methods handle errors properly? thanks
you can add check in catch differentiate between validation , other errors:
sails.models.user.findone(...) .then(function(){ //... //... return sails.models.user.update(...); }) .then(....) .then(....) .catch(function(err){ if(err.error == "e_validation") { // validation error } })
Comments
Post a Comment