Skip to content

Commit

Permalink
Propagate original error information (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
davydog187 authored Mar 13, 2024
1 parent 3705b91 commit 6759959
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
13 changes: 5 additions & 8 deletions lib/lua.ex
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,16 @@ defmodule Lua do
raise Lua.CompilerException, reason: reason
end
rescue
e in [ArgumentError, FunctionClauseError] ->
reraise e, __STACKTRACE__

e in [CaseClauseError, MatchError] ->
reraise Lua.RuntimeException, "Could not match #{inspect(e.term)}", __STACKTRACE__

e in [UndefinedFunctionError] ->
reraise Lua.RuntimeException,
Util.format_function([e.module, e.function], e.arity),
__STACKTRACE__

e in [ErlangError] ->
reraise Lua.RuntimeException, e.original, __STACKTRACE__
e in [Lua.RuntimeException, Lua.CompilerException] ->
reraise e, __STACKTRACE__

e ->
reraise Lua.RuntimeException, e, __STACKTRACE__
end

# Deep-set a value within a nested Lua table structure,
Expand Down
19 changes: 15 additions & 4 deletions lib/lua/runtime_exception.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Lua.RuntimeException do
defexception [:message]
defexception [:message, :original, :state]

alias Lua.Util

Expand Down Expand Up @@ -29,6 +29,8 @@ defmodule Lua.RuntimeException do
stacktrace = Luerl.New.get_stacktrace(state)

%__MODULE__{
original: error,
state: state,
message: """
Lua runtime error: #{message}
Expand All @@ -37,8 +39,8 @@ defmodule Lua.RuntimeException do
}
end

def exception({:api_error, details, _state}) do
%__MODULE__{message: "Lua API error: #{details}"}
def exception({:api_error, details, state}) do
%__MODULE__{original: details, state: state, message: "Lua API error: #{details}"}
end

def exception(list) when is_list(list) do
Expand All @@ -47,6 +49,8 @@ defmodule Lua.RuntimeException do
message = Keyword.fetch!(list, :message)

%__MODULE__{
original: list,
state: nil,
message: "Lua runtime error: #{format_function(scope, function)} failed, #{message}"
}
end
Expand All @@ -56,7 +60,14 @@ defmodule Lua.RuntimeException do
end

def exception(error) do
%__MODULE__{message: "Lua runtime error: #{inspect(error)}"}
message =
if is_exception(error) do
Exception.message(error)
else
inspect(error)
end

%__MODULE__{original: error, message: "Lua runtime error: #{message}"}
end

defp format_function([], function), do: "#{function}()"
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Lua.MixProject do
use Mix.Project

@url "https://github.com/tv-labs/lua"
@version "0.0.2"
@version "0.0.3"

def project do
[
Expand Down
14 changes: 13 additions & 1 deletion test/lua_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@ defmodule LuaTest do
""")
end
end

test "arithmetic exceptions are handled" do
error = "Lua runtime error: bad argument in arithmetic expression"

assert_raise Lua.RuntimeException, error, fn ->
lua = Lua.new()

Lua.eval!(lua, "return 5 / 0")
end
end
end

describe "set!/2 and get!/2" do
Expand Down Expand Up @@ -404,7 +414,9 @@ defmodule LuaTest do
end

test "it cannot return tuples from Elixir", %{lua: lua} do
assert_raise ArgumentError, fn ->
error = "Lua runtime error: argument error"

assert_raise Lua.RuntimeException, error, fn ->
Lua.eval!(lua, "return example.tuple()")
end
end
Expand Down

0 comments on commit 6759959

Please sign in to comment.