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

added descriptive error messages for URL parser #2016

Merged
merged 3 commits into from
Mar 22, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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