Skip to content

Commit 7498383

Browse files
LiviaMedeirosRafaelGSS
authored andcommitted
lib: make validateInternalField() throw ERR_INVALID_THIS
PR-URL: #58765 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 11811c1 commit 7498383

File tree

5 files changed

+13
-18
lines changed

5 files changed

+13
-18
lines changed

lib/internal/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ E('ERR_INVALID_SYNC_FORK_INPUT',
15281528
'Asynchronous forks do not support ' +
15291529
'Buffer, TypedArray, DataView or string input: %s',
15301530
TypeError);
1531-
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError);
1531+
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s', TypeError, HideStackFramesError);
15321532
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple', TypeError);
15331533
E('ERR_INVALID_TYPESCRIPT_SYNTAX', '%s', SyntaxError);
15341534
E('ERR_INVALID_URI', 'URI malformed', URIError);

lib/internal/validators.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const {
2424
codes: {
2525
ERR_INVALID_ARG_TYPE: { HideStackFramesError: ERR_INVALID_ARG_TYPE },
2626
ERR_INVALID_ARG_VALUE: { HideStackFramesError: ERR_INVALID_ARG_VALUE },
27+
ERR_INVALID_THIS: { HideStackFramesError: ERR_INVALID_THIS },
2728
ERR_OUT_OF_RANGE: { HideStackFramesError: ERR_OUT_OF_RANGE },
2829
ERR_SOCKET_BAD_PORT: { HideStackFramesError: ERR_SOCKET_BAD_PORT },
2930
ERR_UNKNOWN_SIGNAL: { HideStackFramesError: ERR_UNKNOWN_SIGNAL },
@@ -526,9 +527,15 @@ const validateLinkHeaderFormat = hideStackFrames((value, name) => {
526527
}
527528
});
528529

530+
/**
531+
* Validate provided `this` object by checking that it has specific own property
532+
* @param {any} object
533+
* @param {string|symbol} fieldKey
534+
* @param {string} className
535+
*/
529536
const validateInternalField = hideStackFrames((object, fieldKey, className) => {
530537
if (typeof object !== 'object' || object === null || !ObjectPrototypeHasOwnProperty(object, fieldKey)) {
531-
throw new ERR_INVALID_ARG_TYPE('this', className, object);
538+
throw new ERR_INVALID_THIS(className);
532539
}
533540
});
534541

test/parallel/test-vm-module-basic.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ const util = require('util');
8787
for (const value of [null, { __proto__: null }, SourceTextModule.prototype]) {
8888
assert.throws(
8989
() => m[util.inspect.custom].call(value),
90-
{
91-
code: 'ERR_INVALID_ARG_TYPE',
92-
message: /The "this" argument must be an instance of Module/,
93-
},
90+
{ code: 'ERR_INVALID_THIS' },
9491
);
9592
}
9693
}

test/parallel/test-vm-module-errors.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,7 @@ async function checkInvalidOptionForEvaluate() {
215215
['link', 'evaluate'].forEach(async (method) => {
216216
await assert.rejects(async () => {
217217
await Module.prototype[method]();
218-
}, {
219-
code: 'ERR_INVALID_ARG_TYPE',
220-
message: /The "this" argument must be an instance of Module/
221-
});
218+
}, { code: 'ERR_INVALID_THIS' });
222219
});
223220
}
224221
}
@@ -240,10 +237,7 @@ function checkInvalidCachedData() {
240237
}
241238

242239
function checkGettersErrors() {
243-
const expectedError = {
244-
code: 'ERR_INVALID_ARG_TYPE',
245-
message: /The "this" argument must be an instance of (?:Module|SourceTextModule)/,
246-
};
240+
const expectedError = { code: 'ERR_INVALID_THIS' };
247241
const getters = ['identifier', 'context', 'namespace', 'status', 'error'];
248242
getters.forEach((getter) => {
249243
assert.throws(() => {

test/parallel/test-vm-module-synthetic.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ const assert = require('assert');
6969
for (const value of [null, {}, SyntheticModule.prototype]) {
7070
assert.throws(() => {
7171
SyntheticModule.prototype.setExport.call(value, 'foo');
72-
}, {
73-
code: 'ERR_INVALID_ARG_TYPE',
74-
message: /The "this" argument must be an instance of SyntheticModule/
75-
});
72+
}, { code: 'ERR_INVALID_THIS' });
7673
}
7774

7875
})().then(common.mustCall());

0 commit comments

Comments
 (0)