From 9fae0723361ceac34fb9ae1a4a7c50680da65567 Mon Sep 17 00:00:00 2001 From: Allan Lasser Date: Wed, 4 Dec 2024 15:50:42 -0500 Subject: [PATCH] Guard against fetch failures in `textPositions` Fixes #939 --- src/lib/api/documents.ts | 10 ++++++---- src/lib/api/tests/documents.test.ts | 3 +++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/lib/api/documents.ts b/src/lib/api/documents.ts index 1f43d78ca..c9aed190e 100644 --- a/src/lib/api/documents.ts +++ b/src/lib/api/documents.ts @@ -209,12 +209,14 @@ export async function textPositions( } } - const resp = await fetch(url).catch(console.error); - if (!resp || isErrorCode(resp.status)) { + try { + const resp = await fetch(url); + if (!resp || isErrorCode(resp.status)) return []; + return resp.json(); + } catch (e) { + console.error(e); return []; } - - return resp.json(); } /** diff --git a/src/lib/api/tests/documents.test.ts b/src/lib/api/tests/documents.test.ts index a8c1b0919..1cab3b657 100644 --- a/src/lib/api/tests/documents.test.ts +++ b/src/lib/api/tests/documents.test.ts @@ -246,6 +246,9 @@ describe("document fetching", () => { const t = await documents.textPositions(document, 1, mockFetch); expect(t).toMatchObject(textPositions); + + mockFetch.mockRejectedValue(new Error("Failed to fetch")); + expect(await documents.textPositions(document, 1, mockFetch)).toEqual([]); }); });