From b29c36b80746733994257b7380245102bc3c4cd6 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Sun, 1 Apr 2018 07:54:32 +0200 Subject: [PATCH] errors: make dns errors consistent Right now the hostname could in some cases be missed, depending on the libuv error number. This makes sure there the hostname is always added, if available. PR-URL: https://github.com/nodejs/node/pull/19754 Reviewed-By: Joyee Cheung Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/dns.js | 2 +- lib/internal/errors.js | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/dns.js b/lib/dns.js index cb8b2ca0beb..fdfcec2e7ee 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -229,7 +229,7 @@ function resolver(bindingName) { req.oncomplete = onresolve; req.ttl = !!(options && options.ttl); var err = this._handle[bindingName](req, name); - if (err) throw dnsException(err, bindingName); + if (err) throw dnsException(err, bindingName, name); return req; } Object.defineProperty(query, 'name', { value: bindingName }); diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 3e940c84968..2fe081bda74 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -556,20 +556,20 @@ function exceptionWithHostPort(err, syscall, address, port, additional) { * @returns {Error} */ function dnsException(code, syscall, hostname) { - let message; - // FIXME(bnoordhuis) Remove this backwards compatibility nonsense and pass - // the true error to the user. ENOTFOUND is not even a proper POSIX error! - if (code === UV_EAI_MEMORY || - code === UV_EAI_NODATA || - code === UV_EAI_NONAME) { - code = 'ENOTFOUND'; // Fabricated error name. - } - if (typeof code === 'string') { // c-ares error code. - message = `${syscall} ${code}${hostname ? ` ${hostname}` : ''}`; - } else { // libuv error number - code = lazyInternalUtil().getSystemErrorName(code); - message = `${syscall} ${code}`; + // If `code` is of type number, it is a libuv error number, else it is a + // c-ares error code. + if (typeof code === 'number') { + // FIXME(bnoordhuis) Remove this backwards compatibility nonsense and pass + // the true error to the user. ENOTFOUND is not even a proper POSIX error! + if (code === UV_EAI_MEMORY || + code === UV_EAI_NODATA || + code === UV_EAI_NONAME) { + code = 'ENOTFOUND'; // Fabricated error name. + } else { + code = lazyInternalUtil().getSystemErrorName(code); + } } + const message = `${syscall} ${code}${hostname ? ` ${hostname}` : ''}`; // eslint-disable-next-line no-restricted-syntax const ex = new Error(message); // TODO(joyeecheung): errno is supposed to be a number / err, like in