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

Documentation on early return from gen_server callbacks using throw #5450

Open
sanzor opened this issue Nov 26, 2021 · 2 comments · May be fixed by #6053
Open

Documentation on early return from gen_server callbacks using throw #5450

sanzor opened this issue Nov 26, 2021 · 2 comments · May be fixed by #6053
Labels
enhancement help wanted Issue not worked on by OTP; help wanted from the community team:PS Assigned to OTP team PS

Comments

@sanzor
Copy link

sanzor commented Nov 26, 2021

Could you please add documentation on how to early return from gen_server callbacks using throw ? I found this to be a neat way when dealing with multiple checks before actually processing a request 👍

-module(some_module).
-behaviour(gen_server).
-export([handle_call/3]).

handle_call(Request,From,State)->
   check1(Request,State),
   check2(Request,State),
   do_call(Request,From,State).

do_call(some_request,From,State)->
   {reply,{ok,some_ok_result},State};
do_call(some_other_request ,From,State)->
   {reply,{ok,some_ok_result},State};
do_call(Request,From,State)->
   {reply,{ok,3,State}.

check1(Request,State)->
   case condition1() of
        1 -> ok;
       WrongResult ->throw({reply,some_wrong_result,State);
   end.
    

check2(Request,State)
  case condition2() of
        3 -> ok;
       WrongResult ->throw({reply,some_other_wrong_result,State);
   end.


In the above code i have to do multiple checks on any request i am receiving before actually processing the request.
Throw comes in handy to do some sort of pipeline where each check condition fails fast (stops the flow) and returns without crashing the server.
I am throwing with a suitable reply in each of my checks.

Could you please add documentation to this , since i find it very very useful .

@rickard-green rickard-green added the team:PS Assigned to OTP team PS label Nov 29, 2021
@RaimoNiskanen
Copy link
Contributor

Pull requests are accepted, also for documentation, and are, for documentation, fairly easily approved.

In the documentation for gen_statem I think there is an explicit mention somewhere about using throw/1 to return from a callback...

@RaimoNiskanen RaimoNiskanen added the help wanted Issue not worked on by OTP; help wanted from the community label Dec 17, 2021
@bucko909
Copy link
Contributor

bucko909 commented Jun 6, 2022

Proposal in #6053. Not sure how to get them properly linked.

@RaimoNiskanen RaimoNiskanen linked a pull request Jun 9, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement help wanted Issue not worked on by OTP; help wanted from the community team:PS Assigned to OTP team PS
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants