From b21715403bc5e229c4171ed996e337526daeadf6 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 25 Jan 2018 04:26:52 +0800 Subject: [PATCH] module: use internal/errors.js in module.require PR-URL: https://github.com/nodejs/node/pull/18359 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- lib/module.js | 13 ++++++++---- test/parallel/test-module-loading-error.js | 24 ++++++++++++++-------- test/sequential/test-module-loading.js | 11 ---------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/module.js b/lib/module.js index 4a8480ed1be3af..5ee537f157289b 100644 --- a/lib/module.js +++ b/lib/module.js @@ -603,10 +603,15 @@ Module.prototype.load = function(filename) { // Loads a module at the given file path. Returns that module's // `exports` property. -Module.prototype.require = function(path) { - assert(path, 'missing path'); - assert(typeof path === 'string', 'path must be a string'); - return Module._load(path, this, /* isMain */ false); +Module.prototype.require = function(id) { + if (typeof id !== 'string') { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'id', 'string', id); + } + 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); }; diff --git a/test/parallel/test-module-loading-error.js b/test/parallel/test-module-loading-error.js index f1c457af2b2c9e..818a35eb582211 100644 --- a/test/parallel/test-module-loading-error.js +++ b/test/parallel/test-module-loading-error.js @@ -59,16 +59,22 @@ assert.throws( } ); -common.expectsError( - require, - { - code: 'ERR_ASSERTION', - message: /^missing path$/ - }); +const re = /^The "id" argument must be of type string\. Received type \w+$/; +[1, false, null, undefined, {}].forEach((value) => { + common.expectsError( + () => { require(value); }, + { + type: TypeError, + code: 'ERR_INVALID_ARG_TYPE', + message: re + }); +}); + common.expectsError( - () => { require({}); }, + () => { require(''); }, { - code: 'ERR_ASSERTION', - message: /^path must be a string$/ + type: Error, + code: 'ERR_INVALID_ARG_VALUE', + message: 'The argument \'id\' must be a non-empty string. Received \'\'' }); diff --git a/test/sequential/test-module-loading.js b/test/sequential/test-module-loading.js index d790850951b064..f0fa933a8ba2b2 100644 --- a/test/sequential/test-module-loading.js +++ b/test/sequential/test-module-loading.js @@ -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() { assert.ok(a.A instanceof Function); assert.strictEqual(a.A(), 'A done');