module: use internal/errors.js in module.require
PR-URL: https://github.com/nodejs/node/pull/18359 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
6ef17303a7
commit
b21715403b
@ -603,10 +603,15 @@ Module.prototype.load = function(filename) {
|
|||||||
|
|
||||||
// Loads a module at the given file path. Returns that module's
|
// Loads a module at the given file path. Returns that module's
|
||||||
// `exports` property.
|
// `exports` property.
|
||||||
Module.prototype.require = function(path) {
|
Module.prototype.require = function(id) {
|
||||||
assert(path, 'missing path');
|
if (typeof id !== 'string') {
|
||||||
assert(typeof path === 'string', 'path must be a string');
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'id', 'string', id);
|
||||||
return Module._load(path, this, /* isMain */ false);
|
}
|
||||||
|
if (id === '') {
|
||||||
|
throw new errors.Error('ERR_INVALID_ARG_VALUE',
|
||||||
|
'id', id, 'must be a non-empty string');
|
||||||
|
}
|
||||||
|
return Module._load(id, this, /* isMain */ false);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -59,16 +59,22 @@ assert.throws(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const re = /^The "id" argument must be of type string\. Received type \w+$/;
|
||||||
|
[1, false, null, undefined, {}].forEach((value) => {
|
||||||
common.expectsError(
|
common.expectsError(
|
||||||
require,
|
() => { require(value); },
|
||||||
{
|
{
|
||||||
code: 'ERR_ASSERTION',
|
type: TypeError,
|
||||||
message: /^missing path$/
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
|
message: re
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
common.expectsError(
|
common.expectsError(
|
||||||
() => { require({}); },
|
() => { require(''); },
|
||||||
{
|
{
|
||||||
code: 'ERR_ASSERTION',
|
type: Error,
|
||||||
message: /^path must be a string$/
|
code: 'ERR_INVALID_ARG_VALUE',
|
||||||
|
message: 'The argument \'id\' must be a non-empty string. Received \'\''
|
||||||
});
|
});
|
||||||
|
@ -297,17 +297,6 @@ try {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// require() must take string, and must be truthy
|
|
||||||
assert.throws(function() {
|
|
||||||
console.error('require non-string');
|
|
||||||
require({ foo: 'bar' });
|
|
||||||
}, /path must be a string/);
|
|
||||||
|
|
||||||
assert.throws(function() {
|
|
||||||
console.error('require empty string');
|
|
||||||
require('');
|
|
||||||
}, /missing path/);
|
|
||||||
|
|
||||||
process.on('exit', function() {
|
process.on('exit', function() {
|
||||||
assert.ok(a.A instanceof Function);
|
assert.ok(a.A instanceof Function);
|
||||||
assert.strictEqual(a.A(), 'A done');
|
assert.strictEqual(a.A(), 'A done');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user