lua icon indicating copy to clipboard operation
lua copied to clipboard

Error formating exceptions from luerl_lib errors

Open anjou-low opened this issue 4 months ago • 1 comments

Hi!

Using version 0.3.0, it seems there is a problem with the formating of a Lua.RuntimeException with the following code:

code = """
  for i in pairs(nil) do
    print(i)
  end
"""

Lua.eval!(Lua.new(), code)
** (Lua.RuntimeException) Lua runtime error: cannot convert the given list to a string.

To be converted to a string, a list must either be empty or only
contain the following elements:

  * strings
  * integers representing Unicode code points
  * a list containing one of these three elements

Please check the given list or call inspect/1 to get the list representation, got:

[nil, nil]

    (elixir 1.18.4) lib/list.ex:1124: List.to_string/1
    (lua 0.3.0) lib/lua/util.ex:62: Lua.Util.format_error/1
    (lua 0.3.0) lib/lua/runtime_exception.ex:8: Lua.RuntimeException.exception/1
    (lua 0.3.0) lib/lua.ex:388: Lua.eval!/3
    iex:1: (file)

Note that the same issue arises when using ipairs instead of pairs

After a quick investigation it looks like the error returned by luerl is {:badarg, :next, [nil, nil]} is handled by the wrong branch here https://github.com/tv-labs/lua/blob/51a566f070d45aa1e25a68272adb5d8ae0e394ea/lib/lua/util.ex#L61

Fixing this does not solve the problem however as another error arises when trying to format the stacktrace

** (Lua.RuntimeException) Lua runtime error: protocol String.Chars not implemented for type Tuple.
Got value:

    {:luerl_lib_basic, :next, :undefined}

    (elixir 1.18.4) lib/string/chars.ex:3: String.Chars.impl_for!/1
    (elixir 1.18.4) lib/string/chars.ex:22: String.Chars.to_string/1
    (elixir 1.18.4) lib/enum.ex:4497: Enum.map_intersperse_list/3
    (elixir 1.18.4) lib/enum.ex:1815: Enum.map_join/3
    (lua 0.3.0) lib/lua/util.ex:147: Lua.Util.format_function/2
    (lua 0.3.0) lib/lua/util.ex:104: anonymous fn/3 in Lua.Util.format_stacktrace/3
    (elixir 1.18.4) lib/enum.ex:1815: anonymous fn/2 in Enum.map_join/3
    (elixir 1.18.4) lib/enum.ex:4497: Enum.map_intersperse_list/3
    (elixir 1.18.4) lib/enum.ex:1815: Enum.map_join/3
    (lua 0.3.0) lib/lua/runtime_exception.ex:17: Lua.RuntimeException.exception/1
    (lua 0.3.0) lib/lua.ex:388: Lua.eval!/3
    iex:5: (file)

There seems to be a general issue with the handling of luerl's standard lib errors, e.g.

Lua.eval!(Lua.new(), "string.lower(nil)")
** (Lua.RuntimeException) Lua runtime error: protocol String.Chars not implemented for type Tuple.

Got value:

    {:luerl_lib_string, :lower, :undefined}

    (elixir 1.18.4) lib/string/chars.ex:3: String.Chars.impl_for!/1
    (elixir 1.18.4) lib/string/chars.ex:22: String.Chars.to_string/1
    (elixir 1.18.4) lib/enum.ex:4497: Enum.map_intersperse_list/3
    (elixir 1.18.4) lib/enum.ex:1815: Enum.map_join/3
    (lua 0.3.0) lib/lua/util.ex:147: Lua.Util.format_function/2
    (lua 0.3.0) lib/lua/util.ex:104: anonymous fn/3 in Lua.Util.format_stacktrace/3
    (elixir 1.18.4) lib/enum.ex:1815: anonymous fn/2 in Enum.map_join/3
    (elixir 1.18.4) lib/enum.ex:4497: Enum.map_intersperse_list/3
    (elixir 1.18.4) lib/enum.ex:1815: Enum.map_join/3
    (lua 0.3.0) lib/lua/runtime_exception.ex:17: Lua.RuntimeException.exception/1
    (lua 0.3.0) lib/lua.ex:388: Lua.eval!/3
    iex:6: (file)

Do you want me to try something? If so do you have directions :P

anjou-low avatar Aug 20 '25 12:08 anjou-low

Error handling is mostly in a really bad state right now.

Part of the issue was me guessing on how to format errors. The other issue is that Luerl errors are already lacking in a lot of details (and often obtuse or wrong stack traces)

Improvements and PRs are very welcome. I don't have any explicit guidance at this time

davydog187 avatar Aug 20 '25 21:08 davydog187