lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

	Hi Sam

	Maybe this works for you (I've skipped part of your original code,
but it stays the same; note the modification in opts table!):

We want to handle some errors, but for syntax and type errors we would
like to be able to "pass them" up to the lua
standalone, preserving the backtrace, which often shows exactly what happened.
-- shows how bug locations are masked by during error catch and rethrow

opts = {raw=1, pcall=2, xpcall=3} -- I've changed this line

kind = assert(opts[arg[1]], "arg1 is missing")

-- example utility function, this might be called by many locations in the code,
-- when it dies, but bug is in how it is called, not here
function double(_)
   return 2 * _

-- this function has a bug, it is attempting to double a table
function some_function()
   local _ = double{}
   return _


Using xpcall, notice that the error message is a bit different of the
"raw" call, but contains the same information:

traceable.lua:10: attempt to perform arithmetic on local '_' (a table value)
stack traceback:
        traceable.lua:10: in function 'double'
        traceable.lua:15: in function <traceable.lua:14>
        [C]: in function 'xpcall'
        traceable.lua:71: in main chunk
        [C]: ?


if kind == opts.xpcall then
    local ok, err_or_result = xpcall(some_function, debug.traceback)
    if not ok then
        print (err_or_result)