Skip to content

Commit f1effcb

Browse files
author
José Valim
committed
Use Exception.message/1 instead of exception.message
1 parent 237c485 commit f1effcb

File tree

19 files changed

+117
-123
lines changed

19 files changed

+117
-123
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
* Soft deprecations (no warnings emitted)
2222
* [Application] use Application.Behaviour is deprecated in favor of use Application
23+
* [Exception] Use `Exception.message/1` to retrieve exception messages instead of `exception.message`
2324
* [Mix] `Mix.project/0` is deprecated in favor of `Mix.Project.config/0`
2425
* [Process] `Process.spawn/1`, `Process.spawn/3`, `Process.spawn_link/1`, `Process.spawn_link/3`, `Process.spawn_monitor/1`, `Process.spawn_monitor/3`, `Process.send/2` and `Process.self/0` are deprecated in favor of the ones in Kernel
2526

lib/elixir/lib/exception.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,14 @@ defmodule Exception do
186186
Gets the message for an exception.
187187
"""
188188
def message(exception) do
189-
exception.message
189+
try do
190+
exception.message
191+
rescue
192+
e ->
193+
raise ArgumentError, message:
194+
"Got #{inspect e.__record__(:name)} with message " <>
195+
"\"#{message(e)}\" while retrieving message for #{inspect(exception)}"
196+
end
190197
end
191198

192199
@doc """
@@ -292,7 +299,7 @@ defmodule Exception do
292299

293300
def format_banner(:error, exception, stacktrace) do
294301
exception = normalize_error(exception, stacktrace)
295-
"** (" <> inspect(exception.__record__(:name)) <> ") " <> exception.message
302+
"** (" <> inspect(exception.__record__(:name)) <> ") " <> message(exception)
296303
end
297304

298305
def format_banner(:throw, reason, _stacktrace) do

lib/elixir/lib/inspect/algebra.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ defmodule Inspect.Algebra do
162162
rescue
163163
e ->
164164
res = Inspect.Tuple.inspect(arg, opts)
165-
IO.puts :stderr, "** (Inspect.Error) Got #{inspect e.__record__(:name)} with message " <>
166-
"#{e.message} while inspecting #{pretty(res, opts.width)}"
167-
res
165+
raise ArgumentError, message:
166+
"Got #{inspect e.__record__(:name)} with message " <>
167+
"#{Exception.message(e)} while inspecting #{pretty(res, opts.width)}"
168168
end
169169
else
170170
Inspect.Tuple.inspect(arg, opts)
@@ -178,9 +178,9 @@ defmodule Inspect.Algebra do
178178
rescue
179179
e ->
180180
res = Inspect.Map.inspect(map, opts)
181-
IO.puts :stderr, "** (Inspect.Error) Got #{inspect e.__record__(:name)} with message " <>
182-
"#{e.message} while inspecting #{pretty(res, opts.width)}"
183-
res
181+
raise ArgumentError, message:
182+
"Got #{inspect e.__record__(:name)} with message " <>
183+
"\"#{Exception.message(e)}\" while inspecting #{pretty(res, opts.width)}"
184184
end
185185
else
186186
Inspect.Map.inspect(map, opts)

lib/elixir/lib/kernel.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ defmodule Kernel do
17441744
rescue
17451745
exception ->
17461746
stacktrace = System.stacktrace
1747-
if exception.message == "Oops" do
1747+
if Exception.message(exception) == "Oops" do
17481748
raise exception, [], stacktrace
17491749
end
17501750
end

lib/elixir/test/elixir/access_test.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ defmodule AccessTest do
3939
foo = :foo
4040
foo[:atom]
4141
end
42-
assert exception.message == "The access protocol can only be invoked for atoms " <>
43-
"at compilation time, tried to invoke it for :foo"
42+
assert Exception.message(exception) ==
43+
"The access protocol can only be invoked for atoms " <>
44+
"at compilation time, tried to invoke it for :foo"
4445
end
4546
end

lib/elixir/test/elixir/exception_test.exs

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,41 @@ defmodule Kernel.ExceptionTest do
66
# Ensure fields passed through an expression are valid
77
defexception Custom, ~w(message)a
88

