Skip to content

Build your personal knowledge base with Trilium Notes

fix(client/collections): handle a failed page load in usePagination

Claudecd05e12d3dde
diff --git a/apps/client/src/widgets/collections/Pagination.spec.tsx b/apps/client/src/widgets/collections/Pagination.spec.tsx
index 044629409f..6bd3b36b47 100644
--- a/apps/client/src/widgets/collections/Pagination.spec.tsx
+++ b/apps/client/src/widgets/collections/Pagination.spec.tsx
@@ -166,4 +166,23 @@ describe("usePagination", () => {
getNotesSpy.mockRestore();
}
});
+
+ it("reports a failed note load instead of leaving the rejection unhandled", async () => {
+ const getNotesSpy = vi.spyOn(froca, "getNotes")
+ .mockRejectedValue(new Error("tree/load failed"));
+ const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
+ try {
+ const note = buildNote({ title: "Search", type: "search" });
+ await mount(note, 10);
+ await settle();
+
+ // An unhandled rejection here fails the whole Vitest run, not just this test.
+ expect(consoleSpy).toHaveBeenCalled();
+ expect(observed?.pageNotes).toBeUndefined();
+ expect(observed?.page).toBe(1);
+ } finally {
+ consoleSpy.mockRestore();
+ getNotesSpy.mockRestore();
+ }
+ });
});
diff --git a/apps/client/src/widgets/collections/Pagination.tsx b/apps/client/src/widgets/collections/Pagination.tsx
index 42a8edd813..ec7fff7c61 100644
--- a/apps/client/src/widgets/collections/Pagination.tsx
+++ b/apps/client/src/widgets/collections/Pagination.tsx
@@ -173,11 +173,15 @@ export function usePagination(
// Overlapping loads (rapid page flips, a shrinking result set) can resolve out of order;
// the cleanup flag makes sure a superseded load can no longer overwrite the current page.
let cancelled = false;
- froca.getNotes(pageNoteIds).then((notes) => {
- if (!cancelled) {
- setPageNotes(notes);
- }
- });
+ froca.getNotes(pageNoteIds)
+ .then((notes) => {
+ if (!cancelled) {
+ setPageNotes(notes);
+ }
+ })
+ // froca.getNotes() rejects when its `tree/load` request fails. Report it and leave
+ // `pageNotes` as it stands, so the rejection reaches a handler.
+ .catch((e) => console.error(`Loading the notes of page ${effectivePage} failed`, e));
return () => {
cancelled = true;
};