lua-users home
lua-l archive

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


I guess I will have to try that. I can add instructions to my libs saying "If your scripts throw an error, they will be wrapped up in a table with the following params"...

It'll be a bit messy, though. If two libraries follow this convention the errors the user will receive will look like this:

  The following error happened: 
  {
    error = {
      error = 'the original error',
      traceback = 'the trace my user cares about'
    },
    traceback =  '<trace by library 1, the users usually does not care about>'
  }
  traceback: <trace by library 2, which the user does not care about either'>
  
With an error(msg, traceback) or an equivalent thing, I would be able to send the user the trace he cares about without all this wrapping and unwrapping.

Regards,

Enrique (@otikik)



On Wed, Sep 25, 2013 at 5:42 PM, Philipp Janda <siffiejoe@gmx.net> wrote:
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 ?