如何从另一个Meteor.call中的Meteor.call方法返回错误

时间:2014-09-12 18:25:21

标签: meteor

我的流星代码在某些方面深入探讨了几种Meteor.call方法。如果我在第二层有错误,我想将流星错误扔回客户端,我该怎么办呢?

目前我有这样的事情,但我的输出非常混乱,我不认为我完全理解当我打电话给throw new Meteor.Error(500, e.category_code, e.description);

时发生的事情

在client.js

Meteor.call('firstCall', data, function (error, result) {
  if(result) {
    doSomething();
  }
  else{
    console.log(error);//just shows 500
  }
});

在server.js

var Future = Meteor.npmRequire("fibers/future");

function extractFromPromise(promise) {
    var fut = new Future();
    promise.then(function (result) {
        fut.return(result);
    }, function (error) {
        console.log(error);
        fut.throw(error);
    });
    return fut.wait();
}

firstCall: function (data){
  try{
    Meteor.call('secondCall', data, 'http://testhref.com/test', 'http://testhref2.com/test' function (error, result) {
      return result;
    });
  }
  catch(e){
    throw new Meteor.Error(500, e.category_code, e.description);
  }
}

secondCall: function (data, paymentHref, otherHref){
  try{
    var associate = extractFromPromise(balanced.get(paymentHref).associate_to_customer(otherHref).debit({
                "amount": data.paymentInformation[0].total_amount * 100,
                "appears_on_statement_as": "Trash Mountain"}));
  }
  catch(e){
    Collection.update(data.id, {
        $set: {
            'failed.category_code': e.category_code,
            'failed.description': e.description
        }
    });
    throw new Meteor.Error(500, e.category_code, e.description);
  }
}

2 个答案:

答案 0 :(得分:2)

在您的情况下,当firstCall抛出时,e.category_code中的捕获不会为e.descriptionsecondCall定义任何内容。这是因为在secondCall中您将这两个作为参数传递给Meteor.Error,其作为参数errorreasondetails

  

https://github.com/meteor/meteor/blob/devel/packages/meteor/errors.js

为了传递这些内容,您需要修改firstCall以使用这些属性:

firstCall: function (data){
  try{
    Meteor.call('secondCall', data, 'http://testhref.com/test', 'http://testhref2.com/test');
  }
  catch(e){
    throw new Meteor.Error(500, e.reason, e.details);
  }
}

我甚至不确定你需要将它分成两个模块化调用,因为你可以使用普通的Javascript函数。但我们可以在别处讨论。

答案 1 :(得分:0)

我在这里提几件事:

  1. 异步函数不会抛出异常(除非你使用Meteor._wrapAsync进行同步,我将在后面解释),它们以另一种方式返回错误(作为NodeJS回调式的第一个参数) )。这适用于Meteor.calldoSomeAsyncThing
  2. 我看不到在服务器上使用Meteor.call的好处。 Meteor.call用于从客户端调用服务器方法。在这种情况下,您只需从YourObj.secondCall内部拨打firstCall
  3. 从回调内部返回某些内容(正如您在firstCall内所做的那样)并没有任何效果。您希望异步代码作为同步代码使用,因此我建议使用Meteor._wrapAsync,这是非常好的解释here。 所以,我将实现服务器端有点不同:
  4. firstCall: function (data){
      try{
        return this.secondCall(data);
      }
      catch(e){
        throw new Meteor.Error(500, e.category_code, e.description);
      }
    
    secondCall: function (data){
      try{
        return Meteor._wrapAsync(doSomeAsyncThing)(data);
      }
      catch(e){
        Collection.update(data.id, {
            $set: {
                'failed.category_code': e.category_code,
                'failed.description': e.description
            }
        });
        throw new Meteor.Error(500, e.category_code, e.description);
      }
    

    希望这有帮助!