Improved error messages

This commit is contained in:
kepler155c@gmail.com 2020-05-12 21:25:37 -06:00
parent 2629f2a172
commit 90ce2bb1a5
4 changed files with 53 additions and 35 deletions

View File

@ -52,8 +52,8 @@ local function run(...)
loadFn = loadfile loadFn = loadfile
end end
local fn, err = loadFn(path, env) local bill, err = loadFn(path, env)
if not fn then if not bill then
error(err, -1) error(err, -1)
end end
@ -68,7 +68,7 @@ local function run(...)
} }
env[ "arg" ] = { [0] = path, table.unpack(args) } env[ "arg" ] = { [0] = path, table.unpack(args) }
local r = { fn(table.unpack(args)) } local r = { bill(table.unpack(args)) }
tProgramStack[#tProgramStack] = nil tProgramStack[#tProgramStack] = nil

View File

@ -106,17 +106,34 @@ function multishell.openTab(env, tab)
end end
tab.title = tab.title or 'untitled' tab.title = tab.title or 'untitled'
tab.window = tab.window or window.create(parentTerm, 1, 2, w, h - 1, false) tab.window = tab.window or window.create(parentTerm, 1, 2, w, h - 1, false)
tab.onExit = tab.onExit or function(self, result, err) tab.onExit = tab.onExit or function(self, result, err, stack)
if not result and err and err ~= 'Terminated' or (err and err ~= 0) then if not result and err and err ~= 'Terminated' then
self.terminal.setBackgroundColor(colors.black) self.terminal.setTextColor(colors.white)
self.terminal.setCursorBlink(false)
print('\nThe program terminated with an error.\n')
if tonumber(err) then if tonumber(err) then
self.terminal.setTextColor(colors.orange) printError('Process exited with error code: ' .. err)
print('Process exited with error code: ' .. err)
elseif err then elseif err then
printError(tostring(err)) printError(tostring(err))
end end
self.terminal.setTextColor(colors.white) if type(stack) == 'table' and #stack > 0 then
print('\nPress enter to close') local _, cy = self.terminal.getCursorPos()
local _, th = self.terminal.getSize()
self.terminal.setTextColor(colors.white)
if cy < th - 4 then
print('\nstack traceback:')
for _, v in ipairs(stack or { }) do
_, cy = self.terminal.getCursorPos()
if cy > th - 3 then
print(' ...')
break
end
print(v)
end
end
end
self.terminal.setTextColor(parentTerm.isColor() and colors.yellow or colors.white)
_G.write('\nPress enter to close')
self.isDead = true self.isDead = true
self.hidden = false self.hidden = false
redrawMenu() redrawMenu()

View File

@ -110,7 +110,7 @@ end
function Routine:run() function Routine:run()
self.co = self.co or coroutine.create(function() self.co = self.co or coroutine.create(function()
local result, err, fn local result, err, fn, stack
if self.fn then if self.fn then
fn = self.fn fn = self.fn
@ -122,12 +122,12 @@ function Routine:run()
end end
if fn then if fn then
result, err = trace(fn, table.unpack(self.args or { } )) result, err, stack = trace(fn, table.unpack(self.args or { } ))
else else
err = err or 'kernel: invalid routine' err = err or 'kernel: invalid routine'
end end
pcall(self.onExit, self, result, err) pcall(self.onExit, self, result, err, stack)
self:cleanup() self:cleanup()
if not result then if not result then

View File

@ -32,15 +32,28 @@ local function traceback(x)
end end
end end
local function trim_traceback(target) local function trim_traceback(stack)
local t = { } local trace = { }
local filters = { local filters = {
"%[C%]: in function 'xpcall'", "%[C%]: in function 'xpcall'",
"(...tail calls...)", "(...tail calls...)",
"xpcall: $", "xpcall: $",
"trace.lua:%d+:", "trace.lua:%d+:",
"stack traceback:",
} }
for line in stack:gmatch("([^\n]*)\n?") do table.insert(trace, line) end
local err = { }
while true do
local line = table.remove(trace, 1)
if not line or line == 'stack traceback:' then
break
end
table.insert(err, line)
end
err = table.concat(err, '\n')
local function matchesFilter(line) local function matchesFilter(line)
for _, filter in pairs(filters) do for _, filter in pairs(filters) do
if line:match(filter) then if line:match(filter) then
@ -49,13 +62,15 @@ local function trim_traceback(target)
end end
end end
for line in target:gmatch("([^\n]*)\n?") do local t = { }
for _, line in pairs(trace) do
if not matchesFilter(line) then if not matchesFilter(line) then
line = line:gsub("in function", "in")
table.insert(t, line) table.insert(t, line)
end end
end end
return t return err, t
end end
return function (fn, ...) return function (fn, ...)
@ -66,29 +81,15 @@ return function (fn, ...)
return fn(table.unpack(args)) return fn(table.unpack(args))
end, traceback)) end, traceback))
local ok, err = res[1], res[2] if not res[1] and res[2] ~= nil then
local err, trace = trim_traceback(res[2])
if not ok and err ~= nil then
local trace = trim_traceback(err)
err = { }
while true do
local line = table.remove(trace, 1)
if not line or line == 'stack traceback:' then
break
end
table.insert(err, line)
end
err = table.concat(err, '\n')
_G._syslog('\n' .. err .. '\n' .. 'stack traceback:') _G._syslog('\n' .. err .. '\n' .. 'stack traceback:')
for _, v in ipairs(trace) do for _, v in ipairs(trace) do
if v ~= 'stack traceback:' then _G._syslog(v)
_G._syslog(v:gsub("in function", "in"))
end
end end
return ok, err return res[1], err, trace
end end
return table.unpack(res, 1, res.n) return table.unpack(res, 1, res.n)