Skip to content

Commit

Permalink
use luerl's format error (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
davydog187 authored Mar 14, 2024
1 parent 0803ba3 commit 58fb92b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 32 deletions.
31 changes: 11 additions & 20 deletions lib/lua/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,31 @@ defmodule Lua.Util do

def format_error(error) do
case error do
{:error_call, message} ->
format_function("error", message)

{:undefined_function, nil} ->
"undefined function"

{:undefined_function, "sandboxed"} ->
"sandboxed function"

{:undefined_function, ref} ->
"undefined function #{inspect(ref)}"

{:undefined_method, nil, name} ->
"undefined method #{inspect(name)}"

{: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}"
"Failed to #{type}: illegal token on line #{line}: #{value}"

{:badarith, operator, values} ->
expression = values |> Enum.map(&to_string/1) |> Enum.join(" #{operator} ")

"bad arithmetic #{expression}"

{:illegal_index, _type, message} ->
# TODO we can try to get fancy here and
# print what the object was that they tried to access
"invalid index \"#{message}\""

{line, _type, message} ->
"Line #{line}: #{message}"

error ->
"unknown error #{inspect(error)}"
:luerl_lib.format_error(error)
# "unknown error #{inspect(error)}"
end
end

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.6"
@version "0.0.7"

def project do
[
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/illegal_token.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shisjg;alskdgjl''z'
2 changes: 1 addition & 1 deletion test/fixtures/syntax_error.lua
Original file line number Diff line number Diff line change
@@ -1 +1 @@
shisjg;alskdgjl''z'
print("hell"),
34 changes: 24 additions & 10 deletions test/lua_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,28 @@ defmodule LuaTest do
""")
end

test "loading files with illegal tokens returns an error" do
path = test_file("illegal_token")

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

test "loading files with syntax errors returns an error" do
path = test_file("syntax_error")

error = """
Failed to compile Lua script!
Failed to tokenize illegal token on line 1: '
Line 1: syntax error before: ','
"""

assert_raise Lua.CompilerException, error, fn ->
Expand All @@ -56,7 +70,7 @@ defmodule LuaTest do
"""
Failed to compile Lua script!
undefined function
undefined function nil
script line 1: <unknown function>()
"""
Expand Down Expand Up @@ -90,7 +104,7 @@ defmodule LuaTest do
lua = Lua.new()

error = """
Lua runtime error: undefined function
Lua runtime error: undefined function nil
script line 1: <unknown function>()
"""
Expand Down Expand Up @@ -131,7 +145,7 @@ defmodule LuaTest do
lua = Lua.new()

error = """
Lua runtime error: undefined function
Lua runtime error: undefined function nil
script line 2: <unknown function>("yuup")
"""
Expand All @@ -150,7 +164,7 @@ defmodule LuaTest do
error = """
Failed to compile Lua script!
Failed to tokenize illegal token on line 1: ")
Failed to tokenize: illegal token on line 1: ")
"""

Expand All @@ -163,7 +177,7 @@ defmodule LuaTest do
error = """
Failed to compile Lua script!
Failed to tokenize illegal token on line 1: "yuup)
Failed to tokenize: illegal token on line 1: "yuup)
"""

Expand All @@ -178,7 +192,7 @@ defmodule LuaTest do
lua = Lua.new()

error = """
Lua runtime error: undefined function "a"
Lua runtime error: undefined function 'a'
"a" with arguments ("b")
^--- self is incorrect for object with keys "name"
Expand Down Expand Up @@ -213,7 +227,7 @@ defmodule LuaTest do
lua = Lua.new()

error = """
Lua runtime error: undefined function
Lua runtime error: undefined function nil
script line 2: <unknown function>(\"dude\")
script line 6: foo(2, \"dude\")
Expand Down Expand Up @@ -291,7 +305,7 @@ defmodule LuaTest do

test "error/1 raises an exception" do
error = """
Lua runtime error: error("this is an error")
Lua runtime error: this is an error
script line 1:error("this is an error")
"""
Expand Down

0 comments on commit 58fb92b

Please sign in to comment.