luerl icon indicating copy to clipboard operation
luerl copied to clipboard

How to report lua errors?

Open tisba opened this issue 9 years ago • 6 comments

I'm trying to figure out, how to report errors caused by lua to the user.

Using this script (notice that foo is not defined):

print("Hello World!")
foo()

and run it via

{error, State} = luerl:evalfile("test.lua").

State will contain something about undef_function, but I'm having a hard time to build a understandable error message out of it I can present my user.

Using C-lua I get

Hello World!
lua: test.lua:2: attempt to call global 'foo' (a nil value)
stack traceback:
    test.lua:2: in main chunk
    [C]: in ?

And I would like to present a similar error message. I could not find an example on how to deal with and report errors. Is there something I'm missing?

[0] State is…

{lua_error,{undef_function,nil},
           {luerl,{array,13,100,undefined,
                         {{{table,{array,0,10,nil,10},
                                  {{{{empty,<<"_G">>,
                                            {tref,0},
                                            empty,<<"_VERSION">>,<<"Lua 5.2">>,empty},
                                     <<"assert">>,
                                     {function,#Fun<luerl_lib_basic.1.82106474>},
                                     {empty,<<"bit32">>,{tref,5},empty},
                                     <<"collectgarbage">>,
                                     {function,#Fun<luerl_lib_basic.2.82106474>},
                                     {empty,<<"debug">>,{tref,12},empty}},
                                    <<"dofile">>,
                                    {function,#Fun<luerl_lib_basic.3.82106474>},
                                    {{empty,<<"eprint">>,
                                            {function,#Fun<luerl_lib_basic.4.82106474>},
                                            empty},
                                     <<"error">>,
                                     {function,#Fun<luerl_lib_basic.0.82106474>},
                                     {empty,<<"getmetatable">>,
                                            {function,#Fun<luerl_lib_basic.5.82106474>},
                                            empty,<<...>>,...}}},
                                   <<"ipairs">>,
                                   {function,#Fun<luerl_lib_basic.6.82106474>},
                                   {{{empty,<<"load">>,
                                            {function,#Fun<luerl_lib_basic.7.82106474>},
                                            empty},
                                     <<"loadfile">>,
                                     {function,#Fun<luerl_lib_basic.8.82106474>},
                                     {empty,<<"loadstring">>,
                                            {function,#Fun<luerl_lib_basic.9.82106474>},
                                            empty,<<...>>,...}},
                                    <<"next">>,
                                    {function,#Fun<luerl_lib_basic.10.82106474>},
                                    {{empty,<<"os">>,{tref,8},empty},
                                     <<"package">>,
                                     {tref,4},
                                     {empty,<<...>>,...},
                                     <<"pcal"...>>,{...},...}},
                                   <<"rawequal">>,
                                   {function,#Fun<luerl_lib_basic.14.82106474>},
                                   {{{empty,<<"rawget">>,
                                            {function,#Fun<luerl_lib_basic.15.82106474>},
                                            empty},
                                     <<"rawlen">>,
                                     {function,#Fun<luerl_lib_basic.16.82106474>},
                                     {empty,<<...>>,...}},
                                    <<"select">>,
                                    {function,#Fun<luerl_lib_basic.18.82106474>},
                                    {{empty,<<...>>,...},<<"stri"...>>,{...},...},
                                    <<"tonumber">>,
                                    {function,...},
                                    {...}}},
                                  nil},
                           {table,{array,3,10,nil,
                                         {nil,{function,#Fun<luerl_lib_package.2.74952575>},
                                              {function,#Fun<luerl_lib_package.3.74952575>},
                                              nil,nil,nil,nil,nil,nil,...}},
                                  empty,nil},
                           {table,{array,0,10,nil,10},
                                  {{{empty,<<"_G">>,{tref,0},empty},
                                    <<"bit32">>,
                                    {tref,5},
                                    {empty,<<"debug">>,{tref,12},empty},
                                    <<"io">>,
                                    {tref,6},
                                    {empty,<<"math">>,{tref,...},empty}},
                                   <<"os">>,
                                   {tref,8},
                                   {{empty,<<"package">>,{tref,4},empty},
                                    <<"string">>,
                                    {tref,9},
                                    {empty,<<"table">>,{tref,...},empty}}},
                                  nil},
                           {table,{array,0,10,nil,10},empty,nil},
                           {table,{array,0,10,nil,10},
                                  {{empty,<<"config">>,<<"/\n;\n?\n!\n-\n">>,empty},
                                   <<"loaded">>,
                                   {tref,2},
                                   {empty,<<"path">>,<<"./?.lua;./?/"...>>,empty},
                                   <<"preload">>,
                                   {tref,3},
                                   {empty,<<"sear"...>>,{...},...}},
                                  nil},
                           {table,{array,0,10,nil,10},
                                  {{{empty,<<"arshift">>,
                                           {function,#Fun<luerl_lib_bit32.7.29927074>},
                                           empty,<<"band">>,
                                           {function,...},
                                           empty},
                                    <<"bnot">>,
                                    {function,#Fun<luerl_lib_bit32.1.29927074>},
                                    {empty,<<"bor">>,{function,...},empty}},
                                   <<"btest">>,
                                   {function,#Fun<luerl_lib_bit32.3.29927074>},
                                   {{empty,<<"bxor">>,{function,...},empty},
                                    <<"extract">>,
                                    {function,#Fun<luerl_lib_bit32.10.29927074>},
                                    {empty,...}},
                                   <<"lshift">>,
                                   {function,#Fun<luerl_lib_bit32.5.29927074>},
                                   {{empty,...},<<...>>,...}},
                                  nil},
                           {table,{array,0,10,nil,10},
                                  {empty,<<"flush">>,
                                         {function,#Fun<luerl_lib_io.0.101991831>},
                                         empty,<<"write">>,
                                         {function,#Fun<luerl_lib_io.1.101991831>},
                                         empty},
                                  nil},
                           {table,{array,0,10,nil,10},
                                  {{{{empty,<<"abs">>,{function,...},empty},
                                     <<"acos">>,
                                     {function,#Fun<luerl_lib_math.3.97665772>},
                                     {empty,...}},
                                    <<"atan">>,
                                    {function,#Fun<luerl_lib_math.5.97665772>},
                                    {{empty,...},<<...>>,...}},
                                   <<"cosh">>,
                                   {function,#Fun<luerl_lib_math.9.97665772>},
                                   {{{empty,...},<<...>>,...},<<"fmod">>,{...},...},
                                   <<"log">>,
                                   {function,...},
                                   {...}},
                                  nil},
                           {table,{array,0,10,nil,10},
                                  {{empty,<<"clock">>,
                                          {function,#Fun<luerl_lib_os.0.73040098>},
                                          empty},
                                   <<"date">>,
                                   {function,#Fun<luerl_lib_os.1.73040098>},
                                   {empty,<<...>>,...},
                                   <<"gete"...>>,{...},...},
                                  nil},
                           {table,{array,0,10,nil,10},
                                  {{{empty,<<"byte">>,{...},...},
                                    <<"char">>,
                                    {function,...},
                                    {...}},
                                   <<"find">>,
                                   {function,#Fun<luerl_lib_string.3.80821632>},
                                   {{...},...},
                                   <<...>>,...},
                                  nil}},
                          {{table,{array,0,10,nil,10},
                                  {empty,<<"__index">>,{tref,9},empty},
                                  nil},
                           {table,{array,0,10,nil,10},
                                  {{empty,<<"concat">>,
                                          {function,#Fun<luerl_lib_table.0.51275605>},
                                          empty},
                                   <<"insert">>,
                                   {function,#Fun<luerl_lib_table.1.51275605>},
                                   {empty,<<"pack">>,
                                          {function,#Fun<luerl_lib_table.2.51275605>},
                                          empty},
                                   <<"remove">>,
                                   {function,#Fun<luerl_lib_table.3.51275605>},
                                   {empty,<<"sort">>,
                                          {function,#Fun<luerl_lib_table.4.51275605>},
                                          empty,<<...>>,...}},
                                  nil},
                           {table,{array,0,10,nil,10},
                                  {{empty,<<"getmetatable">>,
                                          {function,#Fun<luerl_lib_debug.0.67778288>},
                                          empty},
                                   <<"getuservalue">>,
                                   {function,#Fun<luerl_lib_debug.1.67778288>},
                                   {empty,<<"setmetatable">>,
                                          {function,#Fun<luerl_lib_debug.2.67778288>},
                                          empty,<<"setuserv"...>>,
                                          {function,...},
                                          empty}},
                                  nil},
                           undefined,undefined,undefined,undefined,undefined,undefined,
                           undefined},
                          10,10,10,10,10,10,10,10,10}},
                  [],13,
                  {array,0,10,undefined,10},
                  [],0,
                  {tref,0},
                  [],
                  {meta,nil,nil,nil,{tref,10}},
                  #Ref<0.0.8.29>}}

tisba avatar Jul 11 '16 07:07 tisba

I stumbled across http://erlang.org/pipermail/erlang-questions/2015-January/082696.html and https://groups.google.com/d/msg/luerl/9p5LS14LX8Y/OALhHx_89Q4J and I was wondering if there is any change to the status quo on this particular issue.

tisba avatar Jul 14 '16 14:07 tisba

No, there have been no changes on that issue. The problem is that we have a combination of erlang stack and lua stack so when we call a lua function we end up calling the erlang function which evaluates all lua functions. The lua stack is used for temporary arguments and values. The lua VM would need to be changed to fix this. It is in the pipeline.

rvirding avatar Jul 14 '16 15:07 rvirding

I'm currently evaluating what the "best" way is to run user provided bits of logic in Elixir/Erlang. Lua seems to be a good fit, but without error reports I can present to the user is a pretty big deal breaker.

I think I begin to understand the issue, at least a little bit. This sounds like a pretty substantial change to add this kind of tracing information to be able to construct something comparable to the lua backtrace. I'd love to help out, but I think I lack the required Erlang fu and I'm got lost a couple of times trying to understand how luerl actually works :-/

tisba avatar Jul 14 '16 15:07 tisba

Hopefully I will get time to do this when I fix Lua 5.3. It will make other things easier and possible as well, for example the coroutines discussed in #62.

rvirding avatar Jul 22 '16 18:07 rvirding

Any updates on this one? Any pointers where to help?

codeadict avatar Apr 30 '20 13:04 codeadict

Yes, I am attacking this in the new-engine branch. I am building up a call stack which will have enough information to generate a callstack similar to the one which Erlang returns. It works and I am now working on modifying the API in luerl.erl to better handle it.

This may backwards incompatible changes.

rvirding avatar Jun 15 '20 20:06 rvirding