在Meteor中使用期货时如何访问错误内容?

时间:2014-06-16 14:54:38

标签: asynchronous meteor

我正在使用期货对均衡支付进行异步调用。我想捕获错误并将其反馈给用户,以便我可以告诉他们付款失败的原因。

以下是我在服务器控制台上获得的内容。如何从此错误中解析错误[0] .status或错误[0] .category_code?我试过简单地console.log(error[0].status);,但这没有做任何事。

I20140616-14:38:59.169(0)?     "errors": [
I20140616-14:38:59.169(0)?         {
I20140616-14:38:59.170(0)?             "status": "Conflict",
I20140616-14:38:59.170(0)?             "category_code": "card-not-validated",
I20140616-14:38:59.170(0)?             "additional": null,
I20140616-14:38:59.170(0)?             "status_code": 409,
I20140616-14:38:59.171(0)?             "category_type": "logical",
I20140616-14:38:59.171(0)?             "extras": {},
I20140616-14:38:59.171(0)?             "request_id": "OHMf39d5030f56311e39cde02a
1fe53e539",
I20140616-14:38:59.171(0)?             "description": "Card cannot be validated.
 Your request id is OHMf39d5030f56311e39cde02a1fe53e539."
I20140616-14:38:59.172(0)?         }
I20140616-14:38:59.172(0)?     ]

以下是我正在使用的未来功能。

var Future = Npm.require("fibers/future");

  function extractFromPromise(promise) {
    var fut = new Future();
    promise.then(function (result) {
      fut["return"](result);
    }, function (error) {      
      fut["throw"](error);
      fut.return(error.message);
    });
    return fut.wait();
  }

我使用类似的东西从我的代码中调用此函数。

var customerData =  extractFromPromise(balanced.marketplace.customers.create({
        'name': customerInfo.fname + " " + customerInfo.lname,
        "address": {
          "city": customerInfo.city,
          "state": customerInfo.region,
          "line1": customerInfo.address_line1,
          "line2": customerInfo.address_line2,
          "postal_code": customerInfo.postal_code,
        },
        'email': customerInfo.email_address, 
        'phone': customerInfo.phone_number
        }));

1 个答案:

答案 0 :(得分:0)

我得到了一些帮助并得到了答案。希望这有助于其他人。这是我最初编写的代码的重写。

未来的代码都返回并抛出错误,所以删除后,代码也被清理了一点。

var Future = Npm.require("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();
  }

然后整个事情被包裹在试试中。我控制台注销消息的不同部分。原来,JSON是字符串化的,所以必须首先解析,然后我可以像普通的JSON数据一样访问错误。而且我刚刚了解到,除非您首先使用Meteor.error语法,否则任何错误都不会返回给客户端。

var customerData;

      try {
        customerData =  extractFromPromise(balanced.marketplace.customers.create({
        'name': customerInfo.fname + " " + customerInfo.lname,
        "address": {
          "city": customerInfo.city,
          "state": customerInfo.region,
          "line1": customerInfo.address_line1,
          "line2": customerInfo.address_line2,
          "postal_code": customerInfo.postal_code,
        },
        'email': customerInfo.email_address, 
        'phone': customerInfo.phone_number
        }));
    } catch (e) {
      console.log(JSON.parse(e.message).errors[0].extras);  
      console.log(JSON.parse(e.message).errors[0].category_code);            
      var error = JSON.parse(e.message).errors[0]; 
      throw new Meteor.Error(error.category_code, error.status_code, error.description, error.extras);
    }