diff options
author | Daniel Lenksjö <dlenksjo@gmail.com> | 2020-05-04 16:06:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-04 10:06:47 -0400 |
commit | 1500547afa9c7784967f1915398bc4e1b3a18e08 (patch) | |
tree | 4584393d1fc7eda5d9a7b34c06fd1b653f7a21d3 /cli/js | |
parent | 821a4ae5fd92a03dacf125b35bcf977343d27310 (diff) |
fix: URL constructor throws confusing error on invalid scheme (#5057)
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/tests/url_test.ts | 10 | ||||
-rw-r--r-- | cli/js/web/url.ts | 4 |
2 files changed, 11 insertions, 3 deletions
diff --git a/cli/js/tests/url_test.ts b/cli/js/tests/url_test.ts index 12e7cb26a..6529bf055 100644 --- a/cli/js/tests/url_test.ts +++ b/cli/js/tests/url_test.ts @@ -224,7 +224,7 @@ unitTest(function throwForInvalidPortConstructor(): void { ]; for (const url of urls) { - assertThrows(() => new URL(url)); + assertThrows(() => new URL(url), TypeError, "Invalid URL."); } // Do not throw for 0 & 65535 @@ -232,6 +232,14 @@ unitTest(function throwForInvalidPortConstructor(): void { new URL("https://baz.qat:0"); }); +unitTest(function throwForInvalidSchemeConstructor(): void { + assertThrows( + () => new URL("invalid_scheme://baz.qat"), + TypeError, + "Invalid URL." + ); +}); + unitTest(function doNotOverridePortIfInvalid(): void { const initialPort = "3000"; const ports = [ diff --git a/cli/js/web/url.ts b/cli/js/web/url.ts index 1ae24ff87..cdbba36d9 100644 --- a/cli/js/web/url.ts +++ b/cli/js/web/url.ts @@ -370,7 +370,7 @@ export class URLImpl implements URL { throw new TypeError("Invalid URL."); } - const { port } = (urlParts.protocol ? urlParts : baseParts) as URLParts; + const { port } = !urlParts.protocol && baseParts ? baseParts : urlParts; if (this.#validatePort(port) === undefined) { throw new TypeError("Invalid URL."); } @@ -389,7 +389,7 @@ export class URLImpl implements URL { hash: urlParts.hash, }); } else { - throw new TypeError("URL requires a base URL."); + throw new TypeError("Invalid URL."); } this.#updateSearchParams(); |