Skip to content

Commit c5ffa76

Browse files
author
José Valim
committed
Soft deprecate is_exception/1 in favor of Exception.exception?/1
1 parent 31e639f commit c5ffa76

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

CHANGELOG.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
* [System] Convert remaining functions in System to rely on char data
2727

2828
* Soft deprecations (no warnings emitted)
29-
* [Application] use Application.Behaviour is deprecated in favor of use Application
30-
* [Exception] Use `Exception.message/1` to retrieve exception messages instead of `exception.message`
29+
* [Application] `use Application.Behaviour` is deprecated in favor of `use Application`
30+
* [Exception] `exception.message` is deprecated in favor `Exception.message/1` for retrieving exception messages
31+
* [Kernel] `is_exception/1`, `is_record/1` and `is_record/2` are deprecated in favor of `Exception.exception?1`, `Record.record?/1` and `Record.record?/2`
3132
* [Mix] `Mix.project/0` is deprecated in favor of `Mix.Project.config/0`
32-
* [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
33+
* [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`
3334

3435
* Deprecations
3536
* [Kernel] `lc` and `bc` comprehensions are deprecated in favor of `for`
36-
* [Macro] `Macro.safe_terms` is deprecated
37+
* [Macro] `Macro.safe_terms/1` is deprecated
3738
* [Process] `Process.delete/0` is deprecated
3839
* [Regex] Deprecate `:global` option in `Regex.split/3` in favor of `parts: :infinity`
3940
* [String] Deprecate `:global` option in `String.split/3` in favor of `parts: :infinity`

lib/elixir/lib/exception.ex

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,14 @@ defmodule Exception do
182182
@typep arity_or_args :: non_neg_integer | list
183183
@typep location :: Keyword.t
184184

185+
@doc """
186+
Returns true if the given argument is an exception.
187+
"""
188+
def exception?(exception) do
189+
is_tuple(exception) and tuple_size(exception) > 1 and
190+
:erlang.element(2, exception) == :__exception__
191+
end
192+
185193
@doc """
186194
Gets the message for an exception.
187195
"""
@@ -218,13 +226,15 @@ defmodule Exception do
218226
def normalize(kind, payload, stacktrace \\ nil)
219227

220228
def normalize(:error, exception, stacktrace) do
221-
normalize_error(exception, stacktrace)
229+
if exception?(exception) do
230+
exception
231+
else
232+
normalize_error(exception, stacktrace)
233+
end
222234
end
223235

224-
def normalize(_kind, payload, _stacktrace), do: payload
225-
226-
defp normalize_error(exception, _stacktrace) when is_exception(exception) do
227-
exception
236+
def normalize(_kind, payload, _stacktrace) do
237+
payload
228238
end
229239

230240
defp normalize_error(:badarg, _stacktrace) do
@@ -298,7 +308,7 @@ defmodule Exception do
298308
def format_banner(kind, exception, stacktrace \\ nil)
299309

300310
def format_banner(:error, exception, stacktrace) do
301-
exception = normalize_error(exception, stacktrace)
311+
exception = normalize(:error, exception, stacktrace)
302312
"** (" <> inspect(exception.__record__(:name)) <> ") " <> message(exception)
303313
end
304314

lib/elixir/lib/kernel.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,7 @@ defmodule Kernel do
17761776
17771777
"""
17781778
defmacro is_exception(thing) do
1779+
# IO.write :stderr, "Kernel.is_exception/1 is deprecated, please use Exception.exception?/1 instead\n#{Exception.format_stacktrace}"
17791780
case Macro.Env.in_guard?(__CALLER__) do
17801781
true ->
17811782
quote do
@@ -1806,6 +1807,7 @@ defmodule Kernel do
18061807
18071808
"""
18081809
defmacro is_record(thing, kind) do
1810+
# IO.write :stderr, "Kernel.is_record/2 is deprecated, please use Record.record?/2 instead\n#{Exception.format_stacktrace}"
18091811
case Macro.Env.in_guard?(__CALLER__) do
18101812
true ->
18111813
quote do
@@ -1825,6 +1827,7 @@ defmodule Kernel do
18251827
Checks if the given argument is a record.
18261828
"""
18271829
defmacro is_record(thing) do
1830+
# IO.write :stderr, "Kernel.is_record/1 is deprecated, please use Record.record?/1 instead\n#{Exception.format_stacktrace}"
18281831
case Macro.Env.in_guard?(__CALLER__) do
18291832
true ->
18301833
quote do

lib/elixir/test/elixir/exception_test.exs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ defmodule Kernel.ExceptionTest do
1919
[file: ^file, line: 12]} = stacktrace
2020
end
2121

22-
test "is_exception" do
23-
assert is_exception(RuntimeError.new)
24-
refute is_exception(empty_tuple)
25-
refute is_exception(a_tuple)
26-
refute is_exception(a_list)
22+
test "exception?" do
23+
assert Exception.exception?(RuntimeError.new)
24+
refute Exception.exception?({})
25+
refute Exception.exception?(%{})
2726
end
2827

2928
test "message" do
@@ -220,8 +219,4 @@ defmodule Kernel.ExceptionTest do
220219
assert ErlangError.new(original: :sample) |> message ==
221220
"erlang error: :sample"
222221
end
223-
224-
defp empty_tuple, do: {}
225-
defp a_tuple, do: {:foo, :bar, :baz}
226-
defp a_list, do: [ :foo, :bar, :baz ]
227222
end

0 commit comments

Comments
 (0)