How to parse HTTP errors?

I don’t understand how to parse HTTP errors. Assuming something like this:

try {
  result = HTTP.call('GET', someUrl, { params: { ... } });
} catch (e) {
  console.log(e);
}

This prints out:

{ [Error: failed [404] [{“errorCode”:“NOT_FOUND”,“message”:“The requested resource does not exist”}]] stack: [Getter] }

Yet if I try to dump the value of e.errorCode, it says it’s undefined. I don’t understand how to parse this error object. Any help would be appreciated!

EJSON.parse

Afraid not. EJSON.parse expects a string as an argument, and e in this case is not a string.

meteor debug
Set breakpoint at console.log line. Inspect the exact structure that the e variable has.

Alternative:
console.log(JSON.stringify(e, null, 4));
Also shows you the same thing.
I suspect that the error has a response or body property or similar and that’s where you’ll find the errorCode, i.e. e.response.errorCode or similar.

Edit: Formatting.

What you need is result.statusCode.

AFAIK on the server side, result.statusCode will not get what you want because Meteor encapsulates all 4xx and 5xx errors into its own error.

Reference: [Solved] How to get http error code?

something like this might work depending on what you need to extract from your HTTP error response object:

var response;
try {
    response = HTTP.get('url', { options });
} catch (e) {
    response = e.response;
    console.log(response);
    // or rethrow this error as a new error
    // throw new Meteor.Error(response.data.code, response.data.message, response.data.description);
}
return response;