ITADN

`defeffect` is discarding `rescue` and `catch` blocks

#6Closeddoorgan 创建于 2024-08-07
D
doorgancommented
Hi! Thanks for the great library 😄 I found an issue in `defeffect` while reviewing the code, and confirmed with a simple test: it's only taking the `:do` block of `defeffect` and ignoring any other block, mainly `rescue` and `catch` which are part of the implicit `try` block in `def` definitions. Here's a livebook with the reproduction for both `rescue` and `catch`: # Efx after/rescue issue ```elixir Mix.install([{:efx, "~> 0.2.3"}]) ExUnit.start() ``` ## Reproduction ```elixir defmodule EfxTest do use Efx @spec with_rescue() :: String.t() defeffect with_rescue() do raise "oh noes" rescue error -> error.message end @spec with_catch() :: String.t() defeffect with_catch() do throw("catch me") catch message -> message end end defmodule EfxTestTest do use ExUnit.Case test "returns oh noes" do assert EfxTest.with_rescue() == "oh noes" end test "returns catch me" do assert EfxTest.with_catch() == "catch me" end end ExUnit.run() ``` <!-- livebook:{"output":true} --> ``` 1) test returns catch me (EfxTestTest) #cell:wgdpdevelbkbrh5u:26 ** (throw) "catch me" stacktrace: #cell:wgdpdevelbkbrh5u:13: EfxTest.with_catch/0 #cell:wgdpdevelbkbrh5u:27: (test) 2) test returns oh noes (EfxTestTest) #cell:wgdpdevelbkbrh5u:22 ** (RuntimeError) oh noes stacktrace: #cell:wgdpdevelbkbrh5u:6: EfxTest.with_rescue/0 #cell:wgdpdevelbkbrh5u:23: (test) Finished in 0.00 seconds (0.00s async, 0.00s sync) 2 tests, 2 failures Randomized with seed 64798 ``` <!-- livebook:{"output":true} --> ``` %{total: 2, failures: 2, excluded: 0, skipped: 0} ``` ## Default behavior ```elixir defmodule NoEfxTest do @spec with_rescue() :: String.t() def with_rescue() do raise "oh noes" rescue error -> error.message end @spec with_catch() :: String.t() def with_catch() do throw("catch me") catch message -> message end end defmodule NoEfxTestTest do use ExUnit.Case test "returns oh noes" do assert NoEfxTest.with_rescue() == "oh noes" end test "returns catch me" do assert NoEfxTest.with_catch() == "catch me" end end ExUnit.run() ``` <!-- livebook:{"output":true} --> ``` .. Finished in 0.00 seconds (0.00s async, 0.00s sync) 2 tests, 0 failures Randomized with seed 64798 ``` <!-- livebook:{"output":true} --> ``` %{total: 2, failures: 0, excluded: 0, skipped: 0} ``` --- I think the issue is probably in these lines: https://github.com/bravobike/efx/blob/49ed4ad65ffa65d7f52a341ac5977533ed8a1427/lib/efx.ex#L236-L238
关闭于 2024-08-09 2 条评论