问题描述:

I am using futures to make an async call to balanced payments. I want to catch errors and feed them back to the user so I can tell them why their payment failed.

Here is what I get on the server console. How can I parse out the errors[0].status, or errors[0].category_code from this error? I've tried to simply console.log(error[0].status);, but this does nothing.

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)? ]

Here is the future function I'm using.

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();

}

I call this function from my code using something like this.

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

}));

网友答案:

I had some help and got the answer. Hope this helps others. Here is the rewrite of the code I initially wrote.

The future code was both returning and throwing the error, so that was removed, the code was cleaned up a bit as well.

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();
  }

And then the whole thing is wrapped in a try catch. I console log out the different parts of the message. Turned out that the JSON was stringified, so that had to be parsed first, then I could access the error like normal JSON data. And I've just learned that no errors will get back to the client unless you use the Meteor.error syntax first.

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);
    }
相关阅读:
Top