[erlang-questions] net_kernel:handle_call

Vance Shipley <>
Sun Feb 21 23:13:52 CET 2010


On Fri, Feb 19, 2010 at 11:23:08AM -0500, Musumeci, Antonio S wrote:
}  While not directly comparable... in other languages when you ask 
}  for an invalid service (method, function, etc) the caller not the 
}  called errors.

The proper thing to do when a server receives an incorrect
call depends on the relationship between the server and the
client.

The Erlang way of handling programming errors is to fail
immediately and leave a stack trace pointing directly at
the problem so that someone can fix the code.  So if your
client and server are two parts of a single program you would
want to have no clause to handle an unknown call.

On the other hand if the server repesents a service access
point and the client is a user of that service from another
program you would want to guard against it's errors.  I use
the following:

    %% @spec (SAP, Request) -> ok
    %%    SAP = Name | {Name,Node} | {global,GlobalName} | pid()
    %%    Node = atom()
    %%    GlobalName = term()
    %%    Request = term()
    %%
    %% @doc Public API function for a service request.
    %%
    call(SAP, Request) ->
         case gen_server:call(SAP, Request) of
             {ok, Result} ->
                 Result;
             {error, Reason} ->
                 exit(Reason)
         end.

    %% @spec (Request, From, State) -> Result
    %%     Request = term()
    %%     From = {pid(), Tag}
    %%     State = term()
    %% @doc Private callback to handle a request sent with call/2.
    %%
    handle_call(Request, _, State) ->
        ...
        {reply, {ok, Result}, State};
    handle_call(Other, _, State) ->
        {reply, {error, badarg}, State}.


I have in the past used something like this:

    handle_call(Other, {Pid, _Tag}, State) ->
        exit(Pid, badarg),
        {noreply, State}.

... however I've come to find the former more appropriate.

-- 
	-Vance


More information about the erlang-questions mailing list