Skip to content

Build your personal knowledge base with Trilium Notes

feat(scripting): report a failed script run against the note that failed

Elian Doran371b7dbb4568
diff --git a/apps/client/src/components/entrypoints.ts b/apps/client/src/components/entrypoints.ts
index b5cfeee1ca..4aa6a9bb2e 100644
--- a/apps/client/src/components/entrypoints.ts
+++ b/apps/client/src/components/entrypoints.ts
@@ -1,6 +1,6 @@
import { CreateChildrenResponse, SqlExecuteResponse } from "@triliumnext/commons";
-import { showBackendScriptingDisabledToast } from "../services/backend_scripting.js";
+import { runBackendScript, showBackendScriptingDisabledToast } from "../services/backend_scripting.js";
import bundleService from "../services/bundle.js";
import dialog from "../services/dialog.js";
import dateNoteService from "../services/date_notes.js";
@@ -200,9 +200,9 @@ export default class Entrypoints extends Component {
return;
}
try {
- await server.post(`script/run/${note.noteId}`);
+ await runBackendScript(note.noteId);
} catch {
- // server.js already reported the error; don't fall through to "Note executed".
+ // Already reported as a scripting error; don't fall through to "Note executed".
return;
}
} else if (note.mime === "text/x-sqlite;schema=trilium") {
diff --git a/apps/client/src/entities/fnote.spec.ts b/apps/client/src/entities/fnote.spec.ts
index 2ddf841444..521bbf3a84 100644
--- a/apps/client/src/entities/fnote.spec.ts
+++ b/apps/client/src/entities/fnote.spec.ts
@@ -1179,7 +1179,10 @@ describe("FNote executeScript", () => {
});
it("posts to the server for a backend script", async () => {
- const postSpy = vi.spyOn(server, "post").mockResolvedValue(undefined);
+ // Runs through backend_scripting, so a failure is reported against the note rather than as
+ // a request that went wrong — which is why the silent variant is the one called.
+ const postSpy = vi.spyOn(server, "postWithSilentInternalServerError")
+ .mockResolvedValue(undefined);
const note = makeNote({ type: "code", mime: "application/javascript;env=backend" });
await note.executeScript();
diff --git a/apps/client/src/entities/fnote.ts b/apps/client/src/entities/fnote.ts
index 1d447d5fa2..eab1b26f83 100644
--- a/apps/client/src/entities/fnote.ts
+++ b/apps/client/src/entities/fnote.ts
@@ -1,5 +1,6 @@
import { getNoteIcon, HighlightedTokenInfo } from "@triliumnext/commons";
+import { runBackendScript } from "../services/backend_scripting.js";
import bundleService from "../services/bundle.js";
import cssClassManager from "../services/css_class_manager.js";
import type { Froca } from "../services/froca-interface.js";
@@ -1132,7 +1133,7 @@ export default class FNote {
if (env === "frontend") {
return await bundleService.getAndExecuteBundle(this.noteId);
} else if (env === "backend") {
- await server.post(`script/run/${this.noteId}`);
+ await runBackendScript(this.noteId);
} else {
throw new Error(`Unrecognized env type ${env} for note ${this.noteId}`);
}
diff --git a/apps/client/src/services/backend_scripting.spec.ts b/apps/client/src/services/backend_scripting.spec.ts
new file mode 100644
index 0000000000..17a925294e
--- /dev/null
+++ b/apps/client/src/services/backend_scripting.spec.ts
@@ -0,0 +1,77 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ post: vi.fn<(url: string) => Promise<unknown>>(),
+ // app_context is pulled in at module scope and asks for the keyboard actions as it loads, so
+ // replacing the server module has to answer that too.
+ get: vi.fn<(url: string) => Promise<unknown>>(
+ async (url) => (url === "keyboard-actions" ? [] : {})),
+ showErrorForScriptNote: vi.fn()
+}));
+
+vi.mock("./server.js", () => ({
+ default: { postWithSilentInternalServerError: mocks.post, get: mocks.get }
+}));
+vi.mock("./toast.js", () => ({
+ default: { showPersistent: vi.fn(), showMessage: vi.fn() },
+ showErrorForScriptNote: mocks.showErrorForScriptNote
+}));
+
+import { readScriptFailure, runBackendScript } from "./backend_scripting.js";
+
+/** How the server's answer reaches the client: the response body, not an Error. */
+const responseBody = (failure: { message: string; noteId?: string }) => JSON.stringify(failure);
+
+describe("readScriptFailure", () => {
+ it("takes the parts the server answered with", () => {
+ expect(readScriptFailure(responseBody({ message: "boom", noteId: "child1" })))
+ .toEqual({ message: "boom", noteId: "child1" });
+ });
+
+ it("answers without a note where the server named none", () => {
+ expect(readScriptFailure(responseBody({ message: "boom" }))).toEqual({ message: "boom" });
+ });
+
+ it("falls back to what it was given", () => {
+ expect(readScriptFailure("not json at all")).toEqual({ message: "not json at all" });
+ expect(readScriptFailure(new Error("thrown"))).toEqual({ message: "thrown" });
+ expect(readScriptFailure(undefined)).toEqual({ message: "undefined" });
+ });
+});
+
+describe("runBackendScript", () => {
+ beforeEach(() => {
+ mocks.post.mockReset();
+ mocks.showErrorForScriptNote.mockReset();
+ });
+
+ it("says nothing when the script runs", async () => {
+ mocks.post.mockResolvedValue(undefined);
+
+ await runBackendScript("note1");
+
+ expect(mocks.post).toHaveBeenCalledWith("script/run/note1");
+ expect(mocks.showErrorForScriptNote).not.toHaveBeenCalled();
+ });
+
+ it("reports a failure against the note, and still lets the caller know", async () => {
+ mocks.post.mockRejectedValue(responseBody({
+ message: "Module 'axioss' could not be loaded.",
+ noteId: "Qp58WenrLLwg"
+ }));
+
+ await expect(runBackendScript("Qp58WenrLLwg")).rejects.toBeDefined();
+
+ expect(mocks.showErrorForScriptNote).toHaveBeenCalledWith(
+ "Qp58WenrLLwg", "Module 'axioss' could not be loaded.", { monospace: true });
+ });
+
+ it("points at the child module note that failed, not the one that was run", async () => {
+ mocks.post.mockRejectedValue(responseBody({ message: "boom", noteId: "childModule" }));
+
+ await expect(runBackendScript("parentScript")).rejects.toBeDefined();
+
+ expect(mocks.showErrorForScriptNote).toHaveBeenCalledWith(
+ "childModule", "boom", { monospace: true });
+ });
+});
diff --git a/apps/client/src/services/backend_scripting.ts b/apps/client/src/services/backend_scripting.ts
index 400e4f0164..6b481eb9af 100644
--- a/apps/client/src/services/backend_scripting.ts
+++ b/apps/client/src/services/backend_scripting.ts
@@ -1,6 +1,9 @@
+import type { ScriptFailure } from "@triliumnext/commons";
+
import appContext from "../components/app_context.js";
import { t } from "./i18n.js";
-import toastService from "./toast.js";
+import server from "./server.js";
+import toastService, { showErrorForScriptNote } from "./toast.js";
import { openInAppHelpFromUrl } from "./utils.js";
/**
@@ -59,3 +62,44 @@ export function showBackendScriptingDisabledToast(noteId: string) {
]
});
}
+
+/**
+ * Runs a backend script note, reporting a failure as a scripting error rather than as a request
+ * that went wrong.
+ *
+ * The generic report names a status code and a URL, which say where the failure was noticed rather
+ * than what a script author did — and the toast raised here shows the note itself, so it can be
+ * opened from the failure. Rethrows, so a caller can still tell the run did not happen.
+ */
+export async function runBackendScript(noteId: string): Promise<void> {
+ try {
+ await server.postWithSilentInternalServerError(`script/run/${noteId}`);
+ } catch (e) {
+ const failure = readScriptFailure(e);
+ // The note the server named is the one that failed, which is a child module note where one
+ // of those did; the note asked for is the fallback where it named none.
+ showErrorForScriptNote(failure.noteId ?? noteId, failure.message, { monospace: true });
+ throw e;
+ }
+}
+
+/** What the server said failed, as it answers it — parts rather than one sentence to unpick. */
+export function readScriptFailure(e: unknown): ScriptFailure {
+ if (typeof e !== "string") {
+ return { message: e instanceof Error ? e.message : String(e) };
+ }
+
+ try {
+ const parsed = JSON.parse(e);
+ if (parsed && typeof parsed.message === "string") {
+ return {
+ message: parsed.message,
+ ...(typeof parsed.noteId === "string" ? { noteId: parsed.noteId } : {})
+ };
+ }
+ } catch {
+ // Not JSON, so the body is the best the server said.
+ }
+
+ return { message: e };
+}
diff --git a/apps/client/src/test/setup.ts b/apps/client/src/test/setup.ts
index feb0193325..894b11d1ea 100644
--- a/apps/client/src/test/setup.ts
+++ b/apps/client/src/test/setup.ts
@@ -97,6 +97,12 @@ function mockServer() {
console.warn(`Unsupported GET to mocked server: ${url}`);
}
+ async function post(url: string, data: object) {
+ if (url === "tree/load") {
+ throw new Error(`A module tried to load from the server the following notes: ${((data as any).noteIds || []).join(",")}\nThis is not supported, use Froca mocking instead and ensure the note exist in the mock.`);
+ }
+ }
+
return {
default: {
get,
@@ -105,11 +111,12 @@ function mockServer() {
// in how it reports 404s, which the mock never produces, so share the same routing.
getWithSilentNotFound: get,
- async post(url: string, data: object) {
- if (url === "tree/load") {
- throw new Error(`A module tried to load from the server the following notes: ${((data as any).noteIds || []).join(",")}\nThis is not supported, use Froca mocking instead and ensure the note exist in the mock.`);
- }
- },
+ // A backend script run goes through this variant so its failure is reported against the
+ // note rather than as a request that went wrong; it differs from `post` only in how it
+ // reports a 500, which the mock never produces.
+ postWithSilentInternalServerError: (url: string, data: object) => post(url, data),
+
+ post,
// Widgets that persist as the user edits (attribute writes, view configs) reach for
// these; without them the write rejects and surfaces as an unhandled rejection rather
diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts
index 71c1cd69a5..535b234dae 100644
--- a/packages/commons/src/lib/server_api.ts
+++ b/packages/commons/src/lib/server_api.ts
@@ -1172,3 +1172,11 @@ export interface NetworkAddressesResponse {
}
export type ScriptParams = any[];
+
+/** A script failure, as the parts a caller wants rather than as one sentence to unpick. */
+export interface ScriptFailure {
+ /** What went wrong, unwrapped from the per-note wrappers the bundle puts round it. */
+ message: string;
+ /** The note that failed, which is a child module note where one of those did. */
+ noteId?: string;
+}
diff --git a/packages/trilium-core/src/routes/api/script.ts b/packages/trilium-core/src/routes/api/script.ts
index 997742fba8..3a85d7112a 100644
--- a/packages/trilium-core/src/routes/api/script.ts
+++ b/packages/trilium-core/src/routes/api/script.ts
@@ -5,7 +5,7 @@ import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import attributeService from "../../services/attributes.js";
import { getPlatform } from "../../services/platform.js";
-import scriptService, { type Bundle } from "../../services/script.js";
+import scriptService, { type Bundle, describeScriptFailure } from "../../services/script.js";
import syncService from "../../services/sync.js";
import { assertScriptingEnabled } from "../../services/scripting_guard.js";
import { getSql } from "../../services/sql/index.js";
@@ -51,9 +51,14 @@ function run(req: Request<{ noteId: string }>) {
assertScriptingEnabled();
const note = becca.getNoteOrThrow(req.params.noteId);
- const result = scriptService.executeNote(note, { originEntity: note });
-
- return { executionResult: result };
+ try {
+ return { executionResult: scriptService.executeNote(note, { originEntity: note }) };
+ } catch (e) {
+ // Answered in parts rather than as one sentence: the cause chain does not survive the
+ // response, and what the caller wants out of it — what failed, and which note — would
+ // otherwise have to be read back out of the text on the other side.
+ return [ 500, describeScriptFailure(e) ];
+ }
}
function getBundlesWithLabel(label: string, value?: string) {
diff --git a/packages/trilium-core/src/services/script.spec.ts b/packages/trilium-core/src/services/script.spec.ts
index 170d63a4b6..ca587df0ba 100644
--- a/packages/trilium-core/src/services/script.spec.ts
+++ b/packages/trilium-core/src/services/script.spec.ts
@@ -5,7 +5,7 @@ import becca from "../becca/becca.js";
import { buildNote } from "../test/becca_easy_mocking.js";
import config from "./config.js";
import { getContext } from "./context.js";
-import scriptService, { buildJsx, executeBundle, getScriptBundle } from "./script.js";
+import scriptService, { buildJsx, describeScriptFailure, executeBundle, getScriptBundle } from "./script.js";
import ws from "./ws.js";
vi.mock("./sql.js", () => {
@@ -434,3 +434,43 @@ describe("executeNoteNoException", () => {
});
});
});
+
+describe("describeScriptFailure", () => {
+ /** A failure as the bundle wraps one: the note it stands for, and what it wrapped. */
+ const wrapped = (noteId: string, message: string, cause: unknown) =>
+ Object.assign(new Error(message, { cause }), { scriptNoteId: noteId });
+
+ it("answers the failure itself and the note nearest it", () => {
+ const failure = new Error("Module 'axioss' could not be loaded.", { cause: new Error("Cannot find module") });
+ const outer = wrapped("parent", "Load of script note \"Parent\" (parent) failed with: ...",
+ wrapped("child", "Load of script note \"Child\" (child) failed with: ...", failure));
+
+ expect(describeScriptFailure(outer)).toEqual({
+ message: "Module 'axioss' could not be loaded.",
+ noteId: "child"
+ });
+ });
+
+ it("stops at the failure rather than at what the failure wrapped", () => {
+ // Node answers a missing module with a require stack of Trilium's own files; the message
+ // above it was written to keep that out of the way, so the walk must not go past it.
+ const nodeError = new Error("Cannot find module 'x'\nRequire stack:\n- becca.ts");
+ const failure = new Error("Module 'x' could not be loaded.", { cause: nodeError });
+
+ expect(describeScriptFailure(wrapped("n1", "wrapped", failure)).message)
+ .toBe("Module 'x' could not be loaded.");
+ });
+
+ it("answers a failure that was never wrapped, without a note", () => {
+ expect(describeScriptFailure(new Error("scripting is disabled")))
+ .toEqual({ message: "scripting is disabled" });
+ expect(describeScriptFailure("just a string")).toEqual({ message: "just a string" });
+ });
+
+ it("survives a cycle in the chain", () => {
+ const looped = wrapped("n1", "outer", undefined);
+ looped.cause = looped;
+
+ expect(describeScriptFailure(looped)).toEqual({ message: "outer", noteId: "n1" });
+ });
+});
diff --git a/packages/trilium-core/src/services/script.ts b/packages/trilium-core/src/services/script.ts
index 5421209bd6..aad1b325d9 100644
--- a/packages/trilium-core/src/services/script.ts
+++ b/packages/trilium-core/src/services/script.ts
@@ -1,4 +1,4 @@
-import { ScriptParams } from "@triliumnext/commons";
+import { type ScriptFailure, ScriptParams } from "@triliumnext/commons";
import { t } from "i18next";
import { transform } from "sucrase";
@@ -247,7 +247,7 @@ apiContext.modules['${note.noteId}'] = { exports: {} };
${root ? "return " : ""}${isFrontend ? "await" : ""} ((${isFrontend ? "async" : ""} function(exports, module, require, api${modules.length > 0 ? ", " : ""}${modules.map((child) => sanitizeVariableName(child.title)).join(", ")}) {
try {
${overrideContent || scriptContent};
-} catch (e) { throw new Error("Load of script note \\"${note.title}\\" (${note.noteId}) failed with: " + e.message, { cause: e }); }
+} catch (e) { throw Object.assign(new Error("Load of script note \\"${note.title}\\" (${note.noteId}) failed with: " + e.message, { cause: e }), { scriptNoteId: "${note.noteId}" }); }
for (const exportKey in exports) module.exports[exportKey] = exports[exportKey];
return module.exports;
}).call({}, {}, apiContext.modules['${note.noteId}'], apiContext.require(${JSON.stringify(moduleNoteIds)}), apiContext.apis['${note.noteId}']${modules.length > 0 ? ", " : ""}${modules.map((mod) => `apiContext.modules['${mod.noteId}'].exports`).join(", ")}));
@@ -259,6 +259,39 @@ return module.exports;
return bundle;
}
+/**
+ * Reads a script failure into its parts.
+ *
+ * The bundle wraps a failure once per note it passed through, and each wrapper carries the note it
+ * stands for — so the note that actually failed and the error underneath it are both here, and
+ * neither has to be read back out of the sentence afterwards. The wrappers stay in the message for
+ * the log, which is the one place the whole path is worth having.
+ */
+export function describeScriptFailure(e: unknown): ScriptFailure {
+ let message = typeof e === "string" ? e : "Unknown error";
+ let noteId: string | undefined;
+
+ const seen = new Set<unknown>();
+ let error: unknown = e;
+ while (error instanceof Error && !seen.has(error)) {
+ seen.add(error);
+ message = error.message;
+
+ // Only the wrappers are walked past. Going further reaches what the failure itself wrapped,
+ // such as Node's require stack of Trilium's own files for a missing module.
+ const wrapped = (error as { scriptNoteId?: string }).scriptNoteId;
+ if (!wrapped) {
+ break;
+ }
+
+ // The deepest wrapper is the note nearest the failure, so a later one wins.
+ noteId = wrapped;
+ error = error.cause;
+ }
+
+ return { message, ...(noteId ? { noteId } : {}) };
+}
+
export function buildJsx(contentRaw: string | Uint8Array) {
const content = unwrapStringOrBuffer(contentRaw);
const output = transform(content, {