Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): keep the ribbon's query error when a search request fails

Elian Doran61674171c0fb
diff --git a/apps/client/src/services/search.spec.ts b/apps/client/src/services/search.spec.ts
index 9088cab008..584bf4a497 100644
--- a/apps/client/src/services/search.spec.ts
+++ b/apps/client/src/services/search.spec.ts
@@ -62,15 +62,19 @@ describe("search service", () => {
const loadSearchNote = vi.spyOn(froca, "loadSearchNote");
loadSearchNote.mockResolvedValueOnce({ error: "Bad query" });
- expect(await searchService.runSearchNote(component, "search1", "ntx1")).toBe("Bad query");
+ expect(await searchService.runSearchNote(component, "search1", "ntx1")).toEqual({ error: "Bad query" });
expect(loadSearchNote).toHaveBeenCalledWith("search1");
+
+ loadSearchNote.mockResolvedValueOnce(undefined);
+ expect(await searchService.runSearchNote(component, "search1", "ntx1")).toEqual({ error: undefined });
expect(showError).not.toHaveBeenCalled();
+ // A failed request says nothing about the query, so it returns no result to replace its error.
loadSearchNote.mockRejectedValueOnce(new Error("Network down"));
expect(await searchService.runSearchNote(component, "search1", "ntx1")).toBeUndefined();
expect(showError).toHaveBeenCalledWith("Network down");
- expect(triggerEvent).toHaveBeenCalledTimes(2);
+ expect(triggerEvent).toHaveBeenCalledTimes(3);
expect(triggerEvent).toHaveBeenCalledWith("searchRefreshed", { ntxId: "ntx1" });
});
});
diff --git a/apps/client/src/services/search.ts b/apps/client/src/services/search.ts
index b3af2298a4..24cb927a92 100644
--- a/apps/client/src/services/search.ts
+++ b/apps/client/src/services/search.ts
@@ -27,19 +27,19 @@ async function searchInSubtree(searchString: string, ancestorNoteId: string) {
}
/**
- * Runs a saved search and triggers `searchRefreshed` so the tab shows its results. A failed request
- * is shown as a toast; an error in the query itself is returned for the caller to show.
+ * Runs a saved search and triggers `searchRefreshed` so the tab shows its results. Returns the
+ * query's error for the caller to show, or `undefined` after showing a failed request as a toast.
*/
async function runSearchNote(component: Component | null | undefined, noteId: string, ntxId: string | null | undefined) {
- let error: string | undefined;
+ let result: { error: string | undefined } | undefined;
try {
- error = (await froca.loadSearchNote(noteId))?.error ?? undefined;
+ result = { error: (await froca.loadSearchNote(noteId))?.error ?? undefined };
} catch (e: unknown) {
toast.showError(getErrorMessage(e));
}
component?.triggerEvent("searchRefreshed", { ntxId });
- return error;
+ return result;
}
export default {
diff --git a/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx b/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx
index 82e890e9b1..c34c149122 100644
--- a/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx
+++ b/apps/client/src/widgets/ribbon/SearchDefinitionTab.tsx
@@ -54,8 +54,10 @@ export default function SearchDefinitionTab({ note, ntxId, hidden }: Pick<TabCon
return;
}
- const error = await search.runSearchNote(parentComponent, noteId, ntxId);
- setError(error ? { message: error } : undefined);
+ const result = await search.runSearchNote(parentComponent, noteId, ntxId);
+ if (result) {
+ setError(result.error ? { message: result.error } : undefined);
+ }
}
// Refresh the list of available and active options.
diff --git a/apps/client/src/widgets/search_result.tsx b/apps/client/src/widgets/search_result.tsx
index 1b9e88987d..70d34a3841 100644
--- a/apps/client/src/widgets/search_result.tsx
+++ b/apps/client/src/widgets/search_result.tsx
@@ -37,9 +37,9 @@ export default function SearchResult() {
return;
}
- const error = await search.runSearchNote(parentComponent, note.noteId, ntxId);
- if (error) {
- toast.showError(error);
+ const result = await search.runSearchNote(parentComponent, note.noteId, ntxId);
+ if (result?.error) {
+ toast.showError(result.error);
}
}