Skip to content

Commit 4e93dab

Browse files
author
José Valim
committed
Exception.normalize/1 is deprecated in favor of Exception.normalize/2
1 parent 4847214 commit 4e93dab

File tree

4 files changed

+40
-43
lines changed

4 files changed

+40
-43
lines changed

CHANGELOG.md

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

3333
* Deprecations
3434
* [Dict] `Dict.empty/1`, `Dict.new/1` and `Dict.new/2` are deprecated
35+
* [Exception] `Exception.normalize/1` is deprecated in favor of `Exception.normalize/2`
3536
* [Kernel] `lc` and `bc` comprehensions are deprecated in favor of `for` (this is a soft deprecation, no warning will be emitted)
3637
* [ListDict] `ListDict` is deprecated in favor of `Map` (this is a soft deprecation, no warning will be emitted)
3738

lib/elixir/lib/exception.ex

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,19 @@ end
139139

140140
defmodule Exception do
141141
@moduledoc """
142-
Convenience functions to work with and pretty print
143-
exceptions and stacktraces.
144-
145-
Notice that stacktraces in Elixir are updated on errors.
146-
For example, at any given moement, `System.stacktrace`
147-
will return the stacktrace for the last error that ocurred
148-
in the current process.
149-
150-
That said, many of the functions in this module will
151-
automatically calculate the stacktrace based on the caller,
152-
when invoked without arguments, changing the value of
153-
`System.stacktrace`. If instead you want to format the
154-
stacktrace of the latest error, you should instead explicitly
155-
pass the `System.stacktrace` as argument.
142+
Functions to work with and pretty print exceptions.
143+
144+
Note that stacktraces in Elixir are updated on throw,
145+
errors and exits. For example, at any given moment,
146+
`System.stacktrace` will return the stacktrace for the
147+
last throw/error/exit that ocurred in the current process.
148+
149+
Finally note developers should not rely on the particular
150+
format of the `format` functions provided by this module.
151+
They may be changed in future releases in order to better
152+
suit Elixir's tool chain. In other words, by using the
153+
functions in this module it is guarantee you will format
154+
exceptions as in the current Elixir version being used.
156155
"""
157156

158157
@type stacktrace :: [stacktrace_entry]
@@ -172,75 +171,75 @@ defmodule Exception do
172171
normalizes only `:error`, returning the untouched payload
173172
for others.
174173
"""
175-
def normalize(:error, exception), do: normalize(exception)
176-
def normalize(_kind, other), do: other
174+
def normalize(:error, exception), do: normalize_error(exception)
175+
def normalize(_kind, payload), do: payload
177176

178-
@doc """
179-
Normalizes an exception, converting Erlang exceptions
180-
to Elixir exceptions.
177+
@doc false
178+
def normalize(error) do
179+
IO.write :stderr, "Exception.normalize/1 is deprecated, please use Exception.normalize/2 instead\n#{Exception.format_stacktrace}"
180+
normalize_error(error)
181+
end
181182

