From 80955dfa616e1ea5c4bcde995f2923fb6e771127 Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Mon, 19 Dec 2022 20:58:02 +0100 Subject: [PATCH] fix: display URL in invalid URL error (#17128) --- cli/tests/testdata/run/077_fetch_empty.ts.out | 2 +- cli/tests/unit/url_test.ts | 30 +++++++++++-------- ext/url/00_url.js | 9 ++++-- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/cli/tests/testdata/run/077_fetch_empty.ts.out b/cli/tests/testdata/run/077_fetch_empty.ts.out index e546cfcec4..7975743509 100644 --- a/cli/tests/testdata/run/077_fetch_empty.ts.out +++ b/cli/tests/testdata/run/077_fetch_empty.ts.out @@ -1,2 +1,2 @@ -[WILDCARD]error: Uncaught TypeError: Invalid URL +[WILDCARD]error: Uncaught TypeError: Invalid URL: '' [WILDCARD] diff --git a/cli/tests/unit/url_test.ts b/cli/tests/unit/url_test.ts index 0ba848adda..1faf33cd0d 100644 --- a/cli/tests/unit/url_test.ts +++ b/cli/tests/unit/url_test.ts @@ -35,18 +35,24 @@ Deno.test(function urlParsing() { Deno.test(function urlProtocolParsing() { assertEquals(new URL("Aa+-.1://foo").protocol, "aa+-.1:"); assertEquals(new URL("aA+-.1://foo").protocol, "aa+-.1:"); - assertThrows(() => new URL("1://foo"), TypeError, "Invalid URL"); - assertThrows(() => new URL("+://foo"), TypeError, "Invalid URL"); - assertThrows(() => new URL("-://foo"), TypeError, "Invalid URL"); - assertThrows(() => new URL(".://foo"), TypeError, "Invalid URL"); - assertThrows(() => new URL("_://foo"), TypeError, "Invalid URL"); - assertThrows(() => new URL("=://foo"), TypeError, "Invalid URL"); - assertThrows(() => new URL("!://foo"), TypeError, "Invalid URL"); - assertThrows(() => new URL(`"://foo`), TypeError, "Invalid URL"); - assertThrows(() => new URL("$://foo"), TypeError, "Invalid URL"); - assertThrows(() => new URL("%://foo"), TypeError, "Invalid URL"); - assertThrows(() => new URL("^://foo"), TypeError, "Invalid URL"); - assertThrows(() => new URL("*://foo"), TypeError, "Invalid URL"); + assertThrows(() => new URL("1://foo"), TypeError, "Invalid URL: '1://foo'"); + assertThrows(() => new URL("+://foo"), TypeError, "Invalid URL: '+://foo'"); + assertThrows(() => new URL("-://foo"), TypeError, "Invalid URL: '-://foo'"); + assertThrows(() => new URL(".://foo"), TypeError, "Invalid URL: '.://foo'"); + assertThrows(() => new URL("_://foo"), TypeError, "Invalid URL: '_://foo'"); + assertThrows(() => new URL("=://foo"), TypeError, "Invalid URL: '=://foo'"); + assertThrows(() => new URL("!://foo"), TypeError, "Invalid URL: '!://foo'"); + assertThrows(() => new URL(`"://foo`), TypeError, `Invalid URL: '"://foo'`); + assertThrows(() => new URL("$://foo"), TypeError, "Invalid URL: '$://foo'"); + assertThrows(() => new URL("%://foo"), TypeError, "Invalid URL: '%://foo'"); + assertThrows(() => new URL("^://foo"), TypeError, "Invalid URL: '^://foo'"); + assertThrows(() => new URL("*://foo"), TypeError, "Invalid URL: '*://foo'"); + assertThrows(() => new URL("*://foo"), TypeError, "Invalid URL: '*://foo'"); + assertThrows( + () => new URL("!:", "*://foo"), + TypeError, + "Invalid URL: '!:' with base '*://foo'", + ); }); Deno.test(function urlAuthenticationParsing() { diff --git a/ext/url/00_url.js b/ext/url/00_url.js index 5479cb59ce..ebb7b6277b 100644 --- a/ext/url/00_url.js +++ b/ext/url/00_url.js @@ -64,16 +64,19 @@ componentsBuf.buffer, ); } - return getSerialization(status, href); + return getSerialization(status, href, maybeBase); } - function getSerialization(status, href) { + function getSerialization(status, href, maybeBase) { if (status === 0) { return href; } else if (status === 1) { return core.ops.op_url_get_serialization(); } else { - throw new TypeError("Invalid URL"); + throw new TypeError( + `Invalid URL: '${href}'` + + (maybeBase ? ` with base '${maybeBase}'` : ""), + ); } }