Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors, path: migrate to use internal/errors.js #11319

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
},
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
},
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-watchfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add an assertion on the message as well?
(Changing the message is semver-major so let's enforce it)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a semver-major change, so it's totally fine to change the message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but let's keep it from regressing.


const enoentFile = path.join(common.tmpDir, 'non-existent-file');
const expectedStatObject = new fs.Stats(
Expand Down
38 changes: 15 additions & 23 deletions test/parallel/test-path-parse-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -88,29 +88,21 @@ const unixSpecialCaseFormatTests = [
[{}, '']
];

const expectedMessage = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.
(You can turn this into a Factory that takes the messages)


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},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to agree on whether the tests should check the contents of the message or not, but since have landed tests both with and without, I'm fine with landing as is.

];

checkParseFormat(path.win32, winPaths);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ function fail(fn) {

assert.throws(() => {
fn.apply(null, args);
}, TypeError);
}, common.expectsError({code: 'ERR_INVALID_ARG_TYPE', type: TypeError}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tritto

}

typeErrorTests.forEach((test) => {
Expand Down