Skip to content

Commit

Permalink
Merge pull request #942 from MuckRock/allanlasser/issue939
Browse files Browse the repository at this point in the history
Guard against fetch failures in `textPositions`
  • Loading branch information
allanlasser authored Dec 4, 2024
2 parents fbf37a5 + 9fae072 commit c4d6034
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 c4d6034

Please sign in to comment.