Skip to content

Commit 9788e96

Browse files
Rami Mosherefack
Rami Moshe
authored andcommitted
querystring: convert to using internal/errors
PR-URL: #15565 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
1 parent 1c07724 commit 9788e96

File tree

5 files changed

+33
-12
lines changed

5 files changed

+33
-12
lines changed

doc/api/errors.md

+5
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,11 @@ API] [`URLSearchParams` constructor][`new URLSearchParams(iterable)`] does not
11161116
represent a `[name, value]` tuple – that is, if an element is not iterable, or
11171117
does not consist of exactly two elements.
11181118

1119+
<a id="ERR_INVALID_URI"></a>
1120+
### ERR_INVALID_URI
1121+
1122+
Used when an invalid URI is passed.
1123+
11191124
<a id="ERR_INVALID_URL"></a>
11201125
### ERR_INVALID_URL
11211126

lib/internal/errors.js

+2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ module.exports = exports = {
126126
Error: makeNodeError(Error),
127127
TypeError: makeNodeError(TypeError),
128128
RangeError: makeNodeError(RangeError),
129+
URIError: makeNodeError(URIError),
129130
AssertionError,
130131
E // This is exported only to facilitate testing.
131132
};
@@ -287,6 +288,7 @@ E('ERR_INVALID_SYNC_FORK_INPUT',
287288
'Asynchronous forks do not support Buffer, Uint8Array or string input: %s');
288289
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s');
289290
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple');
291+
E('ERR_INVALID_URI', 'URI malformed');
290292
E('ERR_INVALID_URL', 'Invalid URL: %s');
291293
E('ERR_INVALID_URL_SCHEME',
292294
(expected) => `The URL must be ${oneOf(expected, 'scheme')}`);

lib/querystring.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
'use strict';
2525

2626
const { Buffer } = require('buffer');
27+
const errors = require('internal/errors');
2728
const {
2829
hexTable,
2930
isHexTable
@@ -174,11 +175,12 @@ function qsEscape(str) {
174175
}
175176
// Surrogate pair
176177
++i;
177-
var c2;
178-
if (i < str.length)
179-
c2 = str.charCodeAt(i) & 0x3FF;
180-
else
181-
throw new URIError('URI malformed');
178+
179+
if (i >= str.length)
180+
throw new errors.URIError('ERR_INVALID_URI');
181+
182+
var c2 = str.charCodeAt(i) & 0x3FF;
183+
182184
lastPos = i + 1;
183185
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
184186
out += hexTable[0xF0 | (c >> 18)] +

test/parallel/test-querystring-escape.js

+10-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

55
const qs = require('querystring');
@@ -12,8 +12,15 @@ assert.deepStrictEqual(qs.escape('Ŋōđĕ'), '%C5%8A%C5%8D%C4%91%C4%95');
1212
assert.deepStrictEqual(qs.escape('testŊōđĕ'), 'test%C5%8A%C5%8D%C4%91%C4%95');
1313
assert.deepStrictEqual(qs.escape(`${String.fromCharCode(0xD800 + 1)}test`),
1414
'%F0%90%91%B4est');
15-
assert.throws(() => qs.escape(String.fromCharCode(0xD800 + 1)),
16-
/^URIError: URI malformed$/);
15+
16+
common.expectsError(
17+
() => qs.escape(String.fromCharCode(0xD800 + 1)),
18+
{
19+
code: 'ERR_INVALID_URI',
20+
type: URIError,
21+
message: 'URI malformed'
22+
}
23+
);
1724

1825
// using toString for objects
1926
assert.strictEqual(

test/parallel/test-querystring.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525
const inspect = require('util').inspect;
2626

@@ -271,9 +271,14 @@ qsWeirdObjects.forEach(function(testCase) {
271271
});
272272

273273
// invalid surrogate pair throws URIError
274-
assert.throws(function() {
275-
qs.stringify({ foo: '\udc00' });
276-
}, /^URIError: URI malformed$/);
274+
common.expectsError(
275+
() => qs.stringify({ foo: '\udc00' }),
276+
{
277+
code: 'ERR_INVALID_URI',
278+
type: URIError,
279+
message: 'URI malformed'
280+
}
281+
);
277282

278283
// coerce numbers to string
279284
assert.strictEqual('foo=0', qs.stringify({ foo: 0 }));

0 commit comments

Comments
 (0)