From c3aea2cf8c47c8efc7124cfa32599fa7a6511fa0 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Thu, 30 Oct 2025 15:35:34 +0100 Subject: [PATCH] When a Lua error occurs, report it and jump to source of occurance (best effort) --- client/boot.ts | 2 +- client/client.ts | 14 ++++++++++++++ client/codemirror/editor_state.ts | 6 +----- client/debug.ts | 3 +++ client/editor_ui.tsx | 2 +- 5 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 client/debug.ts diff --git a/client/boot.ts b/client/boot.ts index ef06e049..ad1963ea 100644 --- a/client/boot.ts +++ b/client/boot.ts @@ -14,7 +14,7 @@ import "./lib/polyfills.ts"; import type { BootConfig, ServiceWorkerTargetMessage } from "./types/ui.ts"; import { BoxProxy } from "./lib/box_proxy.ts"; import { importKey } from "@silverbulletmd/silverbullet/lib/crypto"; - +import "./debug.ts"; const logger = initLogger("[Client]"); safeRun(async () => { diff --git a/client/client.ts b/client/client.ts index 0bff5189..5fae7b90 100644 --- a/client/client.ts +++ b/client/client.ts @@ -81,6 +81,8 @@ import { Augmenter } from "./data/data_augmenter.ts"; import { EncryptedKvPrimitives } from "./data/encrypted_kv_primitives.ts"; import type { KvPrimitives } from "./data/kv_primitives.ts"; import { deriveDbName } from "@silverbulletmd/silverbullet/lib/crypto"; +import { LuaRuntimeError } from "./space_lua/runtime.ts"; +import { resolveASTReference } from "./space_lua.ts"; const frontMatterRegex = /^---\n(([^\n]|\n)*?)---\n/; @@ -566,6 +568,18 @@ export class Client { ); } + reportError(e: any, context: string = "") { + console.error(`Error during ${context}:`, e); + + if (e instanceof LuaRuntimeError) { + client.flashNotification(`Lua error: ${e.message}`, "error"); + const origin = resolveASTReference(e.sf.astCtx!); + if (origin) { + client.navigate(origin); + } + } + } + startPageNavigate(mode: "page" | "meta" | "document" | "all") { // Then show the page navigator this.ui.viewDispatch({ type: "start-navigate", mode }); diff --git a/client/codemirror/editor_state.ts b/client/codemirror/editor_state.ts index fe22e476..aa6e8fa9 100644 --- a/client/codemirror/editor_state.ts +++ b/client/codemirror/editor_state.ts @@ -341,11 +341,7 @@ export function createCommandKeyBindings(client: Client): KeyBinding[] { Promise.resolve([]) .then(def.run) .catch((e: any) => { - console.error(e); - client.flashNotification( - `Error running command: ${e.message}`, - "error", - ); + client.reportError(e, "key"); }).then((returnValue: any) => { // Always be focusing the editor after running a command if (returnValue !== false) { diff --git a/client/debug.ts b/client/debug.ts new file mode 100644 index 00000000..1739cc97 --- /dev/null +++ b/client/debug.ts @@ -0,0 +1,3 @@ +globalThis.addEventListener("unhandledrejection", (event) => { + client.reportError(event.reason); +}); diff --git a/client/editor_ui.tsx b/client/editor_ui.tsx index 700fecdc..69f32430 100644 --- a/client/editor_ui.tsx +++ b/client/editor_ui.tsx @@ -224,7 +224,7 @@ export class MainUI { client.focus(); } } catch (e: any) { - console.error("Error running command", e.message); + this.client.reportError(e, "Command invocation"); } } else { setTimeout(() => client.focus());