Chapter IV · Lua.ex integration / Lesson 25 of 27 Host integration

Errors across the boundary

You'll learn: Catch Lua.RuntimeException on the Elixir side. Line, source, and call stack come along.

Runtime errors in Lua raise Lua.RuntimeException on the Elixir side. The exception carries the offending line, the source snippet, and the call stack. try/rescue catches it; Exception.message/1 formats the pretty version you see in this playground.

Elixir · your app
Reference only
try do
  Lua.eval!(Lua.new(), """
    local function deep(x) return x.missing.field end
    deep(nil)
  """)
rescue
  e in Lua.RuntimeException ->
    IO.puts(Exception.message(e))
    {:error, e.line, e.source}
end
errors-host.lua
Output idle
Hit Run to execute.
Try it: Change nil to { missing = { field = 42 } } and the error disappears: you get 42 back. Now toggle Bytecode and find the get_table op that the runtime stack points at.