lua-users home
lua-l archive

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


Am 25.09.2013 15:38 schröbte Enrique Garcia Cota:
Hello Philip,

Hi!


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.

Ah, I assumed, because you proposed an extra argument to `error`. Do you have control over how the errors are caught? If so, maybe you can use the modifications below ...


[...]

Regards,

Enrique

Philipp



On Wed, Sep 25, 2013 at 12:26 PM, Philipp Janda <siffiejoe@gmx.net> wrote:

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

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

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

     b()


Output is (using the Lua 5.2 default interpreter):
    lua: ERR.LUA:20: ARGH!!!!
    stack traceback:
    	err.lua:20: in function <err.lua:19>
    	[C]: in function 'xpcall'
    	err.lua:24: in function 'b'
    	err.lua:30: in main chunk
    	[C]: in ?
    stack traceback:
    	err.lua:26: in function 'b'
    	err.lua:30: in main chunk
    	[C]: in ?