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, url: port url errors to internal/errors #13963

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
7 changes: 4 additions & 3 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const { toASCII } = process.binding('config').hasIntl ?

const { hexTable } = require('internal/querystring');

const errors = require('internal/errors');

// WHATWG URL implementation provided by internal/url
const {
URL,
Expand Down Expand Up @@ -99,7 +101,7 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {

Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
if (typeof url !== 'string') {
throw new TypeError('Parameter "url" must be a string, not ' + typeof url);
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'url', 'string', url);
}

// Copy chrome, IE, opera backslash-handling behavior.
Expand Down Expand Up @@ -556,8 +558,7 @@ function urlFormat(obj, options) {
if (typeof obj === 'string') {
obj = urlParse(obj);
} else if (typeof obj !== 'object' || obj === null) {
throw new TypeError('Parameter "urlObj" must be an object, not ' +
(obj === null ? 'null' : typeof obj));
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'urlObj', 'object', obj);
} else if (!(obj instanceof Url)) {
var format = obj[formatSymbol];
return format ?
Expand Down
10 changes: 7 additions & 3 deletions test/parallel/test-url-format-invalid-input.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const url = require('url');

Expand All @@ -14,8 +14,12 @@ const throwsObjsAndReportTypes = new Map([
]);

for (const [obj, type] of throwsObjsAndReportTypes) {
const error = new RegExp(
`^TypeError: Parameter "urlObj" must be an object, not ${type}$`);
const error = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "urlObj" argument must be of type object. ' +
`Received type ${type}`
});
assert.throws(function() { url.format(obj); }, error);
}
assert.strictEqual(url.format(''), '');
Expand Down
32 changes: 18 additions & 14 deletions test/parallel/test-url-parse-invalid-input.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const url = require('url');

// https://github.com/joyent/node/issues/568
const errMessage = /^TypeError: Parameter "url" must be a string, not (?:undefined|boolean|number|object|function|symbol)$/;
[
undefined,
null,
true,
false,
0.0,
0,
[],
{},
() => {},
Symbol('foo')
].forEach((val) => {
assert.throws(() => { url.parse(val); }, errMessage);
[undefined, 'undefined'],
[null, 'null'],
[true, 'boolean'],
[false, 'boolean'],
[0.0, 'number'],
[0, 'number'],
[[], 'object'],
[{}, 'object'],
[() => {}, 'function'],
[Symbol('foo'), 'symbol']
].forEach(([val, type]) => {
const error = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: `The "url" argument must be of type string. Received type ${type}`
});
assert.throws(() => { url.parse(val); }, error);
});

assert.throws(() => { url.parse('http://%E0%A4%A@fail'); },
Expand Down