9+
test "raise preserves the stacktrace" do
10+
stacktrace =
11+
try do
12+
raise "a"
13+
rescue _ ->
14+
[top|_] = System.stacktrace
15+
top
16+
end
17+
file = __ENV__.file |> Path.relative_to_cwd |> List.from_char_data!
18+
assert {Kernel.ExceptionTest, :"test raise preserves the stacktrace", _,
19+
[file: ^file, line: 12]} = stacktrace
20+
end
21+
922
test "is_exception" do
1023
assert is_exception(RuntimeError.new)
1124
refute is_exception(empty_tuple)
1225
refute is_exception(a_tuple)
1326
refute is_exception(a_list)
1427
end
1528

29+
test "message" do
30+
defmodule BadException do
31+
def message(_) do
32+
raise "oops"
33+
end
34+
end
35+
36+
message = "Got RuntimeError with message \"oops\" " <>
37+
"while retrieving message for {Kernel.ExceptionTest.BadException}"
38+
39+
assert_raise ArgumentError, message, fn ->
40+
Exception.message({BadException})
41+
end
42+
end
43+
1644
test "normalize" do
1745
assert Exception.normalize(:throw, :badarg) == :badarg
1846
assert is_record Exception.normalize(:error, :badarg), ArgumentError
@@ -161,49 +189,36 @@ defmodule Kernel.ExceptionTest do
161189
~r"#Function<\d\.\d+/0 in Kernel\.ExceptionTest\.test format_fa/1>/1"
162190
end
163191

192+
import Exception, only: [message: 1]
193+
164194
test "runtime error message" do
165-
assert RuntimeError.new.message == "runtime error"
166-
assert RuntimeError.new(message: "exception").message == "exception"
195+
assert RuntimeError.new |> message == "runtime error"
196+
assert RuntimeError.new(message: "exception") |> message == "exception"
167197
end
168198

169199
test "argument error message" do
170-
assert ArgumentError.new.message == "argument error"
171-
assert ArgumentError.new(message: "exception").message == "exception"
200+
assert ArgumentError.new |> message == "argument error"
201+
assert ArgumentError.new(message: "exception") |> message == "exception"
172202
end
173203

174204
test "undefined function message" do
175-
assert UndefinedFunctionError.new.message == "undefined function"
176-
assert UndefinedFunctionError.new(module: Foo, function: :bar, arity: 1).message == "undefined function: Foo.bar/1"
177-
assert UndefinedFunctionError.new(module: nil, function: :bar, arity: 0).message == "undefined function: nil.bar/0"
205+
assert UndefinedFunctionError.new |> message == "undefined function"
206+
assert UndefinedFunctionError.new(module: Foo, function: :bar, arity: 1) |> message ==
207+
"undefined function: Foo.bar/1"
208+
assert UndefinedFunctionError.new(module: nil, function: :bar, arity: 0) |> message ==
209+
"undefined function: nil.bar/0"
178210
end
179211

180212
test "function clause message" do
181-
assert FunctionClauseError.new.message == "no function clause matches"
182-
assert FunctionClauseError.new(module: Foo, function: :bar, arity: 1).message == "no function clause matching in Foo.bar/1"
213+
assert FunctionClauseError.new |> message ==
214+
"no function clause matches"
215+
assert FunctionClauseError.new(module: Foo, function: :bar, arity: 1) |> message ==
216+
"no function clause matching in Foo.bar/1"
183217
end
184218

185219
test "erlang error message" do
186-
assert ErlangError.new(original: :sample).message == "erlang error: :sample"
187-
end
188-
189-
test "raise preserves the stacktrace" do
190-
stacktrace =
191-
try do
192-
raise "a"
193-
rescue _ ->
194-
[top|_] = System.stacktrace
195-
top
196-
end
197-
file = __ENV__.file |> Path.relative_to_cwd |> List.from_char_data!
198-
assert {Kernel.ExceptionTest, :"test raise preserves the stacktrace", _,
199-
[file: ^file, line: 192]} = stacktrace
200-
end
201-
202-
test "defexception" do
203-
defexception SampleError, message: nil do
204-
# Check do block is properly inline
205-
def exception(_), do: SampleError[message: "hello"]
206-
end
220+
assert ErlangError.new(original: :sample) |> message ==
221+
"erlang error: :sample"
207222
end
208223

