Skip to content

Build your personal knowledge base with Trilium Notes

fix(script/jsx): critical crash if widget fails to render

Elian Doran88945788d690
diff --git a/apps/client/src/services/bundle.ts b/apps/client/src/services/bundle.ts
index 926f217bc1..9c569f0be6 100644
--- a/apps/client/src/services/bundle.ts
+++ b/apps/client/src/services/bundle.ts
@@ -5,8 +5,9 @@ import type { Entity, WidgetDefinition } from "./frontend_script_api.js";
import { t } from "./i18n.js";
import ScriptContext from "./script_context.js";
import server from "./server.js";
+import toast from "./toast.js";
import toastService, { showError } from "./toast.js";
-import utils from "./utils.js";
+import utils, { getErrorMessage } from "./utils.js";
// TODO: Deduplicate with server.
export interface Bundle {
@@ -95,11 +96,17 @@ export class WidgetsByParent {
// https://github.com/zadam/trilium/issues/4274
.map((w: any) => {
if ("type" in w && w.type === "react-widget") {
- return w.render();
+ try {
+ return w.render();
+ } catch (e: unknown) {
+ toast.showErrorTitleAndMessage(t("toast.widget-render-error.title"), getErrorMessage(e));
+ return null;
+ }
}
return (w.prototype ? new w() : w);
})
+ .filter(Boolean)
);
}
}
@@ -140,7 +147,7 @@ async function getWidgetBundlesByParent() {
} catch (e) {
toastService.showPersistent({
title: t("toast.widget-list-error.title"),
- message: e.toString(),
+ message: getErrorMessage(e),
icon: "bx bx-error-circle"
});
}
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index fac7f588c9..df6d24dd47 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -25,6 +25,9 @@
},
"widget-list-error": {
"title": "Failed to obtain the list of widgets from the server"
+ },
+ "widget-render-error": {
+ "title": "Failed to render a custom React widget"
}
},
"add_link": {