我正在尝试习惯使用Promises。
但是,我很难在子函数中输入错误以冒泡到主函数。
我将Senecajs用于我的所有模块,如下所示:
// resend-invite.js
module.exports = function(options) {
const Promise = require('bluebird'),
seneca = this,
act = Promise.promisify(seneca.act, {context: seneca});
this.add({role:'project-actions', cmd: 'resend-invite'}, function( msg, done ) {
return findProject()
.then(function(project) {
if ( !project.userHasAdminAccess( msg.user_id ) ) {
throw Error( 'User does not have admin access.' );
}
// makes it here...
return act( { role: "user-repository", cmd: "get-referral-user", user: { email: msg.email } } );
})
.then(function(referral_user) {
// doesn't make it here :(
var referral_name = referral_user.first_name + " " + referral_user.last_name;
return anotherFunction();
})
.then(function(mail_info) {
done(null, mail_info);
})
.catch(function(err) {
// doesn't make it here :(
done(err);
})
});
}
就像这样:
// get-referral-user.js
module.exports = function(options) {
const Promise = require('bluebird'),
seneca = this,
User = require('app/models/user');
this.add({role:'user-repository', cmd: 'get-referral-user'}, function( msg, done ) {
return User.findOne(msg.user).exec()
.then(function(user) {
var referral_user_id;
if (user == null) {
throw Error("User wasn't found.");
}
referral_user_id = user.referral_user_id;
return User.findById(referral_user_id).exec()
})
.then(function(user) {
done(null, user.toObject());
})
.catch(function(err) {
// gets to this err handler
done(err);
})
});
}
但是,当get-referral-user.js出现错误时,它会被get-referral-user.js .catch块捕获,但是thr resend-invite.js catch块没有捕获到错误
如何获取resend-invite.js代码来检测子功能(act({role:“user-repository”,cmd:“get-referral-user”,user:{email:msg。电子邮件}})在这种情况下)有一个错误,需要处理。
以下是我的控制台中可能有用的错误部分:
Seneca Fatal Error
Message: seneca: Action cmd:get-referral-user,role:user-repository failed: User wasn't found..
...
ALL ERRORS FATAL: action called with argument fatal$:true (probably a plugin init error, or using a plugin seneca instance)
非常感谢任何帮助。谢谢。