Skip to content

Build your personal knowledge base with Trilium Notes

feat(render): surface the root cause in the render error card

Elian Dorancfbf0066669e
diff --git a/apps/client/src/services/bundle.ts b/apps/client/src/services/bundle.ts
index f4787e3149..133ceb4ee7 100644
--- a/apps/client/src/services/bundle.ts
+++ b/apps/client/src/services/bundle.ts
@@ -10,7 +10,7 @@ import { t } from "./i18n.js";
import ScriptContext from "./script_context.js";
import server from "./server.js";
import toastService, { showErrorForScriptNote } from "./toast.js";
-import utils, { getErrorMessage } from "./utils.js";
+import utils, { getErrorMessage, rootCauseMessage } from "./utils.js";
// TODO: Deduplicate with server.
export interface Bundle {
@@ -181,19 +181,3 @@ export default {
executeStartupBundles,
getWidgetBundlesByParent
};
-
-/**
- * The script bundler wraps each script note's thrown errors as
- * `Load of script note "<title>" (<noteId>) failed with: <inner>` and attaches the original error
- * as the `cause` (see the bundle template in trilium-core's `script.ts`). That prefix is useful in
- * backend logs but redundant in the UI, where the failing note is already shown as a reference link,
- * so we surface the underlying error instead — walking the `cause` chain to the bottom also unwraps
- * the nested errors produced when a `require()`d module fails.
- */
-function rootCauseMessage(e: unknown): string {
- let error = e;
- while (error instanceof Error && error.cause !== undefined) {
- error = error.cause;
- }
- return error instanceof Error ? error.message : String(error);
-}
diff --git a/apps/client/src/services/utils.spec.ts b/apps/client/src/services/utils.spec.ts
index 1caa707827..0d80b0df7f 100644
--- a/apps/client/src/services/utils.spec.ts
+++ b/apps/client/src/services/utils.spec.ts
@@ -28,6 +28,7 @@ import utils, {
reloadFrontendApp,
replaceHtmlEscapedSlashes,
restartDesktopApp,
+ rootCauseMessage,
toggleBodyClass
} from "./utils.js";
@@ -801,6 +802,26 @@ describe("getErrorMessage", () => {
});
});
+describe("rootCauseMessage", () => {
+ it("walks the cause chain to the bottom and returns the root message", () => {
+ const root = new Error("boom");
+ const middle = new Error(`Load of script note "B" (id2) failed with: boom`, { cause: root });
+ const outer = new Error(`Load of script note "A" (id1) failed with: ...`, { cause: middle });
+ expect(rootCauseMessage(outer)).toBe("boom");
+ });
+
+ it("returns the message of an unwrapped error or error-like object as-is", () => {
+ expect(rootCauseMessage(new Error("plain"))).toBe("plain");
+ expect(rootCauseMessage({ message: "server-side" })).toBe("server-side");
+ });
+
+ it("passes strings through and stringifies other primitives", () => {
+ expect(rootCauseMessage("raw string")).toBe("raw string");
+ expect(rootCauseMessage(42)).toBe("42");
+ expect(rootCauseMessage(new Error("outer", { cause: "string cause" }))).toBe("string cause");
+ });
+});
+
describe("handleRightToLeftPlacement", () => {
it("returns the placement unchanged in LTR mode", () => {
const original = window.glob.isRtl;
diff --git a/apps/client/src/services/utils.ts b/apps/client/src/services/utils.ts
index 12b93dea45..31318a4dc7 100644
--- a/apps/client/src/services/utils.ts
+++ b/apps/client/src/services/utils.ts
@@ -847,6 +847,28 @@ export function getErrorMessage(e: unknown) {
}
+/**
+ * The script bundler wraps each script note's thrown errors as
+ * `Load of script note "<title>" (<noteId>) failed with: <inner>` and attaches the original error
+ * as the `cause` (see the bundle template in trilium-core's `script.ts`). That prefix is useful in
+ * backend logs but redundant in the UI, where the failing note is already shown as a reference link,
+ * so we surface the underlying error instead — walking the `cause` chain to the bottom also unwraps
+ * the nested errors produced when a `require()`d module fails.
+ */
+export function rootCauseMessage(e: unknown): string {
+ let error = e;
+ while (error instanceof Error && error.cause !== undefined) {
+ error = error.cause;
+ }
+ if (typeof error === "string") {
+ return error;
+ }
+ if (error && typeof error === "object" && "message" in error && typeof error.message === "string") {
+ return error.message;
+ }
+ return String(error);
+}
+
export function replaceHtmlEscapedSlashes(str: string) {
return str.replace(/&#x2F;/g, "/");
}
diff --git a/apps/client/src/widgets/react/RenderErrorCard.spec.tsx b/apps/client/src/widgets/react/RenderErrorCard.spec.tsx
index b89390f90f..d6209076a3 100644
--- a/apps/client/src/widgets/react/RenderErrorCard.spec.tsx
+++ b/apps/client/src/widgets/react/RenderErrorCard.spec.tsx
@@ -32,6 +32,16 @@ describe("RenderErrorCard", () => {
expect(container.querySelector(".render-error-message")?.textContent).toBe("kaput");
});
+ it("unwraps the bundler's wrapper via the cause chain and shows only the root cause", () => {
+ const wrapped = new Error(
+ `Load of script note "My script" (abc123) failed with: boom`,
+ { cause: new Error("boom") }
+ );
+ const container = mount(<RenderErrorCard error={wrapped} noteId="abc123" />);
+
+ expect(container.querySelector(".render-error-message")?.textContent).toBe("boom");
+ });
+
it("links to the failing script note when a noteId is given", () => {
const container = mount(<RenderErrorCard error="boom" noteId="abc123" />);
diff --git a/apps/client/src/widgets/react/RenderErrorCard.tsx b/apps/client/src/widgets/react/RenderErrorCard.tsx
index 84ef521361..7fe45b2fd9 100644
--- a/apps/client/src/widgets/react/RenderErrorCard.tsx
+++ b/apps/client/src/widgets/react/RenderErrorCard.tsx
@@ -1,7 +1,7 @@
import "./RenderErrorCard.css";
import { t } from "../../services/i18n";
-import { getErrorMessage } from "../../services/utils";
+import { rootCauseMessage } from "../../services/utils";
import { ExtendedAdmonition } from "./Admonition";
import NoteLink from "./NoteLink";
@@ -9,11 +9,14 @@ import NoteLink from "./NoteLink";
* The error card shown when a render/script note fails to run. Shared by the render-note
* type widget and the (jQuery-based) content renderer so both surfaces look identical.
*
+ * The underlying error is surfaced via {@link rootCauseMessage}, so the bundler's
+ * `Load of script note ... failed with:` wrapper never reaches the card — the failing
+ * note is already identified by the reference link below the message.
+ *
* @param noteId the script note that failed, linked below the message so the user can jump to it.
*/
export default function RenderErrorCard({ error, noteId }: { error: unknown; noteId?: string }) {
- const message = typeof error === "string" ? error : getErrorMessage(error);
- const { summary, details } = splitRenderError(message);
+ const { summary, details } = splitRenderError(rootCauseMessage(error));
return (
<ExtendedAdmonition