[erlang-questions] Exceptions and gen_server

Roberto Aloi roberto.aloi@REDACTED
Fri Nov 6 17:42:13 CET 2009


Hi Anton,

I usually do the following:

foo(Params) ->
    case gen_server:call(?MODULE, {foo, Params}) of
        {error, Reason} ->
            erlang:error({Reason, Params}); % I return params to
understand what's going on...
        {ok, Value} ->
            Value
    end.

handle_call({create, Name, Opts}, _From, State) ->
 ...

Regards,

Roberto Aloi
Erlang Training and Consulting Ltd.
http://www.erlang-consulting.com

Anton Krasovsky wrote:
> What would be the standard way of returning/throwing an exception in a
> result to a gen_server:call?
>
> Would something like that be reasonable:
>
> a() ->
> case gen_server:call(..., a) of
>     {ok, Value} -> Value;
>     {error, Error} -> throw({error, Error})
> end
>
> and
>
> handle_call(a, ...) ->
> case ... of
>     {ok, Value} -> {reply, {ok, Value} , State};
>     {error, Error} -> {reply, {error, Error} , State}
> end
>
> Regards,
> Anton
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>   



More information about the erlang-questions mailing list