[erlang-questions] throwing from gen_server:init/1
Mazen Harake
mazen.harake@REDACTED
Fri Nov 25 16:53:30 CET 2011
Why are you doing the validation inside the gen_server?
Perhaps it is better to do it outside (I'm assuming you have a module
API which has a function to start the server?)
In there you can validate your options and return {ok, Pid} or {error,
"Option not supported"} or what ever appropriate.
Assuming that the gen_server has proper options before it starts feels
more robust and proper design since your gen_server *shouldn't* start
if it doesn't have the options for it.
I'm reserving myself to unknown details in your code which makes this
a non-obvious choice :)
On 23 November 2011 16:23, Raimo Niskanen
<raimo+erlang-questions@REDACTED> wrote:
> On Wed, Nov 23, 2011 at 03:05:30PM +0000, Joel Reymont wrote:
>>
>> On Nov 23, 2011, at 2:55 PM, Dmitry Demeshchuk wrote:
>>
>> > Can be a single try-catch instead. As far as I remember, sasl won't
>> > show you proper error message if an exception occurs in init/1.
>>
>>
>> Something like this then?
>
> Since you are doing a deep return I think using class 'error'
> is not appropriate, use 'throw' insted. See below.
>
>>
>> 1> Opts1 = [{'foo', 1}, {'bar', 2}].
>> [{foo,1},{bar,2}]
>>
>> 3> Opts2 = [{'foo', self()}, {'bar', 2}].
>> [{foo,<0.34.0>},{bar,2}]
>>
>> 4> Opts3 = [{'foo', self()}, {'bar', 'baz'}].
>> [{foo,<0.34.0>},{bar,baz}]
>>
>> 5> x:check(Opts1).
>> {error,{invalid_option,foo}}
>>
>> 6> x:check(Opts2).
>> {error,{invalid_option,bar}}
>>
>> 7> x:check(Opts3).
>> {ok,{state,<0.34.0>,baz}}
>>
>> --- x.erl
>>
>> -module(x).
>>
>> -compile([export_all]).
>>
>> -record(state, {
>> foo :: pid(),
>> bar :: atom()
>> }).
>>
>> -spec check(proplists:proplist()) -> {'ok', #state{}} | {'error', _}.
>>
>> check(Opts) ->
>> try
>> Foo = ensure('foo', Opts, fun is_pid/1),
>> Bar = ensure('bar', Opts, fun is_atom/1),
>> {'ok', #state{ foo = Foo, bar = Bar }}
>> catch
>> error:X ->
> %% Class 'throw' is default
> X ->
>> {'error', X}
>> end.
>>
>> ensure(Key, Opts, Guard)
>> when is_atom(Key),
>> is_list(Opts),
>> is_function(Guard) ->
>> case proplists:get_value(Key, Opts) of
>> 'undefined' ->
>> error({'missing_option', Key});
> throw({'missing_option', Key});
>> Value ->
>> case Guard(Value) of
>> 'true' -> Value;
>> _ -> error({'invalid_option', Key})
> _ -> throw({'invalid_option', Key})
>> end
>> end.
>>
>>
>> --------------------------------------------------------------------------
>> - for hire: mac osx device driver ninja, kernel extensions and usb drivers
>> ---------------------+------------+---------------------------------------
>> http://wagerlabs.com | @wagerlabs | http://www.linkedin.com/in/joelreymont
>> ---------------------+------------+---------------------------------------
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>
> --
>
> / Raimo Niskanen, Erlang/OTP, Ericsson AB
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
More information about the erlang-questions
mailing list