Skip to content

Commit

Permalink
Improve the error message when the wrong number of arguments are prov…
Browse files Browse the repository at this point in the history
…ided

Currently when a user provides the wrong number of arguments, they receive a
message saying `echo is not implemented.`.

This message is confusing because it makes the user feel like they typed in the
wrong function name, when in reality they just provided the wrong number of
arguments.

This is a first draft at a fix - feedback welcome!
  • Loading branch information
Patrick Sinclair committed Aug 28, 2024
1 parent 515db34 commit 74ae0ab
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
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
Enum.any?(0..20, fn arity -> Kernel.function_exported?(module, function_name, arity) end)
end

defmacro __using__(_opts) do
quote do
import Expression.Callbacks.EvalHelpers
Expand Down
13 changes: 13 additions & 0 deletions lib/expression/v2/callbacks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,25 @@ defmodule Expression.V2.Callbacks do
function_exported?(Standard, vargs_function_name, 2) ->
{:vargs, Standard, vargs_function_name}

# 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
Enum.any?(0..20, fn arity -> Kernel.function_exported?(module, function_name, arity) end)
end

defmacro __using__(_opts) do
quote do
def callback(module \\ __MODULE__, context, function_name, args)
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

0 comments on commit 74ae0ab

Please sign in to comment.