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
feat(render): intercept bundle errors
Elian Dorana5806c0d1d8f
diff --git a/apps/client/src/services/bundle.ts b/apps/client/src/services/bundle.tsindex d33ba76a0a..7cee01812b 100644--- a/apps/client/src/services/bundle.ts+++ b/apps/client/src/services/bundle.ts@@ -2,7 +2,6 @@ import { h, VNode } from "preact";import BasicWidget, { ReactWrappedWidget } from "../widgets/basic_widget.js";import RightPanelWidget from "../widgets/right_panel_widget.js";-import froca from "./froca.js";import type { Entity } from "./frontend_script_api.js";import { WidgetDefinitionWithType } from "./frontend_script_api_preact.js";import { t } from "./i18n.js";@@ -38,15 +37,18 @@ async function getAndExecuteBundle(noteId: string, originEntity = null, script =export type ParentName = "left-pane" | "center-pane" | "note-detail-pane" | "right-pane";-export async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $container?: JQuery<HTMLElement>) {+export async function executeBundleWithoutErrorHandling(bundle: Bundle, originEntity?: Entity | null, $container?: JQuery<HTMLElement>) {const apiContext = await ScriptContext(bundle.noteId, bundle.allNoteIds, originEntity, $container);+ return await function () {+ return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`);+ }.call(apiContext);+}+export async function executeBundle(bundle: Bundle, originEntity?: Entity | null, $container?: JQuery<HTMLElement>) {try {- return await function () {- return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`);- }.call(apiContext);- } catch (e: any) {- showErrorForScriptNote(bundle.noteId, t("toast.bundle-error.message", { message: e.message }));+ return await executeBundleWithoutErrorHandling(bundle, originEntity, $container);+ } catch (e: unknown) {+ showErrorForScriptNote(bundle.noteId, t("toast.bundle-error.message", { message: getErrorMessage(e) }));logError("Widget initialization failed: ", e);}}diff --git a/apps/client/src/services/render.ts b/apps/client/src/services/render.tsindex ece40306f7..fb92722a68 100644--- a/apps/client/src/services/render.ts+++ b/apps/client/src/services/render.ts@@ -2,7 +2,7 @@ import { h, VNode } from "preact";import type FNote from "../entities/fnote.js";import { renderReactWidgetAtElement } from "../widgets/react/react_utils.jsx";-import bundleService, { type Bundle } from "./bundle.js";+import { type Bundle, executeBundleWithoutErrorHandling } from "./bundle.js";import froca from "./froca.js";import server from "./server.js";@@ -22,7 +22,7 @@ async function render(note: FNote, $el: JQuery<HTMLElement>, onError?: (e: unkno$scriptContainer.append(bundle.html);// async so that scripts cannot block trilium execution- bundleService.executeBundle(bundle, note, $scriptContainer)+ executeBundleWithoutErrorHandling(bundle, note, $scriptContainer).catch(onError).then(result => {// Render JSXdiff --git a/apps/client/src/types.d.ts b/apps/client/src/types.d.tsindex 2e2a36e6ee..f7673901c1 100644--- a/apps/client/src/types.d.ts+++ b/apps/client/src/types.d.ts@@ -119,7 +119,7 @@ declare global {setNote(noteId: string);}- var logError: (message: string, e?: Error | string) => void;+ var logError: (message: string, e?: unknown) => void;var logInfo: (message: string) => void;var glob: CustomGlobals;//@ts-ignore