Skip to content

Commit c5b38fa

Browse files
committed
onError fix
1 parent 0bb17df commit c5b38fa

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

src/elements/Program.lua

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Program.defineProperty(Program, "program", {default = nil, type = "table"})
1515
Program.defineProperty(Program, "path", {default = "", type = "string"})
1616
--- @property running boolean false Whether the program is running
1717
Program.defineProperty(Program, "running", {default = false, type = "boolean"})
18+
--- @property errorCallback function nil The error callback function
19+
Program.defineProperty(Program, "errorCallback", {default = nil, type = "function"})
1820

1921
Program.defineEvent(Program, "*")
2022

@@ -47,13 +49,6 @@ function BasaltProgram:run(path, width, height)
4749
local file = fs.open(pPath, "r")
4850
local content = file.readAll()
4951
file.close()
50-
--[[local env = setmetatable(self.env, {__index=_ENV})
51-
env.shell = shell
52-
env.term = self.window
53-
env.require, env.package = newPackage(env, fs.getDir(pPath))
54-
env.term.current = term.current
55-
env.term.redirect = term.redirect
56-
env.term.native = term.native]]
5752

5853
local env = setmetatable(createShellEnv(fs.getDir(path)), { __index = _ENV })
5954
env.term = self.window
@@ -69,7 +64,7 @@ function BasaltProgram:run(path, width, height)
6964
env = self.env
7065
end
7166

72-
67+
7368
self.coroutine = coroutine.create(function()
7469
local program = load(content, "@/" .. path, nil, env)
7570
if program then
@@ -82,11 +77,13 @@ function BasaltProgram:run(path, width, height)
8277
local ok, result = coroutine.resume(self.coroutine)
8378
term.redirect(current)
8479
if not ok then
85-
if self.onError then
86-
local result = self.onError(self.program, result)
87-
if(result==false)then
80+
local errorCallback = self.program.get("errorCallback")
81+
if errorCallback then
82+
local trace = debug.traceback(self.coroutine, result)
83+
local _result = errorCallback(self.program, result, trace:gsub(result, ""))
84+
if(_result==false)then
8885
self.filter = nil
89-
return
86+
return ok, result
9087
end
9188
end
9289
errorManager.header = "Basalt Program Error ".. path
@@ -122,9 +119,11 @@ function BasaltProgram:resume(event, ...)
122119
if ok then
123120
self.filter = result
124121
else
125-
if self.onError then
126-
local result = self.onError(self.program, result)
127-
if(result==false)then
122+
local errorCallback = self.program.get("errorCallback")
123+
if errorCallback then
124+
local trace = debug.traceback(self.coroutine, result)
125+
local _result = errorCallback(self.program, result, trace:gsub(result, ""))
126+
if(_result==false)then
128127
self.filter = nil
129128
return ok, result
130129
end
@@ -196,10 +195,7 @@ end
196195
--- @param fn function The callback function to register
197196
--- @return Program self The Program instance
198197
function Program:onError(fn)
199-
local program = self.get("program")
200-
if program then
201-
program.onError = fn
202-
end
198+
self.set("errorCallback", fn)
203199
return self
204200
end
205201

0 commit comments

Comments
 (0)