From 45692eb7a7ed0692cc4e416178e50deb1e0fb906 Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Sat, 5 Oct 2024 22:41:22 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20log=20referenced=20URL=20that=20doesn?= =?UTF-8?q?=E2=80=99t=20return=20a=20response?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/fetch-references.js | 18 +++++++++++------- test/fetch-references.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/fetch-references.js b/lib/fetch-references.js index 5f7e59c..5863ff0 100644 --- a/lib/fetch-references.js +++ b/lib/fetch-references.js @@ -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 diff --git a/test/fetch-references.js b/test/fetch-references.js index 9999d99..8054c20 100644 --- a/test/fetch-references.js +++ b/test/fetch-references.js @@ -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"; @@ -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",