When a Lua error occurs, report it and jump to source of occurance (best

effort)
This commit is contained in:
Zef Hemel
2025-10-30 15:35:34 +01:00
parent 000b76a6f7
commit c3aea2cf8c
5 changed files with 20 additions and 7 deletions
+1 -1
View File
@@ -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 () => {
+14
View File
@@ -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 });
+1 -5
View File
@@ -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) {
+3
View File
@@ -0,0 +1,3 @@
globalThis.addEventListener("unhandledrejection", (event) => {
client.reportError(event.reason);
});
+1 -1
View File
@@ -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());