Skip to content

Commit e6fc80f

Browse files
authoredMar 22, 2023
added descriptive error messages for URL parser (#2016)
* added descriptive error messages Signed-off-by: Rishabh Bhandari <rishabhbhandari6@gmail.com> * updating error msgs in tests Signed-off-by: Rishabh Bhandari <rishabhbhandari6@gmail.com> * updated error messages Signed-off-by: Rishabh Bhandari <rishabhbhandari6@gmail.com> --------- Signed-off-by: Rishabh Bhandari <rishabhbhandari6@gmail.com>
1 parent 7276126 commit e6fc80f

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed
 

‎lib/core/util.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -48,38 +48,38 @@ function parseURL (url) {
4848
url = new URL(url)
4949

5050
if (!/^https?:/.test(url.origin || url.protocol)) {
51-
throw new InvalidArgumentError('invalid protocol')
51+
throw new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.')
5252
}
5353

5454
return url
5555
}
5656

5757
if (!url || typeof url !== 'object') {
58-
throw new InvalidArgumentError('invalid url')
58+
throw new InvalidArgumentError('Invalid URL: The URL argument must be a non-null object.')
5959
}
6060

6161
if (url.port != null && url.port !== '' && !Number.isFinite(parseInt(url.port))) {
62-
throw new InvalidArgumentError('invalid port')
62+
throw new InvalidArgumentError('Invalid URL: port must be a valid integer or a string representation of an integer.')
6363
}
6464

6565
if (url.path != null && typeof url.path !== 'string') {
66-
throw new InvalidArgumentError('invalid path')
66+
throw new InvalidArgumentError('Invalid URL path: the path must be a string or null/undefined.')
6767
}
6868

6969
if (url.pathname != null && typeof url.pathname !== 'string') {
70-
throw new InvalidArgumentError('invalid pathname')
70+
throw new InvalidArgumentError('Invalid URL pathname: the pathname must be a string or null/undefined.')
7171
}
7272

7373
if (url.hostname != null && typeof url.hostname !== 'string') {
74-
throw new InvalidArgumentError('invalid hostname')
74+
throw new InvalidArgumentError('Invalid URL hostname: the hostname must be a string or null/undefined.')
7575
}
7676

7777
if (url.origin != null && typeof url.origin !== 'string') {
78-
throw new InvalidArgumentError('invalid origin')
78+
throw new InvalidArgumentError('Invalid URL origin: the origin must be a string or null/undefined.')
7979
}
8080

8181
if (!/^https?:/.test(url.origin || url.protocol)) {
82-
throw new InvalidArgumentError('invalid protocol')
82+
throw new InvalidArgumentError('Invalid URL protocol: the URL must start with `http:` or `https:`.')
8383
}
8484

8585
if (!(url instanceof URL)) {

‎test/client-errors.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ test('invalid options throws', (t) => {
260260
t.fail()
261261
} catch (err) {
262262
t.type(err, errors.InvalidArgumentError)
263-
t.equal(err.message, 'invalid port')
263+
t.equal(err.message, 'Invalid URL: port must be a valid integer or a string representation of an integer.')
264264
}
265265

266266
try {
@@ -364,7 +364,7 @@ test('invalid options throws', (t) => {
364364
t.fail()
365365
} catch (err) {
366366
t.type(err, errors.InvalidArgumentError)
367-
t.equal(err.message, 'invalid protocol')
367+
t.equal(err.message, 'Invalid URL protocol: the URL must start with `http:` or `https:`.')
368368
}
369369

370370
try {
@@ -374,7 +374,7 @@ test('invalid options throws', (t) => {
374374
t.fail()
375375
} catch (err) {
376376
t.type(err, errors.InvalidArgumentError)
377-
t.equal(err.message, 'invalid hostname')
377+
t.equal(err.message, 'Invalid URL hostname: the hostname must be a string or null/undefined.')
378378
}
379379

380380
try {
@@ -392,7 +392,7 @@ test('invalid options throws', (t) => {
392392
t.fail()
393393
} catch (err) {
394394
t.type(err, errors.InvalidArgumentError)
395-
t.equal(err.message, 'invalid url')
395+
t.equal(err.message, 'Invalid URL: The URL argument must be a non-null object.')
396396
}
397397

398398
try {

0 commit comments

Comments
 (0)
Please sign in to comment.