Repository: tv-labs/lua
Pull request #12 (2024-03-14 15:49:39)

Loading files with syntax errors returns an error


Patch:
diff --git a/lib/lua.ex b/lib/lua.ex
index 575ba20..f794497 100644
--- a/lib/lua.ex
+++ b/lib/lua.ex
@@ -155,8 +155,8 @@ defmodule Lua do
       {:lua_error, _e, _state} = error ->
         raise Lua.RuntimeException, error
 
-      {:error, reason, _stuff} ->
-        raise Lua.CompilerException, reason: reason
+      {:error, [error | _], _} ->
+        raise Lua.CompilerException, error
     end
   rescue
     e in [UndefinedFunctionError] ->
@@ -204,15 +204,19 @@ defmodule Lua do
   end
 
   def load_lua_file!(%__MODULE__{state: state} = lua, path) when is_binary(path) do
-    case Luerl.dofile(state, String.to_charlist(path)) do
+    case Luerl.dofile(state, String.to_charlist(path), [:return]) do
       {:ok, [], state} ->
         %__MODULE__{lua | state: state}
 
       {:lua_error, _error, _lua} = error ->
         raise Lua.CompilerException, error
 
-      :error ->
+      {:error, [{:none, :file, :enoent} | _], _} ->
         raise "Cannot load lua file, #{inspect(path <> ".lua")} does not exist"
+
+      {:error, [error | _], _} ->
+        # We just take the first error
+        raise Lua.CompilerException, error
     end
   end
 
diff --git a/lib/lua/compiler_exception.ex b/lib/lua/compiler_exception.ex
index 75fb8be..6ed9b83 100644
--- a/lib/lua/compiler_exception.ex
+++ b/lib/lua/compiler_exception.ex
@@ -7,7 +7,9 @@ defmodule Lua.CompilerException do
     stacktrace = Luerl.New.get_stacktrace(state)
 
     message = """
-    Failed to compile Lua script: #{Util.format_error(error)}
+    Failed to compile Lua script!
+
+    #{Util.format_error(error)}
 
     #{Util.format_stacktrace(stacktrace, state)}
     """
@@ -15,23 +17,13 @@ defmodule Lua.CompilerException do
     %__MODULE__{message: message}
   end
 
-  def exception(data) when is_list(data) do
-    message =
-      data
-      |> Keyword.fetch!(:reason)
-      |> Enum.map_join("\n", fn {line, error, failure} ->
-        "Line #{line}: #{format_error(error)} due to #{format_error(failure)}"
-      end)
-
-    %__MODULE__{message: "Failed to compile Lua script\n\n#{message}\n"}
-  end
-
-  defp format_error(:luerl_parse), do: "failed to parse"
-  defp format_error(:luerl_scan), do: "failed to tokenize"
+  def exception({_line, _type, _failure} = error) do
+    message = """
+    Failed to compile Lua script!
 
-  defp format_error({:illegal, value}) when is_list(value),
-    do: "illegal token: #{to_string(value)}"
+    #{Util.format_error(error)}
+    """
 
-  defp format_error({:illegal, value}), do: "illegal token: #{inspect(value)}}"
-  defp format_error(value), do: inspect(value)
+    %__MODULE__{message: message}
+  end
 end
diff --git a/lib/lua/util.ex b/lib/lua/util.ex
index 15132f4..f339534 100644
--- a/lib/lua/util.ex
+++ b/lib/lua/util.ex
@@ -55,6 +55,15 @@ defmodule Lua.Util do
       {:illegal_index, _, name} ->
         "invalid index #{inspect(name)}"
 
+      {line, type, {:illegal, value}} ->
+        type =
+          case type do
+            :luerl_parse -> "parse"
+            :luerl_scan -> "tokenize"
+          end
+
+        "Failed to #{type} illegal token on line #{line}: #{value}"
+
       {:badarith, operator, values} ->
         expression = values |> Enum.map(&to_string/1) |> Enum.join(" #{operator} ")
 
diff --git a/mix.exs b/mix.exs
index 1cd877e..96bca04 100644
--- a/mix.exs
+++ b/mix.exs
@@ -2,7 +2,7 @@ defmodule Lua.MixProject do
   use Mix.Project
 
   @url "https://github.com/tv-labs/lua"
-  @version "0.0.5"
+  @version "0.0.6"
 
   def project do
     [


Test patch:
diff --git a/test/lua_test.exs b/test/lua_test.exs
index a179fdc..0cbaaab 100644
--- a/test/lua_test.exs
+++ b/test/lua_test.exs
@@ -34,11 +34,17 @@ defmodule LuaTest do
                """)
     end
 
-    @tag :skip
     test "loading files with syntax errors returns an error" do
       path = test_file("syntax_error")
 
-      assert_raise Lua.CompilerException, fn ->
+      error = """
+      Failed to compile Lua script!
+
+      Failed to tokenize illegal token on line 1: '
+
+      """
+
+      assert_raise Lua.CompilerException, error, fn ->
         Lua.load_lua_file!(Lua.new(), path)
       end
     end
@@ -46,11 +52,14 @@ defmodule LuaTest do
     test "loading files with undefined functions returns an error" do
       path = test_file("undefined_function")
 
-      error = """
-      Failed to compile Lua script: undefined function
+      error =
+        """
+        Failed to compile Lua script!
 
-      script line 1: <unknown function>()
-      """
+        undefined function
+
+        script line 1: <unknown function>()
+        """
 
       assert_raise Lua.CompilerException, error, fn ->
         Lua.load_lua_file!(Lua.new(), path)
@@ -139,9 +148,9 @@ defmodule LuaTest do
       lua = Lua.new()
 
       error = """
-      Failed to compile Lua script
+      Failed to compile Lua script!
 
-      Line 1: failed to tokenize due to illegal token: ")
+      Failed to tokenize illegal token on line 1: ")
 
       """
 
@@ -152,9 +161,9 @@ defmodule LuaTest do
       end
 
       error = """
-      Failed to compile Lua script
+      Failed to compile Lua script!
 
-      Line 1: failed to tokenize due to illegal token: "yuup)
+      Failed to tokenize illegal token on line 1: "yuup)
 
       """