Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Don't use try/catch to handle missing plugins
COUCHDB-2796
  • Loading branch information
iilyak committed Sep 2, 2015
1 parent 7f32e4f commit 36b2be55a89f766df44b05d988d040e6a3125529
Showing 4 changed files with 51 additions and 60 deletions.
@@ -126,7 +126,7 @@ safe_set(Hash, #state{} = State) ->
key = Key} = State,
try
Data = get_from_module(Module),
OldData = current(Handle, Subscriber),
OldData = couch_epi_data_gen:current_data(Handle, Subscriber),
ok = couch_epi_data_gen:set(Handle, Subscriber, Data),
couch_epi_server:notify(Subscriber, Key, {data, OldData}, {data, Data}),
{ok, State#state{hash = Hash}}
@@ -135,21 +135,7 @@ safe_set(Hash, #state{} = State) ->
end.

get_from_module(Module) ->
try
Module:data()
catch
error:undef -> []
end.

current(Handle, Subscriber) ->
try
case couch_epi_data_gen:by_source(Handle, Subscriber) of
undefined -> [];
Data -> Data
end
catch error:undef ->
[]
end.
Module:data().

maybe_start_keeper(Key) ->
Handle = couch_epi_data_gen:get_handle(Key),
@@ -24,6 +24,7 @@
-export([keys/1, subscribers/1]).

-export([save/3]).
-export([current_data/2]).

set(Handle, Source, Data) ->
case is_updated(Handle, Source, Data) of
@@ -174,32 +175,43 @@ module_name({Service, Key}) when is_list(Service) andalso is_list(Key) ->

is_updated(Handle, Source, Data) ->
Sig = couch_epi_util:hash(Data),
try Handle:version(Source) of
{error, {unknown, Source}} -> true;
{error, Reason} -> throw(Reason);
Sig -> false;
_ -> true
catch
error:undef -> true;
Class:Reason ->
throw({Class, {Source, Reason}})
end.
if_exists(Handle, version, 1, true, fun() ->
try Handle:version(Source) of
{error, {unknown, Source}} -> true;
{error, Reason} -> throw(Reason);
Sig -> false;
_ -> true
catch
Class:Reason ->
throw({Class, {Source, Reason}})
end
end).

save(Handle, undefined, []) ->
case get_current_data(Handle) of
case current_data(Handle) of
[] -> generate(Handle, []);
_Else -> ok
end;
save(Handle, Source, Data) ->
CurrentData = get_current_data(Handle),
CurrentData = current_data(Handle),
NewDefs = lists:keystore(Source, 1, CurrentData, {Source, Data}),
generate(Handle, NewDefs).

get_current_data(Handle) ->
try Handle:by_source()
catch error:undef -> []
end.
current_data(Handle, Subscriber) ->
if_exists(Handle, by_source, 1, [], fun() ->
Handle:by_source(Subscriber)
end).

current_data(Handle) ->
if_exists(Handle, by_source, 0, [], fun() ->
Handle:by_source()
end).

if_exists(Handle, Func, Arity, Default, Fun) ->
case erlang:function_exported(Handle, Func, Arity) of
true -> Fun();
false -> Default
end.

defined_keys(Defs) ->
Keys = fold_defs(Defs, [], fun({_Source, Key, _Data}, Acc) ->
@@ -161,7 +161,7 @@ safe_set(Hash, Data, #state{} = State) ->
key = Key} = State,

try
OldData = current(Handle, Subscriber),
OldData = couch_epi_data_gen:current_data(Handle, Subscriber),
ok = couch_epi_data_gen:set(Handle, Subscriber, Data),
couch_epi_server:notify(Subscriber, Key, {data, OldData}, {data, Data}),
{ok, State#state{hash = Hash}}
@@ -181,16 +181,6 @@ hash_of_file(FilePath) ->
{ok, Data} = file:read_file(FilePath),
couch_epi_util:md5(Data).

current(Handle, Subscriber) ->
try
case couch_epi_data_gen:by_source(Handle, Subscriber) of
undefined -> [];
Data -> Data
end
catch error:undef ->
[]
end.

maybe_start_keeper(Key) ->
Handle = couch_epi_data_gen:get_handle(Key),
couch_epi_module_keeper:maybe_start_keeper(couch_epi_data_gen, Handle).
@@ -190,16 +190,17 @@ module_name(ServiceId) when is_list(ServiceId) ->

is_updated(Handle, Source, Modules) ->
Sig = hash(Modules),
try Handle:version(Source) of
{error, {unknown, Source}} -> true;
{error, Reason} -> throw(Reason);
Sig -> false;
_ -> true
catch
error:undef -> true;
Class:Reason ->
throw({Class, {Source, Reason}})
end.
if_exists(Handle, version, 1, true, fun() ->
try Handle:version(Source) of
{error, {unknown, Source}} -> true;
{error, Reason} -> throw(Reason);
Sig -> false;
_ -> true
catch
Class:Reason ->
throw({Class, {Source, Reason}})
end
end).

save(Handle, undefined, []) ->
case get_current_definitions(Handle) of
@@ -218,8 +219,14 @@ definitions(Source, Modules) ->
{Source, SrcDefs}.

get_current_definitions(Handle) ->
try Handle:definitions()
catch error:undef -> []
if_exists(Handle, definitions, 0, [], fun() ->
Handle:definitions()
end).

if_exists(Handle, Func, Arity, Default, Fun) ->
case erlang:function_exported(Handle, Func, Arity) of
true -> Fun();
false -> Default
end.

defined_providers(Defs) ->
@@ -300,11 +307,7 @@ parse_opts([], Acc) ->
Acc.

providers(Handle, Function, Arity, #opts{ignore_providers = true}) ->
try
Handle:providers(Function, Arity)
catch
error:undef -> []
end;
Handle:providers(Function, Arity);
providers(Handle, Function, Arity, #opts{}) ->
Handle:providers(Function, Arity).

0 comments on commit 36b2be5

Please sign in to comment.