errors,url: port url errors to internal/errors
PR-URL: https://github.com/nodejs/node/pull/13963 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
fc463639fa
commit
473f0eff29
@ -26,6 +26,8 @@ const { toASCII } = process.binding('config').hasIntl ?
|
|||||||
|
|
||||||
const { hexTable } = require('internal/querystring');
|
const { hexTable } = require('internal/querystring');
|
||||||
|
|
||||||
|
const errors = require('internal/errors');
|
||||||
|
|
||||||
// WHATWG URL implementation provided by internal/url
|
// WHATWG URL implementation provided by internal/url
|
||||||
const {
|
const {
|
||||||
URL,
|
URL,
|
||||||
@ -99,7 +101,7 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
|
|||||||
|
|
||||||
Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
|
Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
|
||||||
if (typeof url !== 'string') {
|
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.
|
// Copy chrome, IE, opera backslash-handling behavior.
|
||||||
@ -556,8 +558,7 @@ function urlFormat(obj, options) {
|
|||||||
if (typeof obj === 'string') {
|
if (typeof obj === 'string') {
|
||||||
obj = urlParse(obj);
|
obj = urlParse(obj);
|
||||||
} else if (typeof obj !== 'object' || obj === null) {
|
} else if (typeof obj !== 'object' || obj === null) {
|
||||||
throw new TypeError('Parameter "urlObj" must be an object, not ' +
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'urlObj', 'object', obj);
|
||||||
(obj === null ? 'null' : typeof obj));
|
|
||||||
} else if (!(obj instanceof Url)) {
|
} else if (!(obj instanceof Url)) {
|
||||||
var format = obj[formatSymbol];
|
var format = obj[formatSymbol];
|
||||||
return format ?
|
return format ?
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const url = require('url');
|
const url = require('url');
|
||||||
|
|
||||||
@ -14,8 +14,12 @@ const throwsObjsAndReportTypes = new Map([
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
for (const [obj, type] of throwsObjsAndReportTypes) {
|
for (const [obj, type] of throwsObjsAndReportTypes) {
|
||||||
const error = new RegExp(
|
const error = common.expectsError({
|
||||||
`^TypeError: Parameter "urlObj" must be an object, not ${type}$`);
|
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.throws(function() { url.format(obj); }, error);
|
||||||
}
|
}
|
||||||
assert.strictEqual(url.format(''), '');
|
assert.strictEqual(url.format(''), '');
|
||||||
|
@ -1,23 +1,27 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const url = require('url');
|
const url = require('url');
|
||||||
|
|
||||||
// https://github.com/joyent/node/issues/568
|
// https://github.com/joyent/node/issues/568
|
||||||
const errMessage = /^TypeError: Parameter "url" must be a string, not (?:undefined|boolean|number|object|function|symbol)$/;
|
|
||||||
[
|
[
|
||||||
undefined,
|
[undefined, 'undefined'],
|
||||||
null,
|
[null, 'null'],
|
||||||
true,
|
[true, 'boolean'],
|
||||||
false,
|
[false, 'boolean'],
|
||||||
0.0,
|
[0.0, 'number'],
|
||||||
0,
|
[0, 'number'],
|
||||||
[],
|
[[], 'object'],
|
||||||
{},
|
[{}, 'object'],
|
||||||
() => {},
|
[() => {}, 'function'],
|
||||||
Symbol('foo')
|
[Symbol('foo'), 'symbol']
|
||||||
].forEach((val) => {
|
].forEach(([val, type]) => {
|
||||||
assert.throws(() => { url.parse(val); }, errMessage);
|
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'); },
|
assert.throws(() => { url.parse('http://%E0%A4%A@fail'); },
|
||||||
|
Loading…
x
Reference in New Issue
Block a user