path: use internal/errors.js
PR-URL: https://github.com/nodejs/node/pull/11319 Ref: https://github.com/nodejs/node/issues/11273 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
This commit is contained in:
parent
4c5cbb7c83
commit
dcfbbacba8
18
lib/path.js
18
lib/path.js
@ -21,11 +21,11 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const inspect = require('util').inspect;
|
||||
const errors = require('internal/errors');
|
||||
|
||||
function assertPath(path) {
|
||||
if (typeof path !== 'string') {
|
||||
throw new TypeError('Path must be a string. Received ' + inspect(path));
|
||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'path', 'string');
|
||||
}
|
||||
}
|
||||
|
||||
@ -816,7 +816,7 @@ const win32 = {
|
||||
|
||||
basename: function basename(path, ext) {
|
||||
if (ext !== undefined && typeof ext !== 'string')
|
||||
throw new TypeError('"ext" argument must be a string');
|
||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'ext', 'string');
|
||||
assertPath(path);
|
||||
var start = 0;
|
||||
var end = -1;
|
||||
@ -959,9 +959,8 @@ const win32 = {
|
||||
|
||||
format: function format(pathObject) {
|
||||
if (pathObject === null || typeof pathObject !== 'object') {
|
||||
throw new TypeError(
|
||||
`Parameter "pathObject" must be an object, not ${typeof pathObject}`
|
||||
);
|
||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'pathObject', 'Object',
|
||||
typeof pathObject);
|
||||
}
|
||||
return _format('\\', pathObject);
|
||||
},
|
||||
@ -1372,7 +1371,7 @@ const posix = {
|
||||
|
||||
basename: function basename(path, ext) {
|
||||
if (ext !== undefined && typeof ext !== 'string')
|
||||
throw new TypeError('"ext" argument must be a string');
|
||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'ext', 'string');
|
||||
assertPath(path);
|
||||
|
||||
var start = 0;
|
||||
@ -1503,9 +1502,8 @@ const posix = {
|
||||
|
||||
format: function format(pathObject) {
|
||||
if (pathObject === null || typeof pathObject !== 'object') {
|
||||
throw new TypeError(
|
||||
`Parameter "pathObject" must be an object, not ${typeof pathObject}`
|
||||
);
|
||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'pathObject', 'Object',
|
||||
typeof pathObject);
|
||||
}
|
||||
return _format('/', pathObject);
|
||||
},
|
||||
|
@ -16,7 +16,7 @@ assert.throws(function() {
|
||||
|
||||
assert.throws(function() {
|
||||
fs.watchFile(new Object(), common.noop);
|
||||
}, /Path must be a string/);
|
||||
}, common.expectsError({code: 'ERR_INVALID_ARG_TYPE', type: TypeError}));
|
||||
|
||||
const enoentFile = path.join(common.tmpDir, 'non-existent-file');
|
||||
const expectedStatObject = new fs.Stats(
|
||||
|
@ -20,7 +20,7 @@
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
'use strict';
|
||||
require('../common');
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const path = require('path');
|
||||
|
||||
@ -88,29 +88,21 @@ const unixSpecialCaseFormatTests = [
|
||||
[{}, '']
|
||||
];
|
||||
|
||||
const expectedMessage = common.expectsError({
|
||||
code: 'ERR_INVALID_ARG_TYPE',
|
||||
type: TypeError
|
||||
});
|
||||
|
||||
const errors = [
|
||||
{method: 'parse', input: [null],
|
||||
message: /^TypeError: Path must be a string\. Received null$/},
|
||||
{method: 'parse', input: [{}],
|
||||
message: /^TypeError: Path must be a string\. Received {}$/},
|
||||
{method: 'parse', input: [true],
|
||||
message: /^TypeError: Path must be a string\. Received true$/},
|
||||
{method: 'parse', input: [1],
|
||||
message: /^TypeError: Path must be a string\. Received 1$/},
|
||||
{method: 'parse', input: [],
|
||||
message: /^TypeError: Path must be a string\. Received undefined$/},
|
||||
{method: 'format', input: [null],
|
||||
message:
|
||||
/^TypeError: Parameter "pathObject" must be an object, not object$/},
|
||||
{method: 'format', input: [''],
|
||||
message:
|
||||
/^TypeError: Parameter "pathObject" must be an object, not string$/},
|
||||
{method: 'format', input: [true],
|
||||
message:
|
||||
/^TypeError: Parameter "pathObject" must be an object, not boolean$/},
|
||||
{method: 'format', input: [1],
|
||||
message:
|
||||
/^TypeError: Parameter "pathObject" must be an object, not number$/},
|
||||
{method: 'parse', input: [null], message: expectedMessage},
|
||||
{method: 'parse', input: [{}], message: expectedMessage},
|
||||
{method: 'parse', input: [true], message: expectedMessage},
|
||||
{method: 'parse', input: [1], message: expectedMessage},
|
||||
{method: 'parse', input: [], message: expectedMessage},
|
||||
{method: 'format', input: [null], message: expectedMessage},
|
||||
{method: 'format', input: [''], message: expectedMessage},
|
||||
{method: 'format', input: [true], message: expectedMessage},
|
||||
{method: 'format', input: [1], message: expectedMessage},
|
||||
];
|
||||
|
||||
checkParseFormat(path.win32, winPaths);
|
||||
|
@ -369,7 +369,7 @@ function fail(fn) {
|
||||
|
||||
assert.throws(() => {
|
||||
fn.apply(null, args);
|
||||
}, TypeError);
|
||||
}, common.expectsError({code: 'ERR_INVALID_ARG_TYPE', type: TypeError}));
|
||||
}
|
||||
|
||||
typeErrorTests.forEach((test) => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user