1
0
Fork 0
mirror of https://github.com/Reuh/candran.git synced 2025-10-27 09:59:29 +00:00

Improve error handling

This commit is contained in:
Étienne Fildadut 2021-05-17 13:59:37 +02:00
parent d68c6fab0d
commit 81f5f8fbbb
3 changed files with 296 additions and 257 deletions

View file

@ -15,8 +15,9 @@
local unpack = unpack or table.unpack
local candran = {
VERSION = "0.13.0"
VERSION = "0.14.0"
}
package.loaded["candran"] = candran
--- Default options.
candran.default = {
@ -221,7 +222,8 @@ function candran.load(chunk, chunkname, env, options={})
end
codeCache[options.chunkname] = code
local f, err = util.load(code, options.chunkname, env)
local f
f, err = util.load(code, "=%s(%s)":format(options.chunkname, "compiled candran"), env)
-- Um. Candran isn't supposed to generate invalid Lua code, so this is a major issue.
-- This is not going to raise an error because this is supposed to behave similarly to Lua's load function.
@ -234,10 +236,9 @@ function candran.load(chunk, chunkname, env, options={})
return f
else
return function(...)
local params = {...}
if not errorRewritingActive then
errorRewritingActive = true
local t = { xpcall(() return f(unpack(params)) end, candran.messageHandler) }
local t = { xpcall(f, candran.messageHandler, ...) }
errorRewritingActive = false
if t[1] == false then
error(t[2], 0)
@ -265,19 +266,21 @@ end
--- Candran error message handler.
-- Use it in xpcall to rewrite stacktraces to display Candran source file lines instead of compiled Lua lines.
function candran.messageHandler(message)
return debug.traceback(message, 2):gsub("(\n?%s*)([^\n]-)%:(%d+)%:", function(indentation, source, line)
if not message:match("\nstack traceback:\n") then
message = debug.traceback(message, 2)
end
return message:gsub("(\n?%s*)([^\n]-)%:(%d+)%:", function(indentation, source, line)
line = tonumber(line)
local originalFile
local strName = source:match("%[string \"(.-)\"%]")
local strName = source:match("^(.-)%(compiled candran%)$")
if strName then
if codeCache[strName] then
originalFile = codeCache[strName]
source = strName
end
else
local fi = io.open(source, "r")
if fi then
if fi = io.open(source, "r") then
originalFile = fi:read("*a")
fi:close()
end
@ -313,21 +316,31 @@ function candran.searcher(modpath)
return "\n\tno candran file in package.path"
end
end
local r, s = candran.loadfile(filepath)
if r then
return r
else
return s
end
return ()
local r, s = candran.loadfile(filepath)
if r then
return r()
else
error("error loading candran module '%s' from file '%s':\n\t%s":format(modpath, filepath, s), 0)
end
end, filepath
end
--- Register the Candran package searcher.
function candran.setup()
if _VERSION == "Lua 5.1" then
table.insert(package.loaders, 2, candran.searcher)
local searchers = if _VERSION == "Lua 5.1" then
package.loaders
else
table.insert(package.searchers, 2, candran.searcher)
package.searchers
end
-- check if already setup
for _, s in ipairs(searchers) do
if s == candran.searcher then
return candran
end
end
-- setup
table.insert(searchers, 2, candran.searcher)
return candran
end