Improve Lua error reporting

pull/1232/head
Zef Hemel 2025-02-06 08:34:23 +01:00
parent 2ea3d8fe4f
commit 4bd3370f71
1 changed files with 7 additions and 5 deletions

View File

@ -3,6 +3,7 @@ import { parsePageRef } from "@silverbulletmd/silverbullet/lib/page_ref";
import { import {
LuaEnv, LuaEnv,
LuaNativeJSFunction, LuaNativeJSFunction,
type LuaRuntimeError,
LuaStackFrame, LuaStackFrame,
LuaTable, LuaTable,
} from "$common/space_lua/runtime.ts"; } from "$common/space_lua/runtime.ts";
@ -61,15 +62,15 @@ export async function buildThreadLocalEnv(
return Promise.resolve(tl); return Promise.resolve(tl);
} }
export async function handleLuaError(e: any, system: System<any>) { export async function handleLuaError(e: LuaRuntimeError, system: System<any>) {
console.error( console.error(
"Lua eval exception", "Lua eval exception",
e.message, e.message,
e.context, e.sf.astCtx,
); );
if (e.context && e.context.ref) { if (e.sf.astCtx && e.sf.astCtx.ref) {
// We got an error and actually know where it came from, let's navigate there to help debugging // We got an error and actually know where it came from, let's navigate there to help debugging
const pageRef = parsePageRef(e.context.ref); const pageRef = parsePageRef(e.sf.astCtx.ref);
await system.localSyscall( await system.localSyscall(
"editor.flashNotification", "editor.flashNotification",
[ [
@ -87,7 +88,8 @@ export async function handleLuaError(e: any, system: System<any>) {
await system.localSyscall("editor.navigate", [ await system.localSyscall("editor.navigate", [
{ {
page: pageRef.page, page: pageRef.page,
pos: pageRef.pos + e.context.from + pos: (typeof pageRef.pos === "number" ? pageRef.pos : 0) +
(e.sf.astCtx?.from ?? 0) +
"```space-lua\n".length, "```space-lua\n".length,
}, },
]); ]);