summaryrefslogtreecommitdiff
path: root/lib/elixir/test/elixir/kernel/raise_test.exs
blob: 29caaf248b750a74526feb4b1d83a3292fb45bc9 (plain)
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
Code.require_file("../test_helper.exs", __DIR__)

defmodule Kernel.RaiseTest do
  use ExUnit.Case, async: true

  # Silence warnings
  defp atom, do: RuntimeError
  defp binary, do: "message"
  defp opts, do: [message: "message"]
  defp struct, do: %RuntimeError{message: "message"}

  @trace [{:foo, :bar, 0, []}]

  test "raise message" do
    assert_raise RuntimeError, "message", fn ->
      raise "message"
    end

    assert_raise RuntimeError, "message", fn ->
      var = binary()
      raise var
    end
  end

  test "raise with no arguments" do
    assert_raise RuntimeError, fn ->
      raise RuntimeError
    end

    assert_raise RuntimeError, fn ->
      var = atom()
      raise var
    end
  end

  test "raise with arguments" do
    assert_raise RuntimeError, "message", fn ->
      raise RuntimeError, message: "message"
    end

    assert_raise RuntimeError, "message", fn ->
      atom = atom()
      opts = opts()
      raise atom, opts
    end
  end

  test "raise existing exception" do
    assert_raise RuntimeError, "message", fn ->
      raise %RuntimeError{message: "message"}
    end

    assert_raise RuntimeError, "message", fn ->
      var = struct()
      raise var
    end
  end

  test "reraise message" do
    try do
      reraise "message", @trace
      flunk("should not reach")
    rescue
      RuntimeError ->
        assert @trace == :erlang.get_stacktrace()
    end

    try do
      var = binary()
      reraise var, @trace
      flunk("should not reach")
    rescue
      RuntimeError ->
        assert @trace == :erlang.get_stacktrace()
    end
  end

  test "reraise with no arguments" do
    try do
      reraise RuntimeError, @trace
      flunk("should not reach")
    rescue
      RuntimeError ->
        assert @trace == :erlang.get_stacktrace()
    end

    try do
      var = atom()
      reraise var, @trace
      flunk("should not reach")
    rescue
      RuntimeError ->
        assert @trace == :erlang.get_stacktrace()
    end
  end

  test "reraise with arguments" do
    try do
      reraise RuntimeError, [message: "message"], @trace
      flunk("should not reach")
    rescue
      RuntimeError ->
        assert @trace == :erlang.get_stacktrace()
    end

    try do
      atom = atom()
      opts = opts()
      reraise atom, opts, @trace
      flunk("should not reach")
    rescue
      RuntimeError ->
        assert @trace == :erlang.get_stacktrace()
    end
  end

  test "reraise existing exception" do
    try do
      reraise %RuntimeError{message: "message"}, @trace
      flunk("should not reach")
    rescue
      RuntimeError ->
        assert @trace == :erlang.get_stacktrace()
    end

    try do
      var = struct()
      reraise var, @trace
      flunk("should not reach")
    rescue
      RuntimeError ->
        assert @trace == :erlang.get_stacktrace()
    end
  end

  describe "rescue" do
    test "runtime error" do
      result =
        try do
          raise "an exception"
        rescue
          RuntimeError -> true
        catch
          :error, _ -> false
        end

      assert result

      result =
        try do
          raise "an exception"
        rescue
          AnotherError -> true
        catch
          :error, _ -> false
        end

      refute result
    end

    test "named runtime error" do
      result =
        try do
          raise "an exception"
        rescue
          x in [RuntimeError] -> Exception.message(x)
        catch
          :error, _ -> false
        end

      assert result == "an exception"
    end

    test "with higher precedence than catch" do
      result =
        try do
          raise "an exception"
        rescue
          _ -> true
        catch
          _, _ -> false
        end

      assert result
    end

    test "argument error from erlang" do
      result =
        try do
          :erlang.error(:badarg)
        rescue
          ArgumentError -> true
        end

      assert result
    end

    test "argument error from elixir" do
      result =
        try do
          raise ArgumentError, ""
        rescue
          ArgumentError -> true
        end

      assert result
    end

    test "catch-all variable" do
      result =
        try do
          raise "an exception"
        rescue
          x -> Exception.message(x)
        end

      assert result == "an exception"
    end

    test "catch-all underscore" do
      result =
        try do
          raise "an exception"
        rescue
          _ -> true
        end

      assert result
    end

    test "catch-all unused variable" do
      result =
        try do
          raise "an exception"
        rescue
          _any -> true
        end

      assert result
    end

    test "catch-all with \"x in _\" syntax" do
      result =
        try do
          raise "an exception"
        rescue
          exception in _ ->
            Exception.message(exception)
        end

      assert result == "an exception"
    end
  end

  describe "normalize" do
    test "wrap custom Erlang error" do
      result =
        try do
          :erlang.error(:sample)
        rescue
          x in [ErlangError] -> Exception.message(x)
        end

      assert result == "Erlang error: :sample"
    end

    test "undefined function error" do
      result =
        try do
          DoNotExist.for_sure()
        rescue
          x in [UndefinedFunctionError] -> Exception.message(x)
        end

      assert result ==
               "function DoNotExist.for_sure/0 is undefined (module DoNotExist is not available)"
    end

    test "function clause error" do
      result =
        try do
          zero(1)
        rescue
          x in [FunctionClauseError] -> Exception.message(x)
        end

      assert result == "no function clause matching in Kernel.RaiseTest.zero/1"
    end

    test "badarg error" do
      result =
        try do
          :erlang.error(:badarg)
        rescue
          x in [ArgumentError] -> Exception.message(x)
        end

      assert result == "argument error"
    end

    test "tuple badarg error" do
      result =
        try do
          :erlang.error({:badarg, [1, 2, 3]})
        rescue
          x in [ArgumentError] -> Exception.message(x)
        end

      assert result == "argument error: [1, 2, 3]"
    end

    test "badarith error" do
      result =
        try do
          :erlang.error(:badarith)
        rescue
          x in [ArithmeticError] -> Exception.message(x)
        end

      assert result == "bad argument in arithmetic expression"
    end

    test "badarity error" do
      fun = fn x -> x end
      string = "#{inspect(fun)} with arity 1 called with 2 arguments (1, 2)"

      result =
        try do
          fun.(1, 2)
        rescue
          x in [BadArityError] -> Exception.message(x)
        end

      assert result == string
    end

    test "badfun error" do
      # Avoid "invalid function call" warning
      x = fn -> :example end

      result =
        try do
          x.().(2)
        rescue
          x in [BadFunctionError] -> Exception.message(x)
        end

      assert result == "expected a function, got: :example"
    end

    test "badmatch error" do
      x = :example

      result =
        try do
          ^x = zero(0)
        rescue
          x in [MatchError] -> Exception.message(x)
        end

      assert result == "no match of right hand side value: 0"
    end

    test "bad key error" do
      result =
        try do
          %{%{} | foo: :bar}
        rescue
          x in [KeyError] -> Exception.message(x)
        end

      assert result == "key :foo not found"

      result =
        try do
          %{}.foo
        rescue
          x in [KeyError] -> Exception.message(x)
        end

      assert result == "key :foo not found in: %{}"
    end

    test "bad map error" do
      result =
        try do
          %{zero(0) | foo: :bar}
        rescue
          x in [BadMapError] -> Exception.message(x)
        end

      assert result == "expected a map, got: 0"
    end

    test "bad boolean error" do
      result =
        try do
          1 and true
        rescue
          x in [BadBooleanError] -> Exception.message(x)
        end

      assert result == "expected a boolean on left-side of \"and\", got: 1"
    end

    test "case clause error" do
      x = :example

      result =
        try do
          case zero(0) do
            ^x -> nil
          end
        rescue
          x in [CaseClauseError] -> Exception.message(x)
        end

      assert result == "no case clause matching: 0"
    end

    test "cond clause error" do
      result =
        try do
          cond do
            !zero(0) -> :ok
          end
        rescue
          x in [CondClauseError] -> Exception.message(x)
        end

      assert result == "no cond clause evaluated to a true value"
    end

    test "try clause error" do
      f = fn -> :example end

      result =
        try do
          try do
            f.()
          else
            :other ->
              :ok
          end
        rescue
          x in [TryClauseError] -> Exception.message(x)
        end

      assert result == "no try clause matching: :example"
    end

    test "undefined function error as Erlang error" do
      result =
        try do
          DoNotExist.for_sure()
        rescue
          x in [ErlangError] -> Exception.message(x)
        end

      assert result ==
               "function DoNotExist.for_sure/0 is undefined (module DoNotExist is not available)"
    end
  end

  describe "__STACKTRACE__" do
    test "returns the stacktrace in catch" do
      try do
        throw(:foo)
      catch
        :foo ->
          assert __STACKTRACE__ == :erlang.get_stacktrace()
      end
    end

    test "returns the stracktrace in rescue" do
      try do
        raise "foo"
      rescue
        _ ->
          assert __STACKTRACE__ == :erlang.get_stacktrace()
      end
    end

    test "returns the right stacktrace in nested tries" do
      try do
        throw(:foo)
      catch
        :foo ->
          bar_trace =
            try do
              throw(:bar)
            catch
              :bar -> __STACKTRACE__
            end

          foo_trace = __STACKTRACE__
          assert bar_trace != foo_trace
      end
    end
  end

  defmacrop exceptions do
    [ErlangError]
  end

  test "with macros" do
    result =
      try do
        DoNotExist.for_sure()
      rescue
        x in exceptions() -> Exception.message(x)
      end

    assert result ==
             "function DoNotExist.for_sure/0 is undefined (module DoNotExist is not available)"
  end

  defp zero(0), do: 0
end