Skip to content

Commit b217154

Browse files
committed
module: use internal/errors.js in module.require
PR-URL: #18359 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 6ef1730 commit b217154

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

lib/module.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,15 @@ Module.prototype.load = function(filename) {
603603

604604
// Loads a module at the given file path. Returns that module's
605605
// `exports` property.
606-
Module.prototype.require = function(path) {
607-
assert(path, 'missing path');
608-
assert(typeof path === 'string', 'path must be a string');
609-
return Module._load(path, this, /* isMain */ false);
606+
Module.prototype.require = function(id) {
607+
if (typeof id !== 'string') {
608+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'id', 'string', id);
609+
}
610+
if (id === '') {
611+
throw new errors.Error('ERR_INVALID_ARG_VALUE',
612+
'id', id, 'must be a non-empty string');
613+
}
614+
return Module._load(id, this, /* isMain */ false);
610615
};
611616

612617

test/parallel/test-module-loading-error.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,22 @@ assert.throws(
5959
}
6060
);
6161

62-
common.expectsError(
63-
require,
64-
{
65-
code: 'ERR_ASSERTION',
66-
message: /^missing path$/
67-
});
62+
const re = /^The "id" argument must be of type string\. Received type \w+$/;
63+
[1, false, null, undefined, {}].forEach((value) => {
64+
common.expectsError(
65+
() => { require(value); },
66+
{
67+
type: TypeError,
68+
code: 'ERR_INVALID_ARG_TYPE',
69+
message: re
70+
});
71+
});
72+
6873

6974
common.expectsError(
70-
() => { require({}); },
75+
() => { require(''); },
7176
{
72-
code: 'ERR_ASSERTION',
73-
message: /^path must be a string$/
77+
type: Error,
78+
code: 'ERR_INVALID_ARG_VALUE',
79+
message: 'The argument \'id\' must be a non-empty string. Received \'\''
7480
});

test/sequential/test-module-loading.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -297,17 +297,6 @@ try {
297297
}
298298

299299

300-
// require() must take string, and must be truthy
301-
assert.throws(function() {
302-
console.error('require non-string');
303-
require({ foo: 'bar' });
304-
}, /path must be a string/);
305-
306-
assert.throws(function() {
307-
console.error('require empty string');
308-
require('');
309-
}, /missing path/);
310-
311300
process.on('exit', function() {
312301
assert.ok(a.A instanceof Function);
313302
assert.strictEqual(a.A(), 'A done');

0 commit comments

Comments
 (0)