0

I'm using the node js request module to send emails via sendgrid. I am getting the error ETIMEDOUT. I intend on using node-retry npm module to retry the sending, but how can I detect what the error code is? Does the sendgrid API return the error code somehow? Also when I do detect the error code, is it just a matter of waiting X seconds to send the email again? If so how do I determine what X is?

_makeAPIRequest (httpMethod, url, body) {
    var defer = Q.defer();
    var options = {
        method: httpMethod,
        url: this.SENDGRID_API_URL + url,
        headers: {
            'content-type': 'application/json',
            authorization: 'Bearer ' + SENDGRID_API_KEY
        },
        body: body,
        json: true
    };

    request(options, function (error, response, body) {
        if (error) {
            console.dir(error);
            return defer.reject(error);
        }
        defer.resolve(body);
    });
    return defer.promise;
}
Chris Hansen
  • 7,813
  • 15
  • 81
  • 165

1 Answers1

4

ETIMEDOUT is an OS error message. It indicates a failed attempt, at the TCP/IP level, to connect to a remote host, probably the one mentioned in SENDGRID_API_URL.

The default value for that is https://api.sendgrid.com/v3/. For some reason, possibly an outbound firewall or some sort of network configuration trouble, your nodejs program cannot reach that URL, and waits for a response. You should check your value of that URL.

If this is intermittent (doesn't happen all the time) you probably can wait a few seconds and try again.

If it starts happening after you've sent a bunch of emails, you may be hitting a limit at sendgrid. Pace out your sending of emails; try putting a half-second delay between them.

O. Jones
  • 103,626
  • 17
  • 118
  • 172