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([]); }); });