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

lib/querystring: convert to using internal/errors #15565

Merged
merged 1 commit into from
Oct 28, 2017
Merged
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
5 changes: 5 additions & 0 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,11 @@ API] [`URLSearchParams` constructor][`new URLSearchParams(iterable)`] does not
represent a `[name, value]` tuple – that is, if an element is not iterable, or
does not consist of exactly two elements.

<a id="ERR_INVALID_URI"></a>
### ERR_INVALID_URI

Used when an invalid URI is passed.

<a id="ERR_INVALID_URL"></a>
### ERR_INVALID_URL

Expand Down
2 changes: 2 additions & 0 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ module.exports = exports = {
Error: makeNodeError(Error),
TypeError: makeNodeError(TypeError),
RangeError: makeNodeError(RangeError),
URIError: makeNodeError(URIError),
AssertionError,
E // This is exported only to facilitate testing.
};
Expand Down Expand Up @@ -287,6 +288,7 @@ E('ERR_INVALID_SYNC_FORK_INPUT',
'Asynchronous forks do not support Buffer, Uint8Array or string input: %s');
E('ERR_INVALID_THIS', 'Value of "this" must be of type %s');
E('ERR_INVALID_TUPLE', '%s must be an iterable %s tuple');
E('ERR_INVALID_URI', 'URI malformed');
E('ERR_INVALID_URL', 'Invalid URL: %s');
E('ERR_INVALID_URL_SCHEME',
(expected) => `The URL must be ${oneOf(expected, 'scheme')}`);
Expand Down
12 changes: 7 additions & 5 deletions lib/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'use strict';

const { Buffer } = require('buffer');
const errors = require('internal/errors');
const {
hexTable,
isHexTable
Expand Down Expand Up @@ -174,11 +175,12 @@ function qsEscape(str) {
}
// Surrogate pair
++i;
var c2;
if (i < str.length)
c2 = str.charCodeAt(i) & 0x3FF;
else
throw new URIError('URI malformed');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this should to be changed to use internal/errors. URIError seems fine to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would definitely recommend to change this to use internal/errors but it should be kept as URIError as I mentioned above (#15565 (comment)).


if (i >= str.length)
throw new errors.URIError('ERR_INVALID_URI');

var c2 = str.charCodeAt(i) & 0x3FF;

lastPos = i + 1;
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
out += hexTable[0xF0 | (c >> 18)] +
Expand Down
13 changes: 10 additions & 3 deletions test/parallel/test-querystring-escape.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 qs = require('querystring');
Expand All @@ -12,8 +12,15 @@ assert.deepStrictEqual(qs.escape('Ŋōđĕ'), '%C5%8A%C5%8D%C4%91%C4%95');
assert.deepStrictEqual(qs.escape('testŊōđĕ'), 'test%C5%8A%C5%8D%C4%91%C4%95');
assert.deepStrictEqual(qs.escape(`${String.fromCharCode(0xD800 + 1)}test`),
'%F0%90%91%B4est');
assert.throws(() => qs.escape(String.fromCharCode(0xD800 + 1)),
/^URIError: URI malformed$/);

common.expectsError(
() => qs.escape(String.fromCharCode(0xD800 + 1)),
{
code: 'ERR_INVALID_URI',
type: URIError,
message: 'URI malformed'
}
);

// using toString for objects
assert.strictEqual(
Expand Down
13 changes: 9 additions & 4 deletions test/parallel/test-querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
require('../common');
const common = require('../common');
const assert = require('assert');
const inspect = require('util').inspect;

Expand Down Expand Up @@ -271,9 +271,14 @@ qsWeirdObjects.forEach(function(testCase) {
});

// invalid surrogate pair throws URIError
assert.throws(function() {
qs.stringify({ foo: '\udc00' });
}, /^URIError: URI malformed$/);
common.expectsError(
() => qs.stringify({ foo: '\udc00' }),
{
code: 'ERR_INVALID_URI',
type: URIError,
message: 'URI malformed'
}
);

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