lua-users home
lua-l archive

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


Hello,

I would like to use 'xpcall' in conjunction with 'debug.traceback' to produce a nice stack trace in case something goes wrong :)

Additionally, I would like to pass some vararg to 'xpcall' as well.

My first attempt looked like this:

function try( ... )
        local aFunction = function()
                tryToDoIt( ... )
        end
        local someResults = { xpcall( aFunction, debug.traceback ) }
        local aStatus = someResults[ 1 ]

        if aStatus == true then
                table.remove( someResults, 1 )

                return unpack( someResults )
        end

        return nil, someResults[ 2 ]
end

Unfortunately, accessing vararg from a function which doesn't directly declare it seems to be frown upon:

lua: TestMeta.lua:298: cannot use '...' outside a vararg function near '...'

The second try looks like the following, with some additional vararg packing/unpacking:

function try( ... )
        local someArguments = { ... }
        local aFunction = function()
                tryToDoIt( unpack( someArguments ) )
        end
        local someResults = { xpcall( aFunction, debug.traceback ) }
        local aStatus = someResults[ 1 ]

        if aStatus == true then
                table.remove( someResults, 1 )

                return unpack( someResults )
        end

        return nil, someResults[ 2 ]
end

This compile fine and produce a nice stack trace as well :)

However, any nil argument get lost in translation during the packing/unpacking of the vararg :/

Is there a way out of this conundrum beside using 'pcall' (and minus the valuable stack trace info)?

For the record, I'm using Lua 5.1 (final).

TIA.

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/