Bluebird OperationalError未捕获catch()

时间:2017-03-09 09:56:04

标签: javascript node.js promise bluebird

以下代码使用promises执行用户注册(Node / Express)。我使用Mailgun来验证电子邮件地址,返回一个承诺,并且如果电子邮件地址无效,它将在承诺内部抛出异常。

但是,当电子邮件地址无效且validate_email引发其异常时,catch中的app.post块永远不会捕获异常,并且节点崩溃。我做错了什么?

NodeJS v6.9.2;快递v4.15;蓝鸟v3.4.7。

(ps我正在创建Bluebird OperationalErrors,因为我已经尝试了自定义错误类型来抛出/捕获,并且没有让它工作,我知道我已经克服了它......)

routes.js



const Promise = require('bluebird');       

router.post('/register', function(req, res, next) {
  console.log('registering... ', req.body); 
  var ret = {};
  
  verifyRecaptcha(req.body.recaptcha)
  .then(()=>{
    // exceptions thrown here do NOT get caught in catch()
    return mail.validate_email(req.body.email);
    
  }).then(() => {
    return db.create_customer(req.body);
    
  }).then((customerId) => {
    ret.status = true;
    ret.customerId = customerId;
    res.send(ret);

  }).error((e)=> { 
    // I expected my validate_email exception 
    // to be caught here, but it isn't
    console.error('/register got an error (108)', e);
    res.status(500).send({
      error: 'unable to register'
    });
    
  }).catch(function(e) {
    console.error('/register got an error (114)', e);
    res.status(500).send({
      error: 'unknown internal error'
    });
  });                                                        
});                                                           




mail.js



const Promise = require('bluebird');       

var mailgun_validate = require('mailgun-validate-email')(<pubkey...>);

exports.validate_email = function(email) {
  console.log('validate_email', email);
  return Promise.delay(1500).then(()=> {
    mailgun_validate(email, (err, result)=> {
      console.log('-> cb', err, result);
      if (err) throw err; // this gets thrown...
      else if (result && result.is_valid === true) return true;
      else throw errors.newOperationalError('invalid email address', {email:email});
    });
  });
};
&#13;
&#13;
&#13;

errors.js

&#13;
&#13;
const Promise = require('bluebird');       

exports.newOperationalError = function(message, data) {
  var e = new Promise.OperationalError(message);
  for (i in data) {
    e[i] = data[i];   
  }
  return e;
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您遇到此问题是因为mailgun_validate未返回承诺,从而违反了您的承诺错误处理模式。

您应该promisify以便then.then.then.catch链工作