I am writing a small status query library for Minecraft servers using a Net socket on nodejs.
According to the JSDoc and Node documentation, the error listener (socket.on('error', () => ...)
should catch errors thrown and it does catch other errors such as when the server unexpectedly closes the connection. However I can not seem to catch host resolve errors which I have to.
client.connect(host.port, host.host, () => {
//sending some
});
client.on('data', (chunk) => {
//receiving some chunks
});
client.on('error', (err) => {
client.end(() => reject(err));
});
here client
is a new Net.Socket();
and the function is async returning a promise that either resolves to the final response or rejects errors.
However whenever I try to connect to a non-existent host my whole program crashes with an uncaught exception:
node:internal/process/promises:246
triggerUncaughtException(err, true /* fromPromise */);
^
Error: getaddrinfo ENOTFOUND testhostmustfail12444.com
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'testhostmustfail12444.com'
}
Ideally I want to catch this specific error and be able to return it to the caller function in order to use the information that the host is not resolvable!