onError fix

This commit is contained in:
Robert Jelic
2025-04-26 13:32:08 +02:00
parent 0bb17df1b9
commit c5b38fae0f

View File

@@ -15,6 +15,8 @@ Program.defineProperty(Program, "program", {default = nil, type = "table"})
Program.defineProperty(Program, "path", {default = "", type = "string"}) Program.defineProperty(Program, "path", {default = "", type = "string"})
--- @property running boolean false Whether the program is running --- @property running boolean false Whether the program is running
Program.defineProperty(Program, "running", {default = false, type = "boolean"}) Program.defineProperty(Program, "running", {default = false, type = "boolean"})
--- @property errorCallback function nil The error callback function
Program.defineProperty(Program, "errorCallback", {default = nil, type = "function"})
Program.defineEvent(Program, "*") Program.defineEvent(Program, "*")
@@ -47,13 +49,6 @@ function BasaltProgram:run(path, width, height)
local file = fs.open(pPath, "r") local file = fs.open(pPath, "r")
local content = file.readAll() local content = file.readAll()
file.close() file.close()
--[[local env = setmetatable(self.env, {__index=_ENV})
env.shell = shell
env.term = self.window
env.require, env.package = newPackage(env, fs.getDir(pPath))
env.term.current = term.current
env.term.redirect = term.redirect
env.term.native = term.native]]
local env = setmetatable(createShellEnv(fs.getDir(path)), { __index = _ENV }) local env = setmetatable(createShellEnv(fs.getDir(path)), { __index = _ENV })
env.term = self.window env.term = self.window
@@ -69,7 +64,7 @@ function BasaltProgram:run(path, width, height)
env = self.env env = self.env
end end
self.coroutine = coroutine.create(function() self.coroutine = coroutine.create(function()
local program = load(content, "@/" .. path, nil, env) local program = load(content, "@/" .. path, nil, env)
if program then if program then
@@ -82,11 +77,13 @@ function BasaltProgram:run(path, width, height)
local ok, result = coroutine.resume(self.coroutine) local ok, result = coroutine.resume(self.coroutine)
term.redirect(current) term.redirect(current)
if not ok then if not ok then
if self.onError then local errorCallback = self.program.get("errorCallback")
local result = self.onError(self.program, result) if errorCallback then
if(result==false)then local trace = debug.traceback(self.coroutine, result)
local _result = errorCallback(self.program, result, trace:gsub(result, ""))
if(_result==false)then
self.filter = nil self.filter = nil
return return ok, result
end end
end end
errorManager.header = "Basalt Program Error ".. path errorManager.header = "Basalt Program Error ".. path
@@ -122,9 +119,11 @@ function BasaltProgram:resume(event, ...)
if ok then if ok then
self.filter = result self.filter = result
else else
if self.onError then local errorCallback = self.program.get("errorCallback")
local result = self.onError(self.program, result) if errorCallback then
if(result==false)then local trace = debug.traceback(self.coroutine, result)
local _result = errorCallback(self.program, result, trace:gsub(result, ""))
if(_result==false)then
self.filter = nil self.filter = nil
return ok, result return ok, result
end end
@@ -196,10 +195,7 @@ end
--- @param fn function The callback function to register --- @param fn function The callback function to register
--- @return Program self The Program instance --- @return Program self The Program instance
function Program:onError(fn) function Program:onError(fn)
local program = self.get("program") self.set("errorCallback", fn)
if program then
program.onError = fn
end
return self return self
end end