Skip to content

Commit

Permalink
feat: log referenced URL that doesn’t return a response
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Oct 5, 2024
1 parent 910a722 commit 45692eb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
18 changes: 11 additions & 7 deletions lib/fetch-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ export const fetchReferences = async (jf2) => {
const references = jf2.references || {};

for await (const url of urls) {
const mf2 = await fetchMf2(url);
const properties = mf2tojf2(mf2);

references[url] = {
url,
...properties,
};
try {
const mf2 = await fetchMf2(url);
const properties = mf2tojf2(mf2);

references[url] = {
url,
...properties,
};
} catch (error) {
console.error(`Unable to fetch reference for ${url} (${error.message})`);
}
}

// Only add `references` property if references found
Expand Down
30 changes: 29 additions & 1 deletion test/fetch-references.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { strict as assert } from "node:assert";
import { describe, it } from "node:test";
import { describe, it, mock } from "node:test";
import { setGlobalDispatcher } from "undici";
import { fetchReferences } from "../lib/fetch-references.js";
import { mockClient } from "../helpers/mock-agent.js";
Expand Down Expand Up @@ -45,6 +45,34 @@ describe("mf2tojf2", () => {
);
});

it("Logs referenced URL that didn’t return a response", async () => {
mock.method(console, "error", () => {});

const expected = await fetchReferences({
type: "entry",
name: "What my friend ate for lunch yesterday",
published: "2019-02-12T10:00:00.000+00:00",
url: "https://website.example/bookmarks/lunch",
"bookmark-of": "https://another.example/404.html",
});

assert.equal(
console.error.mock.calls[0].arguments[0],
`Unable to fetch reference for https://another.example/404.html (Not Found)`,
);

assert.deepEqual(
{
type: "entry",
name: "What my friend ate for lunch yesterday",
published: "2019-02-12T10:00:00.000+00:00",
url: "https://website.example/bookmarks/lunch",
"bookmark-of": "https://another.example/404.html",
},
expected,
);
});

it("Uses metaformats fallback for each referenced URL", async () => {
const expected = await fetchReferences({
type: "entry",
Expand Down

0 comments on commit 45692eb

Please sign in to comment.