Skip to content

Commit 22da2f7

Browse files
committed
errors: make message non-enumerable
A error message should always be non-enumerable. This makes sure that is true for dns errors as well. It also adds another check in `common.expectsError` to make sure no other regressions are introduced going forward. Fixes #19716 PR-URL: #19719 Fixes: #19716 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 354849e commit 22da2f7

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

lib/internal/errors.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -550,34 +550,33 @@ function exceptionWithHostPort(err, syscall, address, port, additional) {
550550
}
551551

552552
/**
553-
* @param {number|string} err - A libuv error number or a c-ares error code
553+
* @param {number|string} code - A libuv error number or a c-ares error code
554554
* @param {string} syscall
555555
* @param {string} [hostname]
556556
* @returns {Error}
557557
*/
558-
function dnsException(err, syscall, hostname) {
559-
// eslint-disable-next-line no-restricted-syntax
560-
const ex = new Error();
558+
function dnsException(code, syscall, hostname) {
559+
let message;
561560
// FIXME(bnoordhuis) Remove this backwards compatibility nonsense and pass
562561
// the true error to the user. ENOTFOUND is not even a proper POSIX error!
563-
if (err === UV_EAI_MEMORY ||
564-
err === UV_EAI_NODATA ||
565-
err === UV_EAI_NONAME) {
566-
err = 'ENOTFOUND'; // Fabricated error name.
562+
if (code === UV_EAI_MEMORY ||
563+
code === UV_EAI_NODATA ||
564+
code === UV_EAI_NONAME) {
565+
code = 'ENOTFOUND'; // Fabricated error name.
567566
}
568-
if (typeof err === 'string') { // c-ares error code.
569-
const errHost = hostname ? ` ${hostname}` : '';
570-
ex.message = `${syscall} ${err}${errHost}`;
571-
// TODO(joyeecheung): errno is supposed to be a number, like in uvException
572-
ex.code = ex.errno = err;
573-
ex.syscall = syscall;
567+
if (typeof code === 'string') { // c-ares error code.
568+
message = `${syscall} ${code}${hostname ? ` ${hostname}` : ''}`;
574569
} else { // libuv error number
575-
const code = lazyInternalUtil().getSystemErrorName(err);
576-
ex.message = `${syscall} ${code}`;
577-
// TODO(joyeecheung): errno is supposed to be err, like in uvException
578-
ex.code = ex.errno = code;
579-
ex.syscall = syscall;
570+
code = lazyInternalUtil().getSystemErrorName(code);
571+
message = `${syscall} ${code}`;
580572
}
573+
// eslint-disable-next-line no-restricted-syntax
574+
const ex = new Error(message);
575+
// TODO(joyeecheung): errno is supposed to be a number / err, like in
576+
// uvException.
577+
ex.errno = code;
578+
ex.code = code;
579+
ex.syscall = syscall;
581580
if (hostname) {
582581
ex.hostname = hostname;
583582
}

test/common/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,9 @@ exports.expectsError = function expectsError(fn, settings, exact) {
691691
fn = undefined;
692692
}
693693
function innerFn(error) {
694+
const descriptor = Object.getOwnPropertyDescriptor(error, 'message');
695+
assert.strictEqual(descriptor.enumerable,
696+
false, 'The error message should be non-enumerable');
694697
if ('type' in settings) {
695698
const type = settings.type;
696699
if (type !== Error && !Error.isPrototypeOf(type)) {

test/parallel/test-dns-lookup.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ dns.lookup('example.com', common.mustCall((error, result, addressType) => {
9292
assert(error);
9393
assert.strictEqual(tickValue, 1);
9494
assert.strictEqual(error.code, 'ENOENT');
95+
const descriptor = Object.getOwnPropertyDescriptor(error, 'message');
96+
assert.strictEqual(descriptor.enumerable,
97+
false, 'The error message should be non-enumerable');
9598
}));
9699

97100
// Make sure that the error callback is called

test/parallel/test-dns-resolveany-bad-ancount.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ server.bind(0, common.mustCall(() => {
3030
assert.strictEqual(err.code, 'EBADRESP');
3131
assert.strictEqual(err.syscall, 'queryAny');
3232
assert.strictEqual(err.hostname, 'example.org');
33+
const descriptor = Object.getOwnPropertyDescriptor(err, 'message');
34+
assert.strictEqual(descriptor.enumerable,
35+
false, 'The error message should be non-enumerable');
3336
server.close();
3437
}));
3538
}));

0 commit comments

Comments
 (0)