Skip to content

Commit

Permalink
Guard against fetch failures in textPositions
Browse files Browse the repository at this point in the history
Fixes #939
  • Loading branch information
allanlasser committed Dec 4, 2024
1 parent ba7cba2 commit 9fae072
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/lib/api/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/lib/api/tests/documents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);
});
});

Expand Down

0 comments on commit 9fae072

Please sign in to comment.