Skip to content

Commit 473f0ef

Browse files
starkwangrefack
authored andcommitted
errors,url: port url errors to internal/errors
PR-URL: #13963 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent fc46363 commit 473f0ef

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

lib/url.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const { toASCII } = process.binding('config').hasIntl ?
2626

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

29+
const errors = require('internal/errors');
30+
2931
// WHATWG URL implementation provided by internal/url
3032
const {
3133
URL,
@@ -99,7 +101,7 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
99101

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

105107
// Copy chrome, IE, opera backslash-handling behavior.
@@ -556,8 +558,7 @@ function urlFormat(obj, options) {
556558
if (typeof obj === 'string') {
557559
obj = urlParse(obj);
558560
} else if (typeof obj !== 'object' || obj === null) {
559-
throw new TypeError('Parameter "urlObj" must be an object, not ' +
560-
(obj === null ? 'null' : typeof obj));
561+
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'urlObj', 'object', obj);
561562
} else if (!(obj instanceof Url)) {
562563
var format = obj[formatSymbol];
563564
return format ?

test/parallel/test-url-format-invalid-input.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const url = require('url');
55

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

1616
for (const [obj, type] of throwsObjsAndReportTypes) {
17-
const error = new RegExp(
18-
`^TypeError: Parameter "urlObj" must be an object, not ${type}$`);
17+
const error = common.expectsError({
18+
code: 'ERR_INVALID_ARG_TYPE',
19+
type: TypeError,
20+
message: 'The "urlObj" argument must be of type object. ' +
21+
`Received type ${type}`
22+
});
1923
assert.throws(function() { url.format(obj); }, error);
2024
}
2125
assert.strictEqual(url.format(''), '');

test/parallel/test-url-parse-invalid-input.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const url = require('url');
55

66
// https://github.com/joyent/node/issues/568
7-
const errMessage = /^TypeError: Parameter "url" must be a string, not (?:undefined|boolean|number|object|function|symbol)$/;
87
[
9-
undefined,
10-
null,
11-
true,
12-
false,
13-
0.0,
14-
0,
15-
[],
16-
{},
17-
() => {},
18-
Symbol('foo')
19-
].forEach((val) => {
20-
assert.throws(() => { url.parse(val); }, errMessage);
8+
[undefined, 'undefined'],
9+
[null, 'null'],
10+
[true, 'boolean'],
11+
[false, 'boolean'],
12+
[0.0, 'number'],
13+
[0, 'number'],
14+
[[], 'object'],
15+
[{}, 'object'],
16+
[() => {}, 'function'],
17+
[Symbol('foo'), 'symbol']
18+
].forEach(([val, type]) => {
19+
const error = common.expectsError({
20+
code: 'ERR_INVALID_ARG_TYPE',
21+
type: TypeError,
22+
message: `The "url" argument must be of type string. Received type ${type}`
23+
});
24+
assert.throws(() => { url.parse(val); }, error);
2125
});
2226

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

0 commit comments

Comments
 (0)