当 Meteor.bindEnvironment 中的服务器出错时无法在客户端中捕获 Meteor.Error
cannot catch Meteor.Error in client when error in server inside Meteor.bindEnvironment
当在服务器代码中 Meteor.bindEnvironment
内部发生错误时,我无法在客户端的 Meteor.call
错误回调中获取错误。下面是要复制的示例代码
在服务器中
Meteor.methods({
customMethod: function(arg1, arg2){
Stripe.customers.create({
email: "email@here.com,
description: "blah blah",
source: token,
metadata: {
planId: planId,
quantity: n
},
plan: planId,
quantity: n
}, Meteor.bindEnvironment(function (err, customer) {
if(err){
console.log("error", err);
// TODO cannot catch this error on the client
throw new Meteor.Error(err.rawType, err.message)
}
}))
}
})
在客户端里面一个Meteor事件,
Meteor.call('customMethod', arg1, arg2, function (err, resp) {
if(err){
Session.set('some-error', err)
}
if(resp){
// TODO cannot catch errors throwing from the server
// when inside Meteor.bindEnvironment
Session.set('some-success', true)
}
});
从未设置会话变量。任何帮助都会很棒。谢谢!
Meteor.bindEnvironment
的第二个参数是一个错误处理程序,只要在您作为第一个参数提供的回调中抛出异常,它就会被调用。所以你可以做这样的事情来将错误传递回客户端:
Meteor.bindEnvironment(function (err, customer) {
if (err) throw err
...
}, function (err) {
if (err) throw new Meteor.Error(err.message)
})
更新
抱歉,有点仓促。问题是您的错误(以及可能的结果)来自异步回调,因此您的方法函数将完成执行,并隐式 returned undefined
(它作为null
) 到回调执行任何操作时。
过去,您会使用 a future 解决此问题,但现在我们有更好的承诺:
Meteor.methods({
customMethod (arg1, arg2) {
return new Promise((resolve, reject) => {
Stripe.customers.create({
email: "email@here.com,
...
}, Meteor.bindEnvironment(function (err, customer) {
if(err){
reject(err)
}
resolve(customer)
})).catch(e => { throw new Meteor.Error(e) })
}
})
Meteor 方法足够聪明,可以等待 promise 解决或拒绝,并通过 DDP return 结果(或错误)。您仍然需要捕获错误并正式抛出它,但是您的方法调用将等待您这样做。
当在服务器代码中 Meteor.bindEnvironment
内部发生错误时,我无法在客户端的 Meteor.call
错误回调中获取错误。下面是要复制的示例代码
在服务器中
Meteor.methods({
customMethod: function(arg1, arg2){
Stripe.customers.create({
email: "email@here.com,
description: "blah blah",
source: token,
metadata: {
planId: planId,
quantity: n
},
plan: planId,
quantity: n
}, Meteor.bindEnvironment(function (err, customer) {
if(err){
console.log("error", err);
// TODO cannot catch this error on the client
throw new Meteor.Error(err.rawType, err.message)
}
}))
}
})
在客户端里面一个Meteor事件,
Meteor.call('customMethod', arg1, arg2, function (err, resp) {
if(err){
Session.set('some-error', err)
}
if(resp){
// TODO cannot catch errors throwing from the server
// when inside Meteor.bindEnvironment
Session.set('some-success', true)
}
});
从未设置会话变量。任何帮助都会很棒。谢谢!
Meteor.bindEnvironment
的第二个参数是一个错误处理程序,只要在您作为第一个参数提供的回调中抛出异常,它就会被调用。所以你可以做这样的事情来将错误传递回客户端:
Meteor.bindEnvironment(function (err, customer) {
if (err) throw err
...
}, function (err) {
if (err) throw new Meteor.Error(err.message)
})
更新
抱歉,有点仓促。问题是您的错误(以及可能的结果)来自异步回调,因此您的方法函数将完成执行,并隐式 returned undefined
(它作为null
) 到回调执行任何操作时。
过去,您会使用 a future 解决此问题,但现在我们有更好的承诺:
Meteor.methods({
customMethod (arg1, arg2) {
return new Promise((resolve, reject) => {
Stripe.customers.create({
email: "email@here.com,
...
}, Meteor.bindEnvironment(function (err, customer) {
if(err){
reject(err)
}
resolve(customer)
})).catch(e => { throw new Meteor.Error(e) })
}
})
Meteor 方法足够聪明,可以等待 promise 解决或拒绝,并通过 DDP return 结果(或错误)。您仍然需要捕获错误并正式抛出它,但是您的方法调用将等待您这样做。