Reputation: 73
Normally, everything works, but when there is no internet connection my app throws an error:
events.js:160
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND
/* my code - putting this to try...catch have no effect: */
var http = require('http');
// (...)
var req = http.request(options, response => {
/* ... */
});
req.write(data);
req.end();
So what can I do when internet connection shuts down and I would like to prevent my app from stopping?
Upvotes: 0
Views: 1465
Reputation: 16276
To prevent your app
from stopping when external server's internet connection is down, you can catch getaddrinfo ENOTFOUND
error and return error message to your end client. Please check this SO on how to catch getaddrinfo ENOTFOUND
error.
In your case, the code would be:
var http = require('http');
// (...)
var req = http.request(options, response => {
/* ... */
});
req.on('error', function (err) {
// Check error type and console.log corresponding message.
});
req.write(data);
req.end();
Upvotes: 1