Reputation: 5644
I have this Meteor code:
Meteor.methods({
'addEmailToList': function(email){
var mailChimpCall = Meteor.wrapAsync(HTTP.call);
try {
var result = mailChimpCall( 'POST', 'https://us10.api.mailchimp.com/3.0/lists/yyy/members/', {
auth: 'xxx',
header: 'content-type: application/json',
data: {"email_address":email, "status":"subscribed"}
});
} catch(error){
console.log(error);
throw new Meteor.Error("Bad result: ", "It was an error when posting");
}
return result
}
});
Which result in this response:
{ [Error: failed [400] {"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Member Exists","status":400,"detail":"[email protected] is already a list member. Use PUT to insert or update list members.","instance":""}] stack: [Getter] }
How can I get the status
(400) from that response to a variable in js?
var errorStatus = SOME CODE HERE
Upvotes: 0
Views: 264
Reputation: 76
There is a bug in Meteor.wrapAsync at the moment, so getting the error message out is a bit of a pain. Better to make a new method that fixes the problem. All credit for this workaround to github/meteor forum user @faceyspacey for this workaround.
var Future = Npm.require( 'fibers/future' );
Meteor.makeAsync = function(fn, context) {
return function (/* arguments */) {
var self = context || this;
var newArgs = _.toArray(arguments);
var callback;
for (var i = newArgs.length - 1; i >= 0; --i) {
var arg = newArgs[i];
var type = typeof arg;
if (type !== "undefined") {
if (type === "function") {
callback = arg;
}
break;
}
}
if(!callback) {
var fut = new Future();
callback = function(error, data) {
fut.return({error: error, data: data});
};
++i;
}
newArgs[i] = Meteor.bindEnvironment(callback);
var result = fn.apply(self, newArgs);
return fut ? fut.wait() : result;
};
};
Place this in a location like server/lib/makeAsync.js
for example, and use it like you would Meteor.wrapAsync, with the caveat that this returns an object with an error and data key, instead of throwing the error. So replace existing try/catch with an if (response.error)
to get the error, and var statusCode = response.data.statusCode;
!
Upvotes: 1
Reputation: 1820
You can get by using jquery or Javascript. At first you have to convert the response to a valid JSON string. then apply one of the following method.
Using Jquery
var jsonString = '{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Member Exists","status":400,"detail":"[email protected] is already a list member. Use PUT to insert or update list members.","instance":""}'
var jsonObject = $.parseJSON(jsonString);
var errorStatus = jsonObject ['status'];
Using javascript
var jsonString = '{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Member Exists","status":400,"detail":"[email protected] is already a list member. Use PUT to insert or update list members.","instance":""}'
var jsonObject = JSON.parse(jsonString);
var errorStatus = jsonObject ['status'];
Upvotes: 0
Reputation: 66480
It's invalid JSON but you can get that number using regular expression:
var response = '{ [Error: failed [400] {"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Member Exists","status":400,"detail":"[email protected] is already a list member. Use PUT to insert or update list members.","instance":""}] stack: [Getter] }';
alert(response.match(/\[([0-9]+)\]/)[1]);
Upvotes: 0
Reputation: 4290
You need to find how to get the appropriate error object from your response, and that object is:
{
"type": "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
"title": "Member Exists",
"status": 400,
"detail": "[email protected] is already a list member. Use PUT to insert or update list members.",
"instance": ""
}
After that you can use the status
property to get the error code.
Alternatively, though it is NOT suggested, you can use Regex to parse that whole string you got and get the number inside [Error: failed [XXXX]
Upvotes: 1