Skip to content

Commit

Permalink
Add more description to the error
Browse files Browse the repository at this point in the history
  • Loading branch information
BBB committed Nov 21, 2023
1 parent 30b2b3c commit f4ce02f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/http-client/src/ImmutableURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,19 @@ export class ImmutableURL implements Readonly<URL> {
constructor(url: URL);
constructor(url: string, base?: string | URL);
constructor(url: string | URL, base?: string | URL) {
this.#url = typeof url === "object" ? url : new URL(url, base);
this.#searchParams = new ImmutableURLSearchParams(this.#url.searchParams);
try {
this.#url = typeof url === "object" ? url : new URL(url, base);
this.#searchParams = new ImmutableURLSearchParams(this.#url.searchParams);
} catch (err) {
if (err instanceof TypeError && err.message == "Invalid URL") {
throw new TypeError(`Invalid URL: ${url}`);
}
throw err;
}
}

static fromPathname(pathname: string) {
return new ImmutableURL(pathname, "http://example.com/");
}

copy(
Expand Down
3 changes: 3 additions & 0 deletions packages/http-client/test/ImmutableURL.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { expect, it } from "vitest";
import { ImmutableURL } from "~/src/ImmutableURL";

it("throws an error on an invalid string", () => {
expect(() => new ImmutableURL("/api")).toThrow(`Invalid URL: /api`);
});
it("can copy the fields", () => {
const underTest = new ImmutableURL("https://example.com/api");
const changed = underTest.copy({ username: "user", password: "pass" });
Expand Down

0 comments on commit f4ce02f

Please sign in to comment.