Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(scripting): report a script run's 500 that is not Trilium's own
Claudebeb707708b52
diff --git a/apps/client/src/services/backend_scripting.spec.ts b/apps/client/src/services/backend_scripting.spec.tsindex 3d024ad8c0..78ad002119 100644--- a/apps/client/src/services/backend_scripting.spec.ts+++ b/apps/client/src/services/backend_scripting.spec.ts@@ -81,4 +81,14 @@ describe("runBackendScript", () => {expect(mocks.showErrorForScriptNote).not.toHaveBeenCalled();});++ // A 500 is silenced for this request, so a body that is not Trilium's own reaches no other toast.+ it("reports a failure that is not Trilium's against the note that was run", async () => {+ mocks.post.mockRejectedValue("<html>Internal Server Error</html>");++ await expect(runBackendScript("note1")).rejects.toBeDefined();++ expect(mocks.showErrorForScriptNote).toHaveBeenCalledWith(+ "note1", "<html>Internal Server Error</html>", { monospace: true });+ });});diff --git a/apps/client/src/services/backend_scripting.ts b/apps/client/src/services/backend_scripting.tsindex 2f430a604f..4c0375e11a 100644--- a/apps/client/src/services/backend_scripting.ts+++ b/apps/client/src/services/backend_scripting.ts@@ -74,10 +74,13 @@ export async function runBackendScript(noteId: string): Promise<void> {try {await server.postWithSilentInternalServerError(`script/run/${noteId}`);} catch (e) {- // Anything other than the route's own script failure was already reported by `server.ts`.const failure = readScriptFailure(e);if (failure) {showErrorForScriptNote(failure.noteId, failure.message, { monospace: true });+ } else if (typeof e === "string" && e.trim() && !isTriliumError(e)) {+ // Trilium's own `{ message }` errors other than a 500 were reported by `server.ts`. Any+ // other body, such as a proxy's 500 page, is reported here, since a 500 is silenced.+ showErrorForScriptNote(noteId, e, { monospace: true });}throw e;}@@ -107,3 +110,14 @@ export function readScriptFailure(e: unknown): Required<ScriptFailure> | undefinreturn undefined;}++/** Whether a response body is the `{ message }` JSON that Trilium's routes answer an error with. */+function isTriliumError(body: string): boolean {+ try {+ const parsed: unknown = JSON.parse(body);+ return !!parsed && typeof parsed === "object"+ && "message" in parsed && typeof parsed.message === "string";+ } catch {+ return false;+ }+}diff --git a/packages/trilium-core/src/routes/api/script.spec.ts b/packages/trilium-core/src/routes/api/script.spec.tsindex a0fe6dc71e..0583f87e9b 100644--- a/packages/trilium-core/src/routes/api/script.spec.ts+++ b/packages/trilium-core/src/routes/api/script.spec.ts@@ -1,5 +1,6 @@import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";+import becca from "../../becca/becca";import attributeService from "../../services/attributes";import config from "../../services/config";import { getLog } from "../../services/log";@@ -123,7 +124,9 @@ describe("Script API (core)", () => {const noteId = await createCodeNote("return 0;");const before = getSql().getValue<string>("SELECT title FROM notes WHERE noteId = ?", [noteId]);vi.spyOn(scriptService, "executeNote").mockImplementation(() => {- getSql().execute("UPDATE notes SET title = 'written by the script' WHERE noteId = ?", [noteId]);+ const note = becca.getNoteOrThrow(noteId);+ note.title = "written by the script";+ note.save();throw new Error("boom");});const logError = vi.spyOn(getLog(), "error");