182-
Useful when interfacing Erlang code with Elixir code.
183-
"""
184-
def normalize(exception) when is_exception(exception) do
183+
defp normalize_error(exception) when is_exception(exception) do
185184
exception
186185
end
187186

188-
def normalize(:badarg) do
187+
defp normalize_error(:badarg) do
189188
ArgumentError[]
190189
end
191190

192-
def normalize(:badarith) do
191+
defp normalize_error(:badarith) do
193192
ArithmeticError[]
194193
end
195194

196-
def normalize(:system_limit) do
195+
defp normalize_error(:system_limit) do
197196
SystemLimitError[]
198197
end
199198

200-
def normalize({ :badarity, { fun, args } }) do
199+
defp normalize_error({ :badarity, { fun, args } }) do
201200
BadArityError[function: fun, args: args]
202201
end
203202

204-
def normalize({ :badfun, term }) do
203+
defp normalize_error({ :badfun, term }) do
205204
BadFunctionError[term: term]
206205
end
207206

208-
def normalize({ :badstruct, struct, term }) do
207+
defp normalize_error({ :badstruct, struct, term }) do
209208
BadStructError[struct: struct, term: term]
210209
end
211210

212-
def normalize({ :badmatch, term }) do
211+
defp normalize_error({ :badmatch, term }) do
213212
MatchError[term: term]
214213
end
215214

216-
def normalize({ :case_clause, term }) do
215+
defp normalize_error({ :case_clause, term }) do
217216
CaseClauseError[term: term]
218217
end
219218

220-
def normalize({ :try_clause, term }) do
219+
defp normalize_error({ :try_clause, term }) do
221220
TryClauseError[term: term]
222221
end
223222

224-
def normalize(:undef) do
223+
defp normalize_error(:undef) do
225224
{ mod, fun, arity } = from_stacktrace(:erlang.get_stacktrace)
226225
UndefinedFunctionError[module: mod, function: fun, arity: arity]
227226
end
228227

229-
def normalize(:function_clause) do
228+
defp normalize_error(:function_clause) do
230229
{ mod, fun, arity } = from_stacktrace(:erlang.get_stacktrace)
231230
FunctionClauseError[module: mod, function: fun, arity: arity]
232231
end
233232

234-
def normalize({ :badarg, payload }) do
233+
defp normalize_error({ :badarg, payload }) do
235234
ArgumentError[message: "argument error: #{inspect(payload)}"]
236235
end
237236

238-
def normalize(other) do
237+
defp normalize_error(other) do
239238
ErlangError[original: other]
240239
end
241240

242241
@doc """
243-
Receives an stacktrace entry and formats it into a string.
242+
Receives a stacktrace entry and formats it into a string.
244243
"""
245244
@spec format_stacktrace_entry(stacktrace_entry) :: String.t
246245
def format_stacktrace_entry(entry)
@@ -260,11 +259,11 @@ defmodule Exception do
260259
format_location(location) <> "(file)"
261260
end
262261

263-
def format_stacktrace_entry({module, fun, arity, location}) do
262+
def format_stacktrace_entry({ module, fun, arity, location }) do
264263
format_application(module) <> format_location(location) <> format_mfa(module, fun, arity)
265264
end
266265

267-
def format_stacktrace_entry({fun, arity, location}) do
266+
def format_stacktrace_entry({ fun, arity, location }) do
268267
format_location(location) <> format_fa(fun, arity)
269268
end
270269

lib/elixir/src/elixir_try.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ each_clause({ rescue, Meta, [{ in, _, [Left, Right]}], Expr }, Return, S) ->
4242
{ FinalClause, _ } = rescue_guards(Meta, ClauseVar, Right, S),
4343
Match = { '=', Meta, [
4444
Left,
45-
{ { '.', Meta, ['Elixir.Exception', normalize] }, Meta, [ClauseVar] }
45+
{ { '.', Meta, ['Elixir.Exception', normalize] }, Meta, [error, ClauseVar] }
4646
] },
4747
FinalExpr = prepend_to_block(Meta, Match, Expr),
4848
each_clause({ 'catch', Meta, FinalClause, FinalExpr }, Return, CS)

lib/elixir/test/elixir/exception_test.exs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ defmodule Kernel.ExceptionTest do
1414
end
1515

1616
test "normalize" do
17-
assert is_record Exception.normalize(:badarg), ArgumentError
18-
assert is_record Exception.normalize(ArgumentError[]), ArgumentError
19-
2017
assert Exception.normalize(:throw, :badarg) == :badarg
2118
assert is_record Exception.normalize(:error, :badarg), ArgumentError
2219
assert is_record Exception.normalize(:error, ArgumentError[]), ArgumentError
@@ -105,7 +102,7 @@ defmodule Kernel.ExceptionTest do
105102
end
106103
file = __ENV__.file |> Path.relative_to_cwd |> String.to_char_list!
107104
assert {Kernel.ExceptionTest, :"test raise preserves the stacktrace", _,
108-
[file: ^file, line: 101]} = stacktrace
105+
[file: ^file, line: 98]} = stacktrace
109106
end
110107

111108
test "defexception" do

0 commit comments

Comments
 (0)