Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify semantics of OK.try rescue clause #42

Open
nietaki opened this issue Jan 19, 2018 · 5 comments
Open

Clarify semantics of OK.try rescue clause #42

nietaki opened this issue Jan 19, 2018 · 5 comments

Comments

@nietaki
Copy link

nietaki commented Jan 19, 2018

I think it's worth it to make it clearer in the docs what happens in the "unhappy path" in OK.try - the wrapped errors get unwrapped and "rescued", but non-tagged errors cause a runtime error.

For example the first test case will succeed, but the second one will fail:

  test "OK.try with wrapped error" do
    res = OK.try do
      foo <- {:error, :foo}
    after
      :unwrapped_success
    rescue
      :foo -> :got_foo
    end

    assert res == :got_foo
  end

  test "OK.try with unwrapped error" do
    bar = fn -> :bar end

    res = OK.try do
      bar <- bar.() # will cause runtime error!
    after
      :unwrapped_success
    rescue
      :bar -> :got_bar
    end

    assert res == :got_bar
  end

For more clarity it might be worth adding that safe_div/2 used in all examples returns {:ok, integer()} | {:error, atom()}, so in the division by zero case it will return {:error, :zero_division}, not :zero_division

@CrowdHailer
Copy link
Owner

Always approve more clarity in the docs 🐳

@CrowdHailer
Copy link
Owner

Did you want to add anything to the docs for this?

@walkr
Copy link

walkr commented Sep 17, 2018

I just saw this library in Elixir Radar, and I also found the "rescued" part confusing. @CrowdHailer I think you should've picked a different word, as people might get confused as to whether the "rescue" part will capture runtime errors and other exceptional errors as the regular try/rescue does. Perhaps:

OK.try do
after
except
end

would've been clearer.

That's just me. Other than this the library seems cool!

@CrowdHailer
Copy link
Owner

@walkr rescue has to be chosen from one of the reserved words that can be used in do/end blocks https://github.com/elixir-lang/elixir/blob/master/lib/elixir/pages/Syntax%20Reference.md

However if there is anything you think could be clearer as a beginner to the library please do open a PR. even one sentence in the docs might help someone.

I'm quite along way from having by beginners hat on with this library so am probably not the best person to do it.

@walkr
Copy link

walkr commented Sep 17, 2018

@CrowdHailer Ah okay, got it! Thanks for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants