lua-users home
lua-l archive

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


Am 25.09.2013 10:18 schröbte Enrique Garcia Cota:
Hi everyone,

Hi!


While all this conversation about the C-part is really interesting, my
original concern was using plain Lua.

Without a way of specifying/altering the traceback when raising an error,
the "error recuperation + cleanup + bubbling up" strategy means either
losing the traceback information or altering the error significantly (i.e.
changing it to a table, and adding a traceback field inside it)

I'm sure this problem can be solved by using C, but I'd be much more
interested in finding a plain Lua solution, if possible.

I don't really know what you are trying to do, but -- reacting to some keywords above (traceback, table, plain Lua) -- how about the following:

    local err_meta = {
      __tostring = function( e ) return table.concat( e, "\n" ) end,
    }
    local function make_error( s )
      return setmetatable( { s }, err_meta )
    end
    local function err_traceback( e )
      e[ 2 ] = e[ 2 ] or debug.traceback( nil, 3 )
      return e
    end

    local function a()
      error( make_error( "argh" ) )
    end

    local function b()
      local ok, err = xpcall( a, err_traceback )
      err[ 1 ] = string.upper( err[ 1 ] ) .. "!!!!"
      error( err )
    end

    b()


Output is (using the Lua 5.2 default interpreter):
    lua: ARGH!!!!
    stack traceback:
    	err.lua:14: in function <err.lua:13>
    	[C]: in function 'xpcall'
    	err.lua:18: in function 'b'
    	err.lua:23: in main chunk
    	[C]: in ?


Philipp