From 1d2fd8b65bacaf4401450edc8ed529106cbcfc67 Mon Sep 17 00:00:00 2001 From: Michaƫl Zasso Date: Sun, 4 Mar 2018 22:16:24 +0100 Subject: lib: port remaining errors to new system PR-URL: https://github.com/nodejs/node/pull/19137 Reviewed-By: Anatoli Papirovski Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Joyee Cheung --- lib/internal/url.js | 91 +++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 41 deletions(-) (limited to 'lib/internal/url.js') diff --git a/lib/internal/url.js b/lib/internal/url.js index 0bdfc4783e..842d26c4aa 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -7,7 +7,18 @@ const { } = require('internal/querystring'); const { getConstructorOf, removeColors } = require('internal/util'); -const errors = require('internal/errors'); +const { + ERR_ARG_NOT_ITERABLE, + ERR_INVALID_ARG_TYPE, + ERR_INVALID_CALLBACK, + ERR_INVALID_FILE_URL_HOST, + ERR_INVALID_FILE_URL_PATH, + ERR_INVALID_THIS, + ERR_INVALID_TUPLE, + ERR_INVALID_URL, + ERR_INVALID_URL_SCHEME, + ERR_MISSING_ARGS +} = require('internal/errors').codes; const querystring = require('querystring'); const { platform } = process; @@ -107,7 +118,7 @@ class URLSearchParams { this[searchParams] = childParams.slice(); } else if (method !== null && method !== undefined) { if (typeof method !== 'function') { - throw new errors.TypeError('ERR_ARG_NOT_ITERABLE', 'Query pairs'); + throw new ERR_ARG_NOT_ITERABLE('Query pairs'); } // sequence> @@ -117,8 +128,7 @@ class URLSearchParams { if ((typeof pair !== 'object' && typeof pair !== 'function') || pair === null || typeof pair[Symbol.iterator] !== 'function') { - throw new errors.TypeError('ERR_INVALID_TUPLE', 'Each query pair', - '[name, value]'); + throw new ERR_INVALID_TUPLE('Each query pair', '[name, value]'); } const convertedPair = []; for (const element of pair) @@ -129,8 +139,7 @@ class URLSearchParams { this[searchParams] = []; for (const pair of pairs) { if (pair.length !== 2) { - throw new errors.TypeError('ERR_INVALID_TUPLE', 'Each query pair', - '[name, value]'); + throw new ERR_INVALID_TUPLE('Each query pair', '[name, value]'); } this[searchParams].push(pair[0], pair[1]); } @@ -162,7 +171,7 @@ class URLSearchParams { [util.inspect.custom](recurseTimes, ctx) { if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); + throw new ERR_INVALID_THIS('URLSearchParams'); } if (typeof recurseTimes === 'number' && recurseTimes < 0) @@ -214,7 +223,7 @@ function onParseComplete(flags, protocol, username, password, } function onParseError(flags, input) { - const error = new errors.TypeError('ERR_INVALID_URL', input); + const error = new ERR_INVALID_URL(input); error.input = input; throw error; } @@ -328,7 +337,7 @@ class URL { [util.inspect.custom](depth, opts) { if (this == null || Object.getPrototypeOf(this[context]) !== URLContext.prototype) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URL'); + throw new ERR_INVALID_THIS('URL'); } if (typeof depth === 'number' && depth < 0) @@ -370,7 +379,7 @@ Object.defineProperties(URL.prototype, { // eslint-disable-next-line func-name-matching value: function format(options) { if (options && typeof options !== 'object') - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object'); + throw new ERR_INVALID_ARG_TYPE('options', 'Object'); options = util._extend({ fragment: true, unicode: false, @@ -943,10 +952,10 @@ function merge(out, start, mid, end, lBuffer, rBuffer) { defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { append(name, value) { if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); + throw new ERR_INVALID_THIS('URLSearchParams'); } if (arguments.length < 2) { - throw new errors.TypeError('ERR_MISSING_ARGS', 'name', 'value'); + throw new ERR_MISSING_ARGS('name', 'value'); } name = toUSVString(name); @@ -957,10 +966,10 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { delete(name) { if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); + throw new ERR_INVALID_THIS('URLSearchParams'); } if (arguments.length < 1) { - throw new errors.TypeError('ERR_MISSING_ARGS', 'name'); + throw new ERR_MISSING_ARGS('name'); } const list = this[searchParams]; @@ -978,10 +987,10 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { get(name) { if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); + throw new ERR_INVALID_THIS('URLSearchParams'); } if (arguments.length < 1) { - throw new errors.TypeError('ERR_MISSING_ARGS', 'name'); + throw new ERR_MISSING_ARGS('name'); } const list = this[searchParams]; @@ -996,10 +1005,10 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { getAll(name) { if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); + throw new ERR_INVALID_THIS('URLSearchParams'); } if (arguments.length < 1) { - throw new errors.TypeError('ERR_MISSING_ARGS', 'name'); + throw new ERR_MISSING_ARGS('name'); } const list = this[searchParams]; @@ -1015,10 +1024,10 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { has(name) { if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); + throw new ERR_INVALID_THIS('URLSearchParams'); } if (arguments.length < 1) { - throw new errors.TypeError('ERR_MISSING_ARGS', 'name'); + throw new ERR_MISSING_ARGS('name'); } const list = this[searchParams]; @@ -1033,10 +1042,10 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { set(name, value) { if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); + throw new ERR_INVALID_THIS('URLSearchParams'); } if (arguments.length < 2) { - throw new errors.TypeError('ERR_MISSING_ARGS', 'name', 'value'); + throw new ERR_MISSING_ARGS('name', 'value'); } const list = this[searchParams]; @@ -1120,7 +1129,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { // must be set to `entries`. entries() { if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); + throw new ERR_INVALID_THIS('URLSearchParams'); } return createSearchParamsIterator(this, 'key+value'); @@ -1128,10 +1137,10 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { forEach(callback, thisArg = undefined) { if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); + throw new ERR_INVALID_THIS('URLSearchParams'); } if (typeof callback !== 'function') { - throw new errors.TypeError('ERR_INVALID_CALLBACK'); + throw new ERR_INVALID_CALLBACK(); } let list = this[searchParams]; @@ -1150,7 +1159,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { // https://heycam.github.io/webidl/#es-iterable keys() { if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); + throw new ERR_INVALID_THIS('URLSearchParams'); } return createSearchParamsIterator(this, 'key'); @@ -1158,7 +1167,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { values() { if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); + throw new ERR_INVALID_THIS('URLSearchParams'); } return createSearchParamsIterator(this, 'value'); @@ -1168,7 +1177,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', { // https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior toString() { if (!this || !this[searchParams] || this[searchParams][searchParams]) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParams'); + throw new ERR_INVALID_THIS('URLSearchParams'); } return serializeParams(this[searchParams]); @@ -1200,7 +1209,7 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParams Iterator', { next() { if (!this || Object.getPrototypeOf(this) !== URLSearchParamsIteratorPrototype) { - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParamsIterator'); + throw new ERR_INVALID_THIS('URLSearchParamsIterator'); } const { @@ -1237,7 +1246,7 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParams Iterator', { }, [util.inspect.custom](recurseTimes, ctx) { if (this == null || this[context] == null || this[context].target == null) - throw new errors.TypeError('ERR_INVALID_THIS', 'URLSearchParamsIterator'); + throw new ERR_INVALID_THIS('URLSearchParamsIterator'); if (typeof recurseTimes === 'number' && recurseTimes < 0) return ctx.stylize('[Object]', 'special'); @@ -1276,7 +1285,7 @@ defineIDLClass(URLSearchParamsIteratorPrototype, 'URLSearchParams Iterator', { function domainToASCII(domain) { if (arguments.length < 1) - throw new errors.TypeError('ERR_MISSING_ARGS', 'domain'); + throw new ERR_MISSING_ARGS('domain'); // toUSVString is not needed. return _domainToASCII(`${domain}`); @@ -1284,7 +1293,7 @@ function domainToASCII(domain) { function domainToUnicode(domain) { if (arguments.length < 1) - throw new errors.TypeError('ERR_MISSING_ARGS', 'domain'); + throw new ERR_MISSING_ARGS('domain'); // toUSVString is not needed. return _domainToUnicode(`${domain}`); @@ -1320,9 +1329,9 @@ function getPathFromURLWin32(url) { var third = pathname.codePointAt(n + 2) | 0x20; if ((pathname[n + 1] === '2' && third === 102) || // 2f 2F / (pathname[n + 1] === '5' && third === 99)) { // 5c 5C \ - throw new errors.TypeError( - 'ERR_INVALID_FILE_URL_PATH', - 'must not include encoded \\ or / characters'); + throw new ERR_INVALID_FILE_URL_PATH( + 'must not include encoded \\ or / characters' + ); } } } @@ -1341,8 +1350,7 @@ function getPathFromURLWin32(url) { var sep = pathname[2]; if (letter < 97 || letter > 122 || // a..z A..Z (sep !== ':')) { - throw new errors.TypeError('ERR_INVALID_FILE_URL_PATH', - 'must be absolute'); + throw new ERR_INVALID_FILE_URL_PATH('must be absolute'); } return pathname.slice(1); } @@ -1350,15 +1358,16 @@ function getPathFromURLWin32(url) { function getPathFromURLPosix(url) { if (url.hostname !== '') { - throw new errors.TypeError('ERR_INVALID_FILE_URL_HOST', platform); + throw new ERR_INVALID_FILE_URL_HOST(platform); } var pathname = url.pathname; for (var n = 0; n < pathname.length; n++) { if (pathname[n] === '%') { var third = pathname.codePointAt(n + 2) | 0x20; if (pathname[n + 1] === '2' && third === 102) { - throw new errors.TypeError('ERR_INVALID_FILE_URL_PATH', - 'must not include encoded / characters'); + throw new ERR_INVALID_FILE_URL_PATH( + 'must not include encoded / characters' + ); } } } @@ -1371,7 +1380,7 @@ function getPathFromURL(path) { return path; } if (path.protocol !== 'file:') - throw new errors.TypeError('ERR_INVALID_URL_SCHEME', 'file'); + throw new ERR_INVALID_URL_SCHEME('file'); return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path); } -- cgit v1.2.3