How can I handle or catch this code or https module in node.js? I'm trying to catch this, because if someone will just disconnect from the internet, it will end up in this error. I tried many things, but it didn't work. The website's body is just v2.2
Here's the code:
const version = "v2.2";
function checkforupdates() {
try {
https.get("https://tomkoid.tk/phyc/latestversion.txt",(res) => {
let updatebody = "";
res.on("data", (chunk) => {
updatebody += chunk;
});
res.on("end", () => {
if(updatebody.toString()!=version) {
console.log("\033[0;33m[Updates] Update " + updatebody.toString() + " found. Download it on GitHub!\033[0m");
} else {
console.log("\033[0;32m[Updates] No update found.\033[0m");
}
});
});
} catch(err) {
console.log("error")
}
}
and here's the error:
node:events:368
throw er; // Unhandled 'error' event
^
Error: getaddrinfo EAI_AGAIN tomkoid.tk
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:72:26)
Emitted 'error' event on ClientRequest instance at:
at TLSSocket.socketErrorListener (node:_http_client:442:9)
at TLSSocket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -3001,
code: 'EAI_AGAIN',
syscall: 'getaddrinfo',
hostname: 'tomkoid.tk'
}
Node.js v17.3.1
I just "fixed" it by trying axios library, works better.