Skip to content

Commit dcfbbac

Browse files
seppevsmhdawson
authored andcommitted
path: use internal/errors.js
PR-URL: #11319 Ref: #11273 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 4c5cbb7 commit dcfbbac

File tree

4 files changed

+25
-35
lines changed

4 files changed

+25
-35
lines changed

lib/path.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
'use strict';
2323

24-
const inspect = require('util').inspect;
24+
const errors = require('internal/errors');
2525

2626
function assertPath(path) {
2727
if (typeof path !== 'string') {
28-
throw new TypeError('Path must be a string. Received ' + inspect(path));
28+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'path', 'string');
2929
}
3030
}
3131

@@ -816,7 +816,7 @@ const win32 = {
816816

817817
basename: function basename(path, ext) {
818818
if (ext !== undefined && typeof ext !== 'string')
819-
throw new TypeError('"ext" argument must be a string');
819+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'ext', 'string');
820820
assertPath(path);
821821
var start = 0;
822822
var end = -1;
@@ -959,9 +959,8 @@ const win32 = {
959959

960960
format: function format(pathObject) {
961961
if (pathObject === null || typeof pathObject !== 'object') {
962-
throw new TypeError(
963-
`Parameter "pathObject" must be an object, not ${typeof pathObject}`
964-
);
962+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'pathObject', 'Object',
963+
typeof pathObject);
965964
}
966965
return _format('\\', pathObject);
967966
},
@@ -1372,7 +1371,7 @@ const posix = {
13721371

13731372
basename: function basename(path, ext) {
13741373
if (ext !== undefined && typeof ext !== 'string')
1375-
throw new TypeError('"ext" argument must be a string');
1374+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'ext', 'string');
13761375
assertPath(path);
13771376

13781377
var start = 0;
@@ -1503,9 +1502,8 @@ const posix = {
15031502

15041503
format: function format(pathObject) {
15051504
if (pathObject === null || typeof pathObject !== 'object') {
1506-
throw new TypeError(
1507-
`Parameter "pathObject" must be an object, not ${typeof pathObject}`
1508-
);
1505+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'pathObject', 'Object',
1506+
typeof pathObject);
15091507
}
15101508
return _format('/', pathObject);
15111509
},

test/parallel/test-fs-watchfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ assert.throws(function() {
1616

1717
assert.throws(function() {
1818
fs.watchFile(new Object(), common.noop);
19-
}, /Path must be a string/);
19+
}, common.expectsError({code: 'ERR_INVALID_ARG_TYPE', type: TypeError}));
2020

2121
const enoentFile = path.join(common.tmpDir, 'non-existent-file');
2222
const expectedStatObject = new fs.Stats(

test/parallel/test-path-parse-format.js

+15-23
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525
const path = require('path');
2626

@@ -88,29 +88,21 @@ const unixSpecialCaseFormatTests = [
8888
[{}, '']
8989
];
9090

91+
const expectedMessage = common.expectsError({
92+
code: 'ERR_INVALID_ARG_TYPE',
93+
type: TypeError
94+
});
95+
9196
const errors = [
92-
{method: 'parse', input: [null],
93-
message: /^TypeError: Path must be a string\. Received null$/},
94-
{method: 'parse', input: [{}],
95-
message: /^TypeError: Path must be a string\. Received {}$/},
96-
{method: 'parse', input: [true],
97-
message: /^TypeError: Path must be a string\. Received true$/},
98-
{method: 'parse', input: [1],
99-
message: /^TypeError: Path must be a string\. Received 1$/},
100-
{method: 'parse', input: [],
101-
message: /^TypeError: Path must be a string\. Received undefined$/},
102-
{method: 'format', input: [null],
103-
message:
104-
/^TypeError: Parameter "pathObject" must be an object, not object$/},
105-
{method: 'format', input: [''],
106-
message:
107-
/^TypeError: Parameter "pathObject" must be an object, not string$/},
108-
{method: 'format', input: [true],
109-
message:
110-
/^TypeError: Parameter "pathObject" must be an object, not boolean$/},
111-
{method: 'format', input: [1],
112-
message:
113-
/^TypeError: Parameter "pathObject" must be an object, not number$/},
97+
{method: 'parse', input: [null], message: expectedMessage},
98+
{method: 'parse', input: [{}], message: expectedMessage},
99+
{method: 'parse', input: [true], message: expectedMessage},
100+
{method: 'parse', input: [1], message: expectedMessage},
101+
{method: 'parse', input: [], message: expectedMessage},
102+
{method: 'format', input: [null], message: expectedMessage},
103+
{method: 'format', input: [''], message: expectedMessage},
104+
{method: 'format', input: [true], message: expectedMessage},
105+
{method: 'format', input: [1], message: expectedMessage},
114106
];
115107

116108
checkParseFormat(path.win32, winPaths);

test/parallel/test-path.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ function fail(fn) {
369369

370370
assert.throws(() => {
371371
fn.apply(null, args);
372-
}, TypeError);
372+
}, common.expectsError({code: 'ERR_INVALID_ARG_TYPE', type: TypeError}));
373373
}
374374

375375
typeErrorTests.forEach((test) => {

0 commit comments

Comments
 (0)