So I'm pulling remote JSON in to provide version number for an auto update procedure, I am able to pull the file just fine if I am online, I disabled my network adaptor to force no connectivity, and the app crashes to desktop.
Here is my code:
var http = require('http');
var options = {
host: 'xxxx.xxxx.io',
path: '/remote.json'
};
var req = http.get(options, function (res) {
var response = "";
res.on("data", function (data) {
response += data;
var json = response;
console.log("output:\n" + response);
});
res.on("error", function (e) {
response += e;
console.log(e);
});
res.on("uncaughtException", function (err) {
response += err;
console.log(err);
});
res.on("end", function () {
console.log("end:\n" + response);
});
});
I guess in my inexperience I am setting up the error checking incorrectly in some way? Could any of you guys provide the answer so the app doesn't crash on startup?
I found the answer to this.
I needed to declare the on.error outside the http.get
function.
var http = require('http');
var options = {
host: 'xxx.xxxxx.io',
path: '/remote.json',
method: 'get'
};
var res = http.get(options, function (res) {
var response = "";
res.on("data", function (data) {
response += data;
});
res.on("end", function () {
console.log("output:\n" + response);
var json = JSON.parse(response);
console.log(json.type);
});
})
.setTimeout(3000, function() {
console.log("ERROR: The connection timed out.");
res.abort();
})
.on('error', (e) => {
if (e.code === "ENOENT") {
console.log("ERROR: There is no Internet connection.");
}
});