Skip to content

Commit

Permalink
added descriptive error messages for URL parser (#2016)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
RishabhKodes committed Mar 22, 2023
1 parent 7276126 commit e6fc80f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,38 @@ function parseURL (url) {
url = new URL(url)

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

return url
}

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

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

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

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

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

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

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

if (!(url instanceof URL)) {
Expand Down
8 changes: 4 additions & 4 deletions test/client-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ test('invalid options throws', (t) => {
t.fail()
} catch (err) {
t.type(err, errors.InvalidArgumentError)
t.equal(err.message, 'invalid port')
t.equal(err.message, 'Invalid URL: port must be a valid integer or a string representation of an integer.')
}

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

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

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

try {
Expand Down

0 comments on commit e6fc80f

Please sign in to comment.