How to properly throw and handle errors in promises in Sails.js?

余生颓废 提交于 2020-01-14 09:19:09

问题


I'm starting to convert my callback code to promises in Sails.js, but I don't understand how I can raise custom errors and handle them in the promise chain. Sails.js uses Q as its promise library.

User.findOne({email: req.param('professorEmail'), role: 'professor'})
    .then(function (user) {
      if (user) {
        return Course.create({
          user_id: user.id,
          section: req.param('section'),
          session: req.param('session'),
          course_code: req.param('course_code')
        });
      } else {
        // At this point `user` is undefined which means that no professor was found so I want to throw an error.
        // Right now the following statement does throw the error, but it crashes the server.
        throw new Error('That professor does not exist.');
        // I want to be able to handle the error in the .fail() or something similar in the promise chain.
      }
    }).then(function (createSuccess) {
      console.log(createSuccess);
    }).fail(function (err) {
      console.log(err);
    });

Right now the .fail() is never called because the thrown error crashes the server.


回答1:


Waterline's claim complete Q promise object after the first then seems untrue by your test. I've verified it myself as well and found a workaround.

You can do this :

var Q = require('q');
[...]
Q(User.findOne({email: req.param('professorEmail'), role: 'professor'}))
.then(function (user) {
  if (user) {
    return Course.create({
      user_id: user.id,
      section: req.param('section'),
      session: req.param('session'),
      course_code: req.param('course_code')
    });
  } else {
    // At this point `user` is undefined which means that no professor was found so I want to throw an error.
    // Right now the following statement does throw the error, but it crashes the server.
    throw new Error('That professor does not exist.');
    // I want to be able to handle the error in the .fail() or something similar in the promise chain.
  }
}).then(function (createSuccess) {
  console.log(createSuccess);
}).fail(function (err) {
  console.log(err);
});

This will return a true Q promise.




回答2:


Use .catch() instead of .fail().



来源:https://stackoverflow.com/questions/19912280/how-to-properly-throw-and-handle-errors-in-promises-in-sails-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!