@@ -139,20 +139,19 @@ end
139
139
140
140
defmodule Exception do
141
141
@ 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.
156
155
"""
157
156
158
157
@ type stacktrace :: [ stacktrace_entry ]
@@ -172,75 +171,75 @@ defmodule Exception do
172
171
normalizes only `:error`, returning the untouched payload
173
172
for others.
174
173
"""
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
177
176
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
181
182
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
185
184
exception
186
185
end
187
186
188
- def normalize ( :badarg ) do
187
+ defp normalize_error ( :badarg ) do
189
188
ArgumentError [ ]
190
189
end
191
190
192
- def normalize ( :badarith ) do
191
+ defp normalize_error ( :badarith ) do
193
192
ArithmeticError [ ]
194
193
end
195
194
196
- def normalize ( :system_limit ) do
195
+ defp normalize_error ( :system_limit ) do
197
196
SystemLimitError [ ]
198
197
end
199
198
200
- def normalize ( { :badarity , { fun , args } } ) do
199
+ defp normalize_error ( { :badarity , { fun , args } } ) do
201
200
BadArityError [ function : fun , args: args ]
202
201
end
203
202
204
- def normalize ( { :badfun , term } ) do
203
+ defp normalize_error ( { :badfun , term } ) do
205
204
BadFunctionError [ term : term ]
206
205
end
207
206
208
- def normalize ( { :badstruct , struct , term } ) do
207
+ defp normalize_error ( { :badstruct , struct , term } ) do
209
208
BadStructError [ struct : struct , term: term ]
210
209
end
211
210
212
- def normalize ( { :badmatch , term } ) do
211
+ defp normalize_error ( { :badmatch , term } ) do
213
212
MatchError [ term : term ]
214
213
end
215
214
216
- def normalize ( { :case_clause , term } ) do
215
+ defp normalize_error ( { :case_clause , term } ) do
217
216
CaseClauseError [ term : term ]
218
217
end
219
218
220
- def normalize ( { :try_clause , term } ) do
219
+ defp normalize_error ( { :try_clause , term } ) do
221
220
TryClauseError [ term : term ]
222
221
end
223
222
224
- def normalize ( :undef ) do
223
+ defp normalize_error ( :undef ) do
225
224
{ mod , fun , arity } = from_stacktrace ( :erlang . get_stacktrace )
226
225
UndefinedFunctionError [ module : mod , function: fun , arity: arity ]
227
226
end
228
227
229
- def normalize ( :function_clause ) do
228
+ defp normalize_error ( :function_clause ) do
230
229
{ mod , fun , arity } = from_stacktrace ( :erlang . get_stacktrace )
231
230
FunctionClauseError [ module : mod , function: fun , arity: arity ]
232
231
end
233
232
234
- def normalize ( { :badarg , payload } ) do
233
+ defp normalize_error ( { :badarg , payload } ) do
235
234
ArgumentError [ message : "argument error: #{ inspect ( payload ) } " ]
236
235
end
237
236
238
- def normalize ( other ) do
237
+ defp normalize_error ( other ) do
239
238
ErlangError [ original : other ]
240
239
end
241
240
242
241
@ doc """
243
- Receives an stacktrace entry and formats it into a string.
242
+ Receives a stacktrace entry and formats it into a string.
244
243
"""
245
244
@ spec format_stacktrace_entry ( stacktrace_entry ) :: String . t
246
245
def format_stacktrace_entry ( entry )
@@ -260,11 +259,11 @@ defmodule Exception do
260
259
format_location ( location ) <> "(file)"
261
260
end
262
261
263
- def format_stacktrace_entry ( { module , fun , arity , location } ) do
262
+ def format_stacktrace_entry ( { module , fun , arity , location } ) do
264
263
format_application ( module ) <> format_location ( location ) <> format_mfa ( module , fun , arity )
265
264
end
266
265
267
- def format_stacktrace_entry ( { fun , arity , location } ) do
266
+ def format_stacktrace_entry ( { fun , arity , location } ) do
268
267
format_location ( location ) <> format_fa ( fun , arity )
269
268
end
270
269
0 commit comments