209224
defp empty_tuple, do: {}

lib/elixir/test/elixir/inspect_test.exs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,6 @@ defmodule Inspect.TupleTest do
165165
assert inspect({Rec[value: 1], 1}) == "{Inspect.TupleTest.Rec[value: 1], 1}"
166166
end
167167

168-
test :false_positives do
169-
import ExUnit.CaptureIO
170-
171-
assert capture_io(:stderr, fn ->
172-
assert inspect({Range, nil}) == "{Range, nil}"
173-
end) =~ "** (Inspect.Error) Got FunctionClauseError with message no function clause matching in Inspect.Range.inspect/2"
174-
end
175-
176168
test :empty do
177169
assert inspect({}) == "{}"
178170
end
@@ -303,12 +295,12 @@ defmodule Inspect.MapTest do
303295
end
304296

305297
test :bad_implementation do
306-
import ExUnit.CaptureIO
298+
msg = "Got RuntimeError with message \"failing\" " <>
299+
"while inspecting %{__struct__: Inspect.MapTest.Failing, key: 0}"
307300

308-
assert capture_io(:stderr, fn ->
301+
assert_raise ArgumentError, msg, fn ->
309302
inspect(%Failing{})
310-
end) =~ ("** (Inspect.Error) Got RuntimeError with message failing while inspecting " <>
311-
"%{__struct__: Inspect.MapTest.Failing, key: 0}")
303+
end
312304
end
313305
end
314306

lib/elixir/test/elixir/kernel/overridable_test.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ defmodule Kernel.OverridableTest do
144144
flunk "expected eval to fail"
145145
rescue
146146
error ->
147-
assert error.message == ("nofile:4: no super defined for foo/0 in module Foo.Forwarding. " <>
148-
"Overridable functions available are: bar/0")
147+
assert Exception.message(error) ==
148+
"nofile:4: no super defined for foo/0 in module Foo.Forwarding. " <>
149+
"Overridable functions available are: bar/0"
149150
end
150151
end
151152
end

lib/elixir/test/elixir/kernel/rescue_test.exs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ defmodule Kernel.RescueTest do
5151
result = try do
5252
raise "an exception"
5353
rescue
54-
x in [RuntimeError] -> x.message
54+
x in [RuntimeError] -> Exception.message(x)
5555
catch
5656
:error, _ -> false
5757
end
@@ -75,7 +75,7 @@ defmodule Kernel.RescueTest do
7575
result = try do
7676
Certainly.Undefined.function(1, 2, 3)
7777
rescue
78-
x in [named] -> x.message
78+
x in [named] -> Exception.message(x)
7979
catch
8080
:error, _ -> "didn't catch it"
8181
end
@@ -87,7 +87,7 @@ defmodule Kernel.RescueTest do
8787
result = try do
8888
raise "an exception"
8989
rescue
90-
x in _ -> x.message
90+
x in _ -> Exception.message(x)
9191
end
9292

9393
assert result == "an exception"
@@ -99,7 +99,7 @@ defmodule Kernel.RescueTest do
9999
result = try do
100100
raise RuntimeError, message: "an exception"
101101
rescue
102-
x in [expected, AnotherError] -> x.message
102+
x in [expected, AnotherError] -> Exception.message(x)
103103
catch
104104
:error, _ -> false
105105
end
@@ -111,7 +111,7 @@ defmodule Kernel.RescueTest do
111111
result = try do
112112
:erlang.error(:sample)
113113
rescue
114-
x in [RuntimeError, ErlangError] -> x.message
114+
x in [RuntimeError, ErlangError] -> Exception.message(x)
115115
end
116116

117117
assert result == "erlang error: :sample"
@@ -121,7 +121,7 @@ defmodule Kernel.RescueTest do
121121
result = try do
122122
DoNotExist.for_sure()
123123
rescue
124-
x in [UndefinedFunctionError] -> x.message
124+
x in [UndefinedFunctionError] -> Exception.message(x)
125125
end
126126

