lua-users home
lua-l archive

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


Am 23.10.2014 um 12:10 schröbte Michele Alessandrini:
`xpcall` and `lua_pcall[k]` can take a message handler function which
can replace or extend the original error message in any way. The message
handler receives the old error message as argument and returns the new
error message/value.


See the implementation of `luaL_traceback` in `lauxlib.c` for pointers.


Sorry, but this does not address the problem. All these methods receive
strings that are already formatted to be human-readable.
For example you get:
   "[string "print("hello world")..."]:70: attempt to perform arithmetic
on global 'x' (a nil value)"

What would be useful is a struct with data similar to these (invented by
me):

   error = LUA_ARITHMETIC_INVALID_OPERAND
   file =  "....."
   line =  70

file and line you can get as in `luaL_traceback` (or `luaL_where`), but the rest is only available as a string (although there have been proposals to change that, e.g. [1], [2]). This applies to the errors the Lua runtime throws, your own error messages can be tables with separate fields ...

   object = LUA_GLOBAL_VARIABLE
   objectname = "x"
   objecttype = LUA_TNIL

This way the program could manage an error in more sophisticated ways,
or format an error string in whatever is more appropriate for the
application. With strings you should do an heavy parsing, and know all
the possible generated strings, and hope they don't change at every
revision.

Bye
Michele


Philipp

  [1]: http://lua-users.org/lists/lua-l/2005-02/msg00033.html
  [2]: http://lua-users.org/lists/lua-l/2013-09/msg00808.html