/
exception.ex
1469 lines (1173 loc) · 42.2 KB
/
exception.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
defmodule Exception do
@moduledoc """
Functions to format throw/catch/exit and exceptions.
Note that stacktraces in Elixir are only available inside
catch and rescue by using the `__STACKTRACE__/0` variable.
Do not rely on the particular format returned by the `format*`
functions in this module. They may be changed in future releases
in order to better suit Elixir's tool chain. In other words,
by using the functions in this module it is guaranteed you will
format exceptions as in the current Elixir version being used.
"""
@typedoc "The exception type"
@type t :: %{
required(:__struct__) => module,
required(:__exception__) => true,
optional(atom) => any
}
@typedoc "The kind handled by formatting functions"
@type kind :: :error | non_error_kind
@typep non_error_kind :: :exit | :throw | {:EXIT, pid}
@type stacktrace :: [stacktrace_entry]
@type stacktrace_entry ::
{module, atom, arity_or_args, location}
| {(... -> any), arity_or_args, location}
@typep arity_or_args :: non_neg_integer | list
@typep location :: keyword
@callback exception(term) :: t
@callback message(t) :: String.t()
@doc """
Called from `Exception.blame/3` to augment the exception struct.
Can be used to collect additional information about the exception
or do some additional expensive computation.
"""
@callback blame(t, stacktrace) :: {t, stacktrace}
@optional_callbacks [blame: 2]
@doc """
Returns `true` if the given `term` is an exception.
"""
def exception?(term)
def exception?(%_{__exception__: true}), do: true
def exception?(_), do: false
@doc """
Gets the message for an `exception`.
"""
def message(%module{__exception__: true} = exception) do
try do
module.message(exception)
rescue
caught_exception ->
"got #{inspect(caught_exception.__struct__)} with message " <>
"#{inspect(message(caught_exception))} while retrieving Exception.message/1 " <>
"for #{inspect(exception)}"
else
result when is_binary(result) ->
result
result ->
"got #{inspect(result)} " <>
"while retrieving Exception.message/1 for #{inspect(exception)} " <>
"(expected a string)"
end
end
@doc """
Normalizes an exception, converting Erlang exceptions
to Elixir exceptions.
It takes the `kind` spilled by `catch` as an argument and
normalizes only `:error`, returning the untouched payload
for others.
The third argument is the stacktrace which is used to enrich
a normalized error with more information. It is only used when
the kind is an error.
"""
@spec normalize(:error, any, stacktrace) :: t
@spec normalize(non_error_kind, payload, stacktrace) :: payload when payload: var
def normalize(kind, payload, stacktrace \\ [])
def normalize(:error, %_{__exception__: true} = payload, _stacktrace), do: payload
def normalize(:error, payload, stacktrace), do: ErlangError.normalize(payload, stacktrace)
def normalize(_kind, payload, _stacktrace), do: payload
@doc """
Normalizes and formats any throw/error/exit.
The message is formatted and displayed in the same
format as used by Elixir's CLI.
The third argument is the stacktrace which is used to enrich
a normalized error with more information. It is only used when
the kind is an error.
"""
@spec format_banner(kind, any, stacktrace) :: String.t()
def format_banner(kind, exception, stacktrace \\ [])
def format_banner(:error, exception, stacktrace) do
exception = normalize(:error, exception, stacktrace)
"** (" <> inspect(exception.__struct__) <> ") " <> message(exception)
end
def format_banner(:throw, reason, _stacktrace) do
"** (throw) " <> inspect(reason)
end
def format_banner(:exit, reason, _stacktrace) do
"** (exit) " <> format_exit(reason, <<"\n ">>)
end
def format_banner({:EXIT, pid}, reason, _stacktrace) do
"** (EXIT from #{inspect(pid)}) " <> format_exit(reason, <<"\n ">>)
end
@doc """
Normalizes and formats throw/errors/exits and stacktraces.
It relies on `format_banner/3` and `format_stacktrace/1`
to generate the final format.
If `kind` is `{:EXIT, pid}`, it does not generate a stacktrace,
as such exits are retrieved as messages without stacktraces.
"""
@spec format(kind, any, stacktrace) :: String.t()
def format(kind, payload, stacktrace \\ [])
def format({:EXIT, _} = kind, any, _) do
format_banner(kind, any)
end
def format(kind, payload, stacktrace) do
message = format_banner(kind, payload, stacktrace)
case stacktrace do
[] -> message
_ -> message <> "\n" <> format_stacktrace(stacktrace)
end
end
@doc """
Attaches information to exceptions for extra debugging.
This operation is potentially expensive, as it reads data
from the file system, parses beam files, evaluates code and
so on.
If the exception module implements the optional `c:blame/2`
callback, it will be invoked to perform the computation.
"""
@doc since: "1.5.0"
@spec blame(:error, any, stacktrace) :: {t, stacktrace}
@spec blame(non_error_kind, payload, stacktrace) :: {payload, stacktrace} when payload: var
def blame(kind, error, stacktrace)
def blame(:error, error, stacktrace) do
%module{} = struct = normalize(:error, error, stacktrace)
if Code.ensure_loaded?(module) and function_exported?(module, :blame, 2) do
module.blame(struct, stacktrace)
else
{struct, stacktrace}
end
end
def blame(_kind, reason, stacktrace) do
{reason, stacktrace}
end
@doc """
Blames the invocation of the given module, function and arguments.
This function will retrieve the available clauses from bytecode
and evaluate them against the given arguments. The clauses are
returned as a list of `{args, guards}` pairs where each argument
and each top-level condition in a guard separated by `and`/`or`
is wrapped in a tuple with blame metadata.
This function returns either `{:ok, definition, clauses}` or `:error`.
Where `definition` is `:def`, `:defp`, `:defmacro` or `:defmacrop`.
"""
@doc since: "1.5.0"
@spec blame_mfa(module, function, args :: [term]) ::
{:ok, :def | :defp | :defmacro | :defmacrop, [{args :: [term], guards :: [term]}]}
| :error
def blame_mfa(module, function, args)
when is_atom(module) and is_atom(function) and is_list(args) do
try do
blame_mfa(module, function, length(args), args)
rescue
_ -> :error
end
end
defp blame_mfa(module, function, arity, call_args) do
with [_ | _] = path <- :code.which(module),
{:ok, {_, [debug_info: debug_info]}} <- :beam_lib.chunks(path, [:debug_info]),
{:debug_info_v1, backend, data} <- debug_info,
{:ok, %{definitions: defs}} <- backend.debug_info(:elixir_v1, module, data, []),
{_, kind, _, clauses} <- List.keyfind(defs, {function, arity}, 0) do
clauses =
for {meta, ex_args, guards, _block} <- clauses do
scope = :elixir_erl.scope(meta, true)
{erl_args, scope} =
:elixir_erl_clauses.match(&:elixir_erl_pass.translate_args/2, ex_args, scope)
{args, binding} =
[call_args, ex_args, erl_args]
|> Enum.zip()
|> Enum.map_reduce([], &blame_arg/2)
guards = Enum.map(guards, &blame_guard(&1, scope, binding))
{args, guards}
end
{:ok, kind, clauses}
else
_ -> :error
end
end
defp blame_arg({call_arg, ex_arg, erl_arg}, binding) do
{match?, binding} = blame_arg(erl_arg, call_arg, binding)
{blame_wrap(match?, rewrite_arg(ex_arg)), binding}
end
defp blame_arg(erl_arg, call_arg, binding) do
binding = :orddict.store(:VAR, call_arg, binding)
try do
{:value, _, binding} = :erl_eval.expr({:match, 0, erl_arg, {:var, 0, :VAR}}, binding, :none)
{true, binding}
rescue
_ -> {false, binding}
end
end
defp rewrite_arg(arg) do
Macro.prewalk(arg, fn
{:%{}, meta, [__struct__: Range, first: first, last: last]} ->
{:.., meta, [first, last]}
other ->
other
end)
end
defp blame_guard({{:., _, [:erlang, op]}, meta, [left, right]}, scope, binding)
when op == :andalso or op == :orelse do
guards = [
blame_guard(left, scope, binding),
blame_guard(right, scope, binding)
]
{rewrite_guard_call(op), meta, guards}
end
defp blame_guard(ex_guard, scope, binding) do
{erl_guard, _} = :elixir_erl_pass.translate(ex_guard, scope)
match? =
try do
{:value, true, _} = :erl_eval.expr(erl_guard, binding, :none)
true
rescue
_ -> false
end
blame_wrap(match?, rewrite_guard(ex_guard))
end
defp rewrite_guard(guard) do
Macro.prewalk(guard, fn
{{:., _, [:erlang, :element]}, _, [{{:., _, [:erlang, :+]}, _, [int, 1]}, arg]} ->
{:elem, [], [arg, int]}
{{:., _, [:erlang, :element]}, _, [int, arg]} when is_integer(int) ->
{:elem, [], [arg, int - 1]}
{:., _, [:erlang, call]} ->
rewrite_guard_call(call)
other ->
other
end)
end
defp rewrite_guard_call(:orelse), do: :or
defp rewrite_guard_call(:andalso), do: :and
defp rewrite_guard_call(:"=<"), do: :<=
defp rewrite_guard_call(:"/="), do: :!=
defp rewrite_guard_call(:"=:="), do: :===
defp rewrite_guard_call(:"=/="), do: :!==
defp rewrite_guard_call(op) when op in [:band, :bor, :bnot, :bsl, :bsr, :bxor],
do: {:., [], [Bitwise, op]}
defp rewrite_guard_call(op) when op in [:xor, :element, :size], do: {:., [], [:erlang, op]}
defp rewrite_guard_call(op), do: op
defp blame_wrap(match?, ast), do: %{match?: match?, node: ast}
@doc """
Formats an exit. It returns a string.
Often there are errors/exceptions inside exits. Exits are often
wrapped by the caller and provide stacktraces too. This function
formats exits in a way to nicely show the exit reason, caller
and stacktrace.
"""
@spec format_exit(any) :: String.t()
def format_exit(reason) do
format_exit(reason, <<"\n ">>)
end
# 2-Tuple could be caused by an error if the second element is a stacktrace.
defp format_exit({exception, maybe_stacktrace} = reason, joiner)
when is_list(maybe_stacktrace) and maybe_stacktrace !== [] do
try do
Enum.map(maybe_stacktrace, &format_stacktrace_entry/1)
catch
:error, _ ->
# Not a stacktrace, was an exit.
format_exit_reason(reason)
else
formatted_stacktrace ->
# Assume a non-empty list formattable as stacktrace is a
# stacktrace, so exit was caused by an error.
message =
"an exception was raised:" <>
joiner <> format_banner(:error, exception, maybe_stacktrace)
Enum.join([message | formatted_stacktrace], joiner <> <<" ">>)
end
end
# :supervisor.start_link returns this error reason when it fails to init
# because a child's start_link raises.
defp format_exit({:shutdown, {:failed_to_start_child, child, {:EXIT, reason}}}, joiner) do
format_start_child(child, reason, joiner)
end
# :supervisor.start_link returns this error reason when it fails to init
# because a child's start_link returns {:error, reason}.
defp format_exit({:shutdown, {:failed_to_start_child, child, reason}}, joiner) do
format_start_child(child, reason, joiner)
end
# 2-Tuple could be an exit caused by mfa if second element is mfa, args
# must be a list of arguments - max length 255 due to max arity.
defp format_exit({reason2, {mod, fun, args}} = reason, joiner)
when length(args) < 256 do
try do
format_mfa(mod, fun, args)
catch
:error, _ ->
# Not an mfa, was an exit.
format_exit_reason(reason)
else
mfa ->
# Assume tuple formattable as an mfa is an mfa,
# so exit was caused by failed mfa.
"exited in: " <>
mfa <> joiner <> "** (EXIT) " <> format_exit(reason2, joiner <> <<" ">>)
end
end
defp format_exit(reason, _joiner) do
format_exit_reason(reason)
end
defp format_exit_reason(:normal), do: "normal"
defp format_exit_reason(:shutdown), do: "shutdown"
defp format_exit_reason({:shutdown, reason}) do
"shutdown: #{inspect(reason)}"
end
defp format_exit_reason(:calling_self), do: "process attempted to call itself"
defp format_exit_reason(:timeout), do: "time out"
defp format_exit_reason(:killed), do: "killed"
defp format_exit_reason(:noconnection), do: "no connection"
defp format_exit_reason(:noproc) do
"no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started"
end
defp format_exit_reason({:nodedown, node_name}) when is_atom(node_name) do
"no connection to #{node_name}"
end
# :gen_server exit reasons
defp format_exit_reason({:already_started, pid}) do
"already started: " <> inspect(pid)
end
defp format_exit_reason({:bad_return_value, value}) do
"bad return value: " <> inspect(value)
end
defp format_exit_reason({:bad_call, request}) do
"bad call: " <> inspect(request)
end
defp format_exit_reason({:bad_cast, request}) do
"bad cast: " <> inspect(request)
end
# :supervisor.start_link error reasons
# If value is a list will be formatted by mfa exit in format_exit/1
defp format_exit_reason({:bad_return, {mod, :init, value}})
when is_atom(mod) do
format_mfa(mod, :init, 1) <> " returned a bad value: " <> inspect(value)
end
defp format_exit_reason({:bad_start_spec, start_spec}) do
"bad child specification, invalid children: " <> inspect(start_spec)
end
defp format_exit_reason({:start_spec, start_spec}) do
"bad child specification, " <> format_sup_spec(start_spec)
end
defp format_exit_reason({:supervisor_data, data}) do
"bad supervisor configuration, " <> format_sup_data(data)
end
defp format_exit_reason(reason), do: inspect(reason)
defp format_start_child(child, reason, joiner) do
"shutdown: failed to start child: " <>
inspect(child) <> joiner <> "** (EXIT) " <> format_exit(reason, joiner <> <<" ">>)
end
defp format_sup_data({:invalid_type, type}) do
"invalid type: " <> inspect(type)
end
defp format_sup_data({:invalid_strategy, strategy}) do
"invalid strategy: " <> inspect(strategy)
end
defp format_sup_data({:invalid_intensity, intensity}) do
"invalid max_restarts (intensity): " <> inspect(intensity)
end
defp format_sup_data({:invalid_period, period}) do
"invalid max_seconds (period): " <> inspect(period)
end
defp format_sup_data({:invalid_max_children, max_children}) do
"invalid max_children: " <> inspect(max_children)
end
defp format_sup_data({:invalid_extra_arguments, extra}) do
"invalid extra_arguments: " <> inspect(extra)
end
defp format_sup_data(other), do: "got: #{inspect(other)}"
defp format_sup_spec({:duplicate_child_name, id}) do
"""
more than one child specification has the id: #{inspect(id)}.
If using maps as child specifications, make sure the :id keys are unique.
If using a module or {module, arg} as child, use Supervisor.child_spec/2 to change the :id, for example:
children = [
Supervisor.child_spec({MyWorker, arg}, id: :my_worker_1),
Supervisor.child_spec({MyWorker, arg}, id: :my_worker_2)
]
"""
end
defp format_sup_spec({:invalid_child_spec, child_spec}) do
"invalid child specification: #{inspect(child_spec)}"
end
defp format_sup_spec({:invalid_child_type, type}) do
"invalid child type: #{inspect(type)}. Must be :worker or :supervisor."
end
defp format_sup_spec({:invalid_mfa, mfa}) do
"invalid mfa: #{inspect(mfa)}"
end
defp format_sup_spec({:invalid_restart_type, restart}) do
"invalid restart type: #{inspect(restart)}. Must be :permanent, :transient or :temporary."
end
defp format_sup_spec({:invalid_shutdown, shutdown}) do
"invalid shutdown: #{inspect(shutdown)}. Must be an integer >= 0, :infinity or :brutal_kill."
end
defp format_sup_spec({:invalid_module, mod}) do
"invalid module: #{inspect(mod)}. Must be an atom."
end
defp format_sup_spec({:invalid_modules, modules}) do
"invalid modules: #{inspect(modules)}. Must be a list of atoms or :dynamic."
end
defp format_sup_spec(other), do: "got: #{inspect(other)}"
@doc """
Receives a stacktrace entry and formats it into a string.
"""
@spec format_stacktrace_entry(stacktrace_entry) :: String.t()
def format_stacktrace_entry(entry)
# From Macro.Env.stacktrace
def format_stacktrace_entry({module, :__MODULE__, 0, location}) do
format_location(location) <> inspect(module) <> " (module)"
end
# From :elixir_compiler_*
def format_stacktrace_entry({_module, :__MODULE__, 1, location}) do
format_location(location) <> "(module)"
end
# From :elixir_compiler_*
def format_stacktrace_entry({_module, :__FILE__, 1, location}) do
format_location(location) <> "(file)"
end
def format_stacktrace_entry({module, fun, arity, location}) do
format_application(module) <> format_location(location) <> format_mfa(module, fun, arity)
end
def format_stacktrace_entry({fun, arity, location}) do
format_location(location) <> format_fa(fun, arity)
end
defp format_application(module) do
# We cannot use Application due to bootstrap issues
case :application.get_application(module) do
{:ok, app} ->
case :application.get_key(app, :vsn) do
{:ok, vsn} when is_list(vsn) ->
"(" <> Atom.to_string(app) <> " " <> List.to_string(vsn) <> ") "
_ ->
"(" <> Atom.to_string(app) <> ") "
end
:undefined ->
""
end
end
@doc """
Formats the stacktrace.
A stacktrace must be given as an argument. If not, the stacktrace
is retrieved from `Process.info/2`.
"""
def format_stacktrace(trace \\ nil) do
trace =
if trace do
trace
else
case Process.info(self(), :current_stacktrace) do
{:current_stacktrace, t} -> Enum.drop(t, 3)
end
end
case trace do
[] -> "\n"
_ -> " " <> Enum.map_join(trace, "\n ", &format_stacktrace_entry(&1)) <> "\n"
end
end
@doc """
Receives an anonymous function and arity and formats it as
shown in stacktraces. The arity may also be a list of arguments.
## Examples
Exception.format_fa(fn -> nil end, 1)
#=> "#Function<...>/1"
"""
def format_fa(fun, arity) when is_function(fun) do
"#{inspect(fun)}#{format_arity(arity)}"
end
@doc """
Receives a module, fun and arity and formats it
as shown in stacktraces. The arity may also be a list
of arguments.
## Examples
iex> Exception.format_mfa(Foo, :bar, 1)
"Foo.bar/1"
iex> Exception.format_mfa(Foo, :bar, [])
"Foo.bar()"
iex> Exception.format_mfa(nil, :bar, [])
"nil.bar()"
Anonymous functions are reported as -func/arity-anonfn-count-,
where func is the name of the enclosing function. Convert to
"anonymous fn in func/arity"
"""
def format_mfa(module, fun, arity) when is_atom(module) and is_atom(fun) do
case Code.Identifier.extract_anonymous_fun_parent(fun) do
{outer_name, outer_arity} ->
"anonymous fn#{format_arity(arity)} in " <>
"#{Code.Identifier.inspect_as_atom(module)}." <>
"#{Code.Identifier.inspect_as_function(outer_name)}/#{outer_arity}"
:error ->
"#{Code.Identifier.inspect_as_atom(module)}." <>
"#{Code.Identifier.inspect_as_function(fun)}#{format_arity(arity)}"
end
end
defp format_arity(arity) when is_list(arity) do
inspected = for x <- arity, do: inspect(x)
"(#{Enum.join(inspected, ", ")})"
end
defp format_arity(arity) when is_integer(arity) do
"/" <> Integer.to_string(arity)
end
@doc """
Formats the given `file` and `line` as shown in stacktraces.
If any of the values are `nil`, they are omitted.
## Examples
iex> Exception.format_file_line("foo", 1)
"foo:1:"
iex> Exception.format_file_line("foo", nil)
"foo:"
iex> Exception.format_file_line(nil, nil)
""
"""
def format_file_line(file, line, suffix \\ "") do
if file do
if line && line != 0 do
"#{file}:#{line}:#{suffix}"
else
"#{file}:#{suffix}"
end
else
""
end
end
defp format_location(opts) when is_list(opts) do
format_file_line(Keyword.get(opts, :file), Keyword.get(opts, :line), " ")
end
end
# Some exceptions implement "message/1" instead of "exception/1" mostly
# for bootstrap reasons. It is recommended for applications to implement
# "exception/1" instead of "message/1" as described in "defexception/1"
# docs.
defmodule RuntimeError do
defexception message: "runtime error"
end
defmodule ArgumentError do
defexception message: "argument error"
@impl true
def blame(
%{message: "argument error"} = exception,
[{:erlang, :apply, [module, function, args], _} | _] = stacktrace
) do
message =
cond do
# Note that args may be an empty list even if they were supplied
not is_atom(module) and is_atom(function) and args == [] ->
"you attempted to apply #{inspect(function)} on #{inspect(module)}. " <>
"If you are using apply/3, make sure the module is an atom. " <>
"If you are using the dot syntax, such as map.field or module.function, " <>
"make sure the left side of the dot is an atom or a map"
not is_atom(module) ->
"you attempted to apply a function on #{inspect(module)}. " <>
"Modules (the first argument of apply) must always be an atom"
not is_atom(function) ->
"you attempted to apply #{inspect(function)} on module #{inspect(module)}. " <>
"Functions (the second argument of apply) must always be an atom"
not is_list(args) ->
"you attempted to apply #{inspect(function)} on module #{inspect(module)} " <>
"with arguments #{inspect(args)}. Arguments (the third argument of apply) must always be a list"
end
{%{exception | message: message}, stacktrace}
end
def blame(exception, stacktrace) do
{exception, stacktrace}
end
end
defmodule ArithmeticError do
defexception message: "bad argument in arithmetic expression"
@unary_ops [:+, :-]
@binary_ops [:+, :-, :*, :/]
@binary_funs [:div, :rem]
@bitwise_binary_funs [:band, :bor, :bxor, :bsl, :bsr]
@impl true
def blame(%{message: message} = exception, [{:erlang, fun, args, _} | _] = stacktrace) do
message =
message <>
case {fun, args} do
{op, [a]} when op in @unary_ops ->
": #{op}(#{inspect(a)})"
{op, [a, b]} when op in @binary_ops ->
": #{inspect(a)} #{op} #{inspect(b)}"
{fun, [a, b]} when fun in @binary_funs ->
": #{fun}(#{inspect(a)}, #{inspect(b)})"
{fun, [a, b]} when fun in @bitwise_binary_funs ->
": Bitwise.#{fun}(#{inspect(a)}, #{inspect(b)})"
{:bnot, [a]} ->
": Bitwise.bnot(#{inspect(a)})"
_ ->
""
end
{%{exception | message: message}, stacktrace}
end
def blame(exception, stacktrace) do
{exception, stacktrace}
end
end
defmodule SystemLimitError do
defexception []
@impl true
def message(_) do
"a system limit has been reached"
end
end
defmodule SyntaxError do
defexception [:file, :line, description: "syntax error"]
@impl true
def message(exception) do
Exception.format_file_line(Path.relative_to_cwd(exception.file), exception.line) <>
" " <> exception.description
end
end
defmodule TokenMissingError do
defexception [:file, :line, description: "expression is incomplete"]
@impl true
def message(%{file: file, line: line, description: description}) do
Exception.format_file_line(file && Path.relative_to_cwd(file), line) <> " " <> description
end
end
defmodule CompileError do
defexception [:file, :line, description: "compile error"]
@impl true
def message(%{file: file, line: line, description: description}) do
Exception.format_file_line(file && Path.relative_to_cwd(file), line) <> " " <> description
end
end
defmodule BadFunctionError do
defexception [:term]
@impl true
def message(%{term: term}) when is_function(term) do
"function #{inspect(term)} is invalid, likely because it points to an old version of the code"
end
def message(exception) do
"expected a function, got: #{inspect(exception.term)}"
end
end
defmodule BadStructError do
defexception [:struct, :term]
@impl true
def message(exception) do
"expected a struct named #{inspect(exception.struct)}, got: #{inspect(exception.term)}"
end
end
defmodule BadMapError do
defexception [:term]
@impl true
def message(exception) do
"expected a map, got: #{inspect(exception.term)}"
end
end
defmodule BadBooleanError do
defexception [:term, :operator]
@impl true
def message(exception) do
"expected a boolean on left-side of \"#{exception.operator}\", got: #{inspect(exception.term)}"
end
end
defmodule MatchError do
defexception [:term]
@impl true
def message(exception) do
"no match of right hand side value: #{inspect(exception.term)}"
end
end
defmodule CaseClauseError do
defexception [:term]
@impl true
def message(exception) do
"no case clause matching: #{inspect(exception.term)}"
end
end
defmodule WithClauseError do
defexception [:term]
@impl true
def message(exception) do
"no with clause matching: #{inspect(exception.term)}"
end
end
defmodule CondClauseError do
defexception []
@impl true
def message(_exception) do
"no cond clause evaluated to a truthy value"
end
end
defmodule TryClauseError do
defexception [:term]
@impl true
def message(exception) do
"no try clause matching: #{inspect(exception.term)}"
end
end
defmodule BadArityError do
defexception [:function, :args]
@impl true
def message(exception) do
fun = exception.function
args = exception.args
insp = Enum.map_join(args, ", ", &inspect/1)
{:arity, arity} = Function.info(fun, :arity)
"#{inspect(fun)} with arity #{arity} called with #{count(length(args), insp)}"
end
defp count(0, _insp), do: "no arguments"
defp count(1, insp), do: "1 argument (#{insp})"
defp count(x, insp), do: "#{x} arguments (#{insp})"
end
defmodule UndefinedFunctionError do
defexception [:module, :function, :arity, :reason, :message]
@impl true
def message(%{message: nil} = exception) do
%{reason: reason, module: module, function: function, arity: arity} = exception
{message, _loaded?} = message(reason, module, function, arity)
message
end
def message(%{message: message}) do
message
end
defp message(nil, module, function, arity) do
cond do
is_nil(function) or is_nil(arity) ->
{"undefined function", false}
is_nil(module) ->
formatted_fun = Exception.format_mfa(module, function, arity)
{"function #{formatted_fun} is undefined", false}
function_exported?(module, :module_info, 0) ->
message(:"function not exported", module, function, arity)
true ->
message(:"module could not be loaded", module, function, arity)
end
end
defp message(:"module could not be loaded", module, function, arity) do
formatted_fun = Exception.format_mfa(module, function, arity)
{"function #{formatted_fun} is undefined (module #{inspect(module)} is not available)", false}
end
defp message(:"function not exported", module, function, arity) do
formatted_fun = Exception.format_mfa(module, function, arity)
{"function #{formatted_fun} is undefined or private", true}
end
defp message(reason, module, function, arity) do
formatted_fun = Exception.format_mfa(module, function, arity)
{"function #{formatted_fun} is undefined (#{reason})", false}
end
@impl true
def blame(exception, stacktrace) do
%{reason: reason, module: module, function: function, arity: arity} = exception
{message, loaded?} = message(reason, module, function, arity)
message = message <> hint(module, function, arity, loaded?)
{%{exception | message: message}, stacktrace}
end
defp hint(nil, _function, 0, _loaded?) do
". If you are using the dot syntax, such as map.field or module.function, " <>
"make sure the left side of the dot is an atom or a map"
end
defp hint(module, function, arity, true) do
behaviour_hint(module, function, arity) <>
hint_for_loaded_module(module, function, arity, nil)
end
defp hint(_module, _function, _arity, _loaded?) do
""
end
@doc false
def hint_for_loaded_module(module, function, arity, exports) do
if macro_exported?(module, function, arity) do
". However there is a macro with the same name and arity. " <>
"Be sure to require #{inspect(module)} if you intend to invoke this macro"
else
IO.iodata_to_binary(did_you_mean(module, function, exports))
end
end
@function_threshold 0.77
@max_suggestions 5
defp did_you_mean(module, function, exports) do
exports = exports || exports_for(module)
result =
case Keyword.take(exports, [function]) do
[] ->
candidates = exports -- deprecated_functions_for(module)
base = Atom.to_string(function)
for {key, val} <- candidates,
dist = String.jaro_distance(base, Atom.to_string(key)),
dist >= @function_threshold,
do: {dist, key, val}
arities ->
for {key, val} <- arities, do: {1.0, key, val}
end
|> Enum.sort(&(elem(&1, 0) >= elem(&2, 0)))
|> Enum.take(@max_suggestions)
|> Enum.sort(&(elem(&1, 1) <= elem(&2, 1)))
case result do