Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the error message when the wrong number of arguments are provided #212

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/expression/callbacks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,25 @@ defmodule Expression.Callbacks do
function_exported?(Standard, vargs_function_name, 2) ->
{:vargs, Standard, vargs_function_name, 2}

# Check if the wrong number of arguments was provided
wrong_arity_but_function_exists?(module, exact_function_name) ->
{:error, "wrong number of arguments to #{function_name}."}

# Check if the wrong number of arguments was provided
wrong_arity_but_function_exists?(Standard, exact_function_name) ->
{:error, "wrong number of arguments to #{function_name}."}

# Otherwise fail
true ->
{:error, "#{function_name} is not implemented."}
end
end

defp wrong_arity_but_function_exists?(module, function_name)
when is_atom(module) and is_atom(function_name) do
module.__info__(:functions)[function_name] != nil
end

defmacro __using__(_opts) do
quote do
import Expression.Callbacks.EvalHelpers
Expand Down
2 changes: 1 addition & 1 deletion test/expression_custom_callbacks_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule ExpressionCustomCallbacksTest do
end

test "wrong arity functions" do
assert {:error, "echo is not implemented."} ==
assert {:error, "wrong number of arguments to echo."} ==
Expression.Callbacks.implements(
ExpressionCustomCallbacksTest.CustomCallback,
"echo",
Expand Down
Loading