Skip to content

Commit d92175a

Browse files
author
José Valim
committed
Merge pull request #1834 from tonini/fix-wrong-error-message
fix wrong error message for `nil.func()` call
2 parents 3f8363c + 2e9f419 commit d92175a

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

lib/elixir/lib/exception.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ defmodule Exception do
277277
iex> Exception.format_mfa Foo, :bar, []
278278
"Foo.bar()"
279279
iex> Exception.format_mfa nil, :bar, []
280-
"bar()"
280+
"nil.bar()"
281281
282282
"""
283283
def format_mfa(module, fun, arity) do
@@ -295,7 +295,6 @@ defmodule Exception do
295295
end
296296
end
297297

298-
defp format_module(nil), do: ""
299298
defp format_module(mod), do: "#{inspect mod}."
300299

301300
@doc """

lib/elixir/test/elixir/exception_test.exs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ defmodule Kernel.ExceptionTest do
5454
assert Exception.format_mfa(Foo, nil, 1) == "Foo.nil/1"
5555
assert Exception.format_mfa(Foo, :bar, 1) == "Foo.bar/1"
5656
assert Exception.format_mfa(Foo, :bar, []) == "Foo.bar()"
57+
assert Exception.format_mfa(nil, :bar, []) == "nil.bar()"
5758
assert Exception.format_mfa(:foo, :bar, [1, 2]) == ":foo.bar(1, 2)"
5859
assert Exception.format_mfa(Foo, :"bar baz", 1) == "Foo.\"bar baz\"/1"
5960
end
@@ -76,6 +77,7 @@ defmodule Kernel.ExceptionTest do
7677
test :undefined_function_message do
7778
assert UndefinedFunctionError.new.message == "undefined function"
7879
assert UndefinedFunctionError.new(module: Foo, function: :bar, arity: 1).message == "undefined function: Foo.bar/1"
80+
assert UndefinedFunctionError.new(module: nil, function: :bar, arity: 0).message == "undefined function: nil.bar/0"
7981
end
8082

8183
test :function_clause_message do
@@ -91,13 +93,13 @@ defmodule Kernel.ExceptionTest do
9193
stacktrace =
9294
try do
9395
raise "a"
94-
rescue _ ->
96+
rescue _ ->
9597
[top|_] = System.stacktrace
9698
top
9799
end
98100
file = to_char_list(__FILE__)
99101
assert {Kernel.ExceptionTest, :test_raise_preserves_the_stacktrace, _,
100-
[file: ^file, line: 93]} = stacktrace # line is sensitive
102+
[file: ^file, line: 95]} = stacktrace # line is sensitive
101103
end
102104

103105
defp empty_tuple, do: {}

lib/iex/lib/iex/evaluator.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,22 @@ defmodule IEx.Evaluator do
183183
end
184184

185185
defp normalize_exception(:undef, [{ IEx.Helpers, fun, arity, _ }|t]) do
186-
{ UndefinedFunctionError[function: fun, arity: arity], t }
186+
{ RuntimeError[message: "undefined function: #{format_function(fun, arity)}"], t }
187187
end
188188

189189
defp normalize_exception(exception, stacktrace) do
190190
{ Exception.normalize(:error, exception), stacktrace }
191191
end
192192

193+
defp format_function(fun, arity) do
194+
cond do
195+
is_list(arity) ->
196+
"#{fun}/#{Enum.count(arity)}"
197+
true ->
198+
"#{fun}/#{arity}"
199+
end
200+
end
201+
193202
defp print_stacktrace(trace, callback) do
194203
try do
195204
io_error callback.()

lib/iex/test/iex/helpers_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ defmodule IEx.HelpersTest do
152152

153153
test "import_file helper" do
154154
with_file "dot-iex", "variable = :hello\nimport IO", fn ->
155-
assert "** (UndefinedFunctionError) undefined function: variable()" <> _
155+
assert "** (RuntimeError) undefined function: variable/0" <> _
156156
= capture_iex("variable")
157-
assert "** (UndefinedFunctionError) undefined function: puts(\"hi\")" <> _
157+
assert "** (RuntimeError) undefined function: puts/1" <> _
158158
= capture_iex("puts \"hi\"")
159159

160160
assert capture_iex("import_file \"dot-iex\"\nvariable\nputs \"hi\"")
@@ -167,11 +167,11 @@ defmodule IEx.HelpersTest do
167167
dot_1 = "variable = :hello\nimport IO"
168168

169169
with_file ["dot-iex", "dot-iex-1"], [dot, dot_1], fn ->
170-
assert "** (UndefinedFunctionError) undefined function: parent()" <> _
170+
assert "** (RuntimeError) undefined function: parent/0" <> _
171171
= capture_iex("parent")
172-
assert "** (UndefinedFunctionError) undefined function: variable()" <> _
172+
assert "** (RuntimeError) undefined function: variable/0" <> _
173173
= capture_iex("variable")
174-
assert "** (UndefinedFunctionError) undefined function: puts(\"hi\")" <> _
174+
assert "** (RuntimeError) undefined function: puts/1" <> _
175175
= capture_iex("puts \"hi\"")
176176

177177
assert capture_iex("import_file \"dot-iex\"\nvariable\nputs \"hi\"\nparent")

lib/iex/test/iex/interaction_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,16 @@ defmodule IEx.InteractionTest do
6060
assert capture_iex("if true do ) false end") =~ "** (SyntaxError) iex:1: \"do\" starting at"
6161
end
6262

63+
test "undefined function" do
64+
assert "** (RuntimeError) undefined function: format/0" <> _ = capture_iex("format")
65+
assert "** (RuntimeError) undefined function: with_one/1" <> _ = capture_iex("with_one(22)")
66+
assert "** (RuntimeError) undefined function: many/3" <> _ = capture_iex("many(:ok, 22, \"hi\")")
67+
end
68+
6369
## .iex file loading
6470

6571
test "no .iex" do
66-
assert "** (UndefinedFunctionError) undefined function: my_variable()" <> _ = capture_iex("my_variable")
72+
assert "** (RuntimeError) undefined function: my_variable/0" <> _ = capture_iex("my_variable")
6773
end
6874

6975
test ".iex" do

0 commit comments

Comments
 (0)