1

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

phzonta
  • 773
  • 3
  • 6
  • 16
  • Avoid the [`Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572)! Just `throw` inside the `catch` callback instead of calling that `reject()` – Bergi Oct 31 '15 at 11:57
  • @Bergi I think reject(err._e || err) is needed to check if err._e exists. The first reject(err._e) can be replaced with throw err._e, but it should be the same – phzonta Nov 01 '15 at 09:29
  • 1
    No, the `||` might be needed, but the `reject()` call is not. You should just be doing `return ….catch(function(err) { throw err._e || err; });` – Bergi Nov 01 '15 at 10:41
  • You can also visit the Sails.js Gitter page if you're running into errors. I've found some help there, and I'm very new to coding. [Gitter](https://gitter.im/balderdashy/sails) – Joe Hill Nov 03 '15 at 06:21

1 Answers1

0

You can simply add a check in the catch to differentiate between validation and 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
      }
  })
adnan kamili
  • 8,967
  • 7
  • 65
  • 125