Skip to content

Commit

Permalink
Fix external link checker exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Arellano committed Oct 17, 2024
1 parent 1c0954e commit f43813b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
33 changes: 25 additions & 8 deletions scripts/js/lib/links/ExternalLink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,38 @@ test("ExternalLink constructor ignores anchors", () => {
});

test.describe("ExternalLink.check()", () => {
let originalFetch: typeof global.fetch;

test.beforeEach(() => {
originalFetch = global.fetch;
});

test.afterEach(() => {
global.fetch = originalFetch;
});

test("valid link", async () => {
let link = new ExternalLink("https://github.com/Qiskit", [
"/testorigin.mdx",
]);
global.fetch = () => Promise.resolve(new Response());
let link = new ExternalLink("https://good-link.com", ["/testorigin.mdx"]);
const result = await link.check();
expect(result).toBeUndefined();
});

test("Validate existing external links", async () => {
let link = new ExternalLink("https://ibm.com/FakePageDoesNotExist", [
"/testorigin.mdx",
]);
test("invalid link", async () => {
global.fetch = () => Promise.resolve(new Response("", { status: 404 }));
let link = new ExternalLink("https://bad-link.com", ["/testorigin.mdx"]);
const result = await link.check();
expect(result).toEqual(
"❌ Could not find link 'https://bad-link.com'. Appears in:\n /testorigin.mdx",
);
});

test("exception handling", async () => {
global.fetch = () => Promise.reject(new Error("some issue"));
let link = new ExternalLink("https://bad-link.com", ["/testorigin.mdx"]);
const result = await link.check();
expect(result).toEqual(
"❌ Could not find link 'https://ibm.com/FakePageDoesNotExist'. Appears in:\n /testorigin.mdx",
"❌ Failed to fetch 'https://bad-link.com': some issue. Appears in:\n /testorigin.mdx",
);
});
});
4 changes: 2 additions & 2 deletions scripts/js/lib/links/ExternalLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export class ExternalLink {
if (response.status >= 300) {
error = `Could not find link '${this.value}'`;
}
} catch (error) {
error = `Failed to fetch '${this.value}': ${(error as Error).message}`;
} catch (err) {
error = `Failed to fetch '${this.value}': ${(err as Error).message}`;
}

if (!error) {
Expand Down

0 comments on commit f43813b

Please sign in to comment.