lua-users home
lua-l archive

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

Hello Philip,

Thanks for answering. I am afraid your example assumes I have control over the how the errors are thrown in the first place - that is not the case.

Unfortunately this is not possible in the two scenarios I'm interested in: Sandboxes and testing libraries. The "hosted code", that is, the sandboxed code in the case of the sandbox and the tests in the case of a testing library, is out of bounds. I am not writing it, and it's probable that it doesn't even exist at the moment. I have no control over what kind of errors it throws: it could be a table, an integer, a string, or a userdata.

In both the testing library and in the sandbox I need to do some "cleanup" after every piece of "hosted" code is run, regardless of whether it threw an error or not.

This is where I have the issue. With the current Lua implementation, as far as I know, I am left with 2 options:

* I either wrap the error in a table and append the traceback as a field on it, but this changes the error semantics (the initial error was a userdata, but the raised error is a table) or
* I raise the same error again, but then the traceback information is lost (the traceback data that appears is the one of my sandbox / testing lib, not the one where the error originally happened).

Hence my suggestion of adding a 'traceback' param to error.

I hope this explains my situation a bit better.



On Wed, Sep 25, 2013 at 12:26 PM, Philipp Janda <> wrote:
Am 25.09.2013 10:18 schröbte Enrique Garcia Cota:
Hi everyone,


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 )
    local function err_traceback( e )
      e[ 2 ] = e[ 2 ] or debug.traceback( nil, 3 )
      return e

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

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


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 ?