Skip to content

Commit 98f5b17

Browse files
BridgeARrvagg
authored andcommitted
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 Backport-PR-URL: #19191 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 9dc1f50 commit 98f5b17

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

lib/internal/errors.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -204,33 +204,33 @@ function exceptionWithHostPort(err, syscall, address, port, additional) {
204204
}
205205

206206
/**
207-
* @param {number|string} err - A libuv error number or a c-ares error code
207+
* @param {number|string} code - A libuv error number or a c-ares error code
208208
* @param {string} syscall
209209
* @param {string} [hostname]
210210
* @returns {Error}
211211
*/
212-
function dnsException(err, syscall, hostname) {
213-
const ex = new Error();
212+
function dnsException(code, syscall, hostname) {
213+
let message;
214214
// FIXME(bnoordhuis) Remove this backwards compatibility nonsense and pass
215215
// the true error to the user. ENOTFOUND is not even a proper POSIX error!
216-
if (err === UV_EAI_MEMORY ||
217-
err === UV_EAI_NODATA ||
218-
err === UV_EAI_NONAME) {
219-
err = 'ENOTFOUND'; // Fabricated error name.
216+
if (code === UV_EAI_MEMORY ||
217+
code === UV_EAI_NODATA ||
218+
code === UV_EAI_NONAME) {
219+
code = 'ENOTFOUND'; // Fabricated error name.
220220
}
221-
if (typeof err === 'string') { // c-ares error code.
222-
const errHost = hostname ? ` ${hostname}` : '';
223-
ex.message = `${syscall} ${err}${errHost}`;
224-
// TODO(joyeecheung): errno is supposed to be a number, like in uvException
225-
ex.code = ex.errno = err;
226-
ex.syscall = syscall;
221+
if (typeof code === 'string') { // c-ares error code.
222+
message = `${syscall} ${code}${hostname ? ` ${hostname}` : ''}`;
227223
} else { // libuv error number
228-
const code = lazyInternalUtil().getSystemErrorName(err);
229-
ex.message = `${syscall} ${code}`;
230-
// TODO(joyeecheung): errno is supposed to be err, like in uvException
231-
ex.code = ex.errno = code;
232-
ex.syscall = syscall;
224+
code = lazyInternalUtil().getSystemErrorName(code);
225+
message = `${syscall} ${code}`;
233226
}
227+
// eslint-disable-next-line no-restricted-syntax
228+
const ex = new Error(message);
229+
// TODO(joyeecheung): errno is supposed to be a number / err, like in
230+
// uvException.
231+
ex.errno = code;
232+
ex.code = code;
233+
ex.syscall = syscall;
234234
if (hostname) {
235235
ex.hostname = hostname;
236236
}

test/common/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,9 @@ exports.expectsError = function expectsError(fn, settings, exact) {
692692
}
693693
function innerFn(error) {
694694
assert.strictEqual(error.code, settings.code);
695+
const descriptor = Object.getOwnPropertyDescriptor(error, 'message');
696+
assert.strictEqual(descriptor.enumerable,
697+
false, 'The error message should be non-enumerable');
695698
if ('type' in settings) {
696699
const type = settings.type;
697700
if (type !== Error && !Error.isPrototypeOf(type)) {

test/parallel/test-dns-lookup.js

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ assert.doesNotThrow(() => {
8181
assert(error);
8282
assert.strictEqual(tickValue, 1);
8383
assert.strictEqual(error.code, 'ENOENT');
84+
const descriptor = Object.getOwnPropertyDescriptor(error, 'message');
85+
assert.strictEqual(descriptor.enumerable,
86+
false, 'The error message should be non-enumerable');
8487
}));
8588

8689
// Make sure that the error callback is called

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

+3
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)