Skip to content

Build your personal knowledge base with Trilium Notes

fix(scripting): don't show "Note executed" when a frontend script fails

Elian Doran604f41859394
diff --git a/apps/client/src/components/entrypoints.ts b/apps/client/src/components/entrypoints.ts
index 249bd95e4d..8db24eb5c6 100644
--- a/apps/client/src/components/entrypoints.ts
+++ b/apps/client/src/components/entrypoints.ts
@@ -174,7 +174,13 @@ export default class Entrypoints extends Component {
// TODO: use note.executeScript()
if (note.mime.endsWith("env=frontend")) {
- await bundleService.getAndExecuteBundle(note.noteId);
+ try {
+ // rethrow so a failed script doesn't fall through to the "Note executed" confirmation;
+ // executeBundle has already surfaced the error toast.
+ await bundleService.getAndExecuteBundle(note.noteId, null, null, null, { rethrow: true });
+ } catch {
+ return;
+ }
} else if (note.mime.endsWith("env=backend")) {
await server.post(`script/run/${note.noteId}`);
} else if (note.mime === "text/x-sqlite;schema=trilium") {
diff --git a/apps/client/src/services/bundle.spec.ts b/apps/client/src/services/bundle.spec.ts
index a23840ccbb..7c7c627ddd 100644
--- a/apps/client/src/services/bundle.spec.ts
+++ b/apps/client/src/services/bundle.spec.ts
@@ -58,6 +58,22 @@ describe("executeBundle / executeBundleWithoutErrorHandling", () => {
expect((window as any).logError).toHaveBeenCalled();
spy.mockRestore();
});
+
+ it("executeBundle rethrows after reporting when opts.rethrow is set", async () => {
+ const id = buildNote({ title: "Throwing note" }).noteId;
+ const spy = vi.spyOn(toast, "showErrorForScriptNote").mockReturnValue(undefined);
+ const bundle: Bundle = {
+ script: `throw new Error("kaboom");`,
+ html: "",
+ noteId: id,
+ allNoteIds: [id]
+ };
+ // The error is still reported (toast + log), but rethrown so the caller can react.
+ await expect(executeBundle(bundle, null, undefined, { rethrow: true })).rejects.toThrow("kaboom");
+ expect(spy).toHaveBeenCalled();
+ expect((window as any).logError).toHaveBeenCalled();
+ spy.mockRestore();
+ });
});
describe("getAndExecuteBundle (default export)", () => {
diff --git a/apps/client/src/services/bundle.ts b/apps/client/src/services/bundle.ts
index 23400fe2c4..f4787e3149 100644
--- a/apps/client/src/services/bundle.ts
+++ b/apps/client/src/services/bundle.ts
@@ -28,13 +28,13 @@ type WithNoteId<T> = T & {
};
export type Widget = WithNoteId<(LegacyWidget | WidgetDefinitionWithType)>;
-async function getAndExecuteBundle(noteId: string, originEntity: FNote | null = null, script: string | null = null, params: ScriptParams | null = null) {
+async function getAndExecuteBundle(noteId: string, originEntity: FNote | null = null, script: string | null = null, params: ScriptParams | null = null, opts?: ExecuteBundleOpts) {
const bundle = await server.post<Bundle>(`script/bundle/${noteId}`, {
script,
params
});
- return await executeBundle(bundle, originEntity);
+ return await executeBundle(bundle, originEntity, undefined, opts);
}
export type ParentName = WidgetDefinitionWithType["parent"];
@@ -46,12 +46,24 @@ export async function executeBundleWithoutErrorHandling(bundle: Bundle, originEn
}.call(apiContext);
}
-export async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $container?: JQuery<HTMLElement>) {
+export interface ExecuteBundleOpts {
+ /**
+ * When `true`, the caught error is re-thrown after being reported, so the caller can tell the
+ * bundle failed (e.g. to avoid claiming success). Defaults to `false` — errors are swallowed,
+ * which is what widget/startup callers rely on.
+ */
+ rethrow?: boolean;
+}
+
+export async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $container?: JQuery<HTMLElement>, opts?: ExecuteBundleOpts) {
try {
return await executeBundleWithoutErrorHandling(bundle, originEntity, $container);
} catch (e: unknown) {
showErrorForScriptNote(bundle.noteId, rootCauseMessage(e), { monospace: true });
logError("Widget initialization failed: ", e);
+ if (opts?.rethrow) {
+ throw e;
+ }
}
}