Rohit
Rohit

Reputation: 428

Error: Unable to reach host: "api.twilio.com"

I'm using node-twilio and I keep getting a "Error: Unable to reach host: "api.twilio.com" for every request. We've checked the packets via mtr and they are reaching api.twilio.com. Running on debian on GCE.

Upvotes: 2

Views: 3212

Answers (2)

Gabriel Arghire
Gabriel Arghire

Reputation: 2360

It may be because of your internet connection.

After couple of minutes, if you have internet, try again and it should work.

Upvotes: 1

Rohit
Rohit

Reputation: 428

After days of digging around, found out that the node-twilio module shows many errors incorrectly as:

"Error: Unable to reach host: "api.twilio.com".

The following lines:

var error = null;
if (err || (response && (response.statusCode < 200 || response.statusCode > 206))) {
    error = {};
    // response is null if server is unreachable
    if (response) {
        error.status = response.statusCode;
        error.message = data ? data.message : 'Unable to complete HTTP request';
        error.code = data && data.code;
        error.moreInfo = data && data.more_info;
    } else {
        error.status = err.code;
        error.message = 'Unable to reach host: "'+client.host+'"';
    }
}

This happens because you have a self signed certificate in your chain and the underlying module twilio depends on is request, which is throwing the following error: Error: SELF_SIGNED_CERT_IN_CHAIN but this is not the error being thrown by node-twilio (bad error propagation on their part)

There are 2 fixes:

1.Tell nodejs to ignore self signed certificates in chain by setting:

export NODE_TLS_REJECT_UNAUTHORIZED=0

  1. Find the self signed certificate and remove it from the chain. Here is an example using openssl: https://serverfault.com/questions/590870/how-to-view-all-ssl-certificates-in-a-bundle

References:

https://github.com/request/request

https://github.com/twilio/twilio-node/blob/45858420688854494c2ed476a1997773c33a32a0/lib/Client.js

Ignore invalid self-signed ssl certificate in node.js with https.request?

Upvotes: 5

Related Questions