127127
assert result == "undefined function: DoNotExist.for_sure/0"
@@ -131,7 +131,7 @@ defmodule Kernel.RescueTest do
131131
result = try do
132132
zero(1)
133133
rescue
134-
x in [FunctionClauseError] -> x.message
134+
x in [FunctionClauseError] -> Exception.message(x)
135135
end
136136

137137
assert result == "no function clause matching in Kernel.RescueTest.zero/1"
@@ -141,7 +141,7 @@ defmodule Kernel.RescueTest do
141141
result = try do
142142
:erlang.error(:badarg)
143143
rescue
144-
x in [ArgumentError] -> x.message
144+
x in [ArgumentError] -> Exception.message(x)
145145
end
146146

147147
assert result == "argument error"
@@ -151,7 +151,7 @@ defmodule Kernel.RescueTest do
151151
result = try do
152152
:erlang.error({:badarg, [1, 2, 3]})
153153
rescue
154-
x in [ArgumentError] -> x.message
154+
x in [ArgumentError] -> Exception.message(x)
155155
end
156156

157157
assert result == "argument error: [1, 2, 3]"
@@ -161,7 +161,7 @@ defmodule Kernel.RescueTest do
161161
result = try do
162162
:erlang.error(:badarith)
163163
rescue
164-
x in [ArithmeticError] -> x.message
164+
x in [ArithmeticError] -> Exception.message(x)
165165
end
166166

167167
assert result == "bad argument in arithmetic expression"
@@ -174,7 +174,7 @@ defmodule Kernel.RescueTest do
174174
result = try do
175175
fun.(1, 2)
176176
rescue
177-
x in [BadArityError] -> x.message
177+
x in [BadArityError] -> Exception.message(x)
178178
end
179179

180180
assert result == string
@@ -185,7 +185,7 @@ defmodule Kernel.RescueTest do
185185
result = try do
186186
x.(2)
187187
rescue
188-
x in [BadFunctionError] -> x.message
188+
x in [BadFunctionError] -> Exception.message(x)
189189
end
190190

191191
assert result == "expected a function, got: :example"
@@ -196,7 +196,7 @@ defmodule Kernel.RescueTest do
196196
result = try do
197197
^x = zero(0)
198198
rescue
199-
x in [MatchError] -> x.message
199+
x in [MatchError] -> Exception.message(x)
200200
end
201201

202202
assert result == "no match of right hand side value: 0"
@@ -209,7 +209,7 @@ defmodule Kernel.RescueTest do
209209
^x -> nil
210210
end
211211
rescue
212-
x in [CaseClauseError] -> x.message
212+
x in [CaseClauseError] -> Exception.message(x)
213213
end
214214

215215
assert result == "no case clause matching: 0"
@@ -225,7 +225,7 @@ defmodule Kernel.RescueTest do
225225
:ok
226226
end
227227
rescue
228-
x in [TryClauseError] -> x.message
228+
x in [TryClauseError] -> Exception.message(x)
229229
end
230230

231231
assert result == "no try clause matching: :example"
@@ -236,7 +236,7 @@ defmodule Kernel.RescueTest do
236236
result = try do
237237
DoNotExist.for_sure()
238238
rescue
239-
x in [expected] -> x.message
239+
x in [expected] -> Exception.message(x)
240240
end
241241

242242
assert result == "undefined function: DoNotExist.for_sure/0"
@@ -246,7 +246,7 @@ defmodule Kernel.RescueTest do
246246
result = try do
247247
DoNotExist.for_sure()
248248
rescue
249-
x in [ErlangError] -> x.message
249+
x in [ErlangError] -> Exception.message(x)
250250
end
251251

252252
assert result == "undefined function: DoNotExist.for_sure/0"
@@ -260,7 +260,7 @@ defmodule Kernel.RescueTest do
260260
result = try do
261261
DoNotExist.for_sure()
262262
rescue
263-
x in exceptions -> x.message
263+
x in exceptions -> Exception.message(x)
264264
end
265265

266266
assert result == "undefined function: DoNotExist.for_sure/0"

0 commit comments

Comments
 (0)