Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log referenced URL that doesn’t return a response #31

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading