Skip to content

Commit ffec9f7

Browse files
committed
rabbit_khepri: Avoid throws in register_projection/0
Previously this function threw errors. With this minor refactor we return them instead so that `register_projection/0` is easier for callers to work with. (In the child commit we will add another caller.)
1 parent 0c44d31 commit ffec9f7

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

deps/rabbit/src/rabbit_khepri.erl

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,10 @@ wait_for_register_projections(_Timeout, 0) ->
301301
wait_for_register_projections(Timeout, Retries) ->
302302
rabbit_log:info("Waiting for Khepri projections for ~tp ms, ~tp retries left",
303303
[Timeout, Retries - 1]),
304-
try
305-
register_projections()
306-
catch
307-
throw : timeout ->
304+
case register_projections() of
305+
ok ->
306+
ok;
307+
{error, timeout} ->
308308
wait_for_register_projections(Timeout, Retries -1)
309309
end.
310310

@@ -1116,20 +1116,23 @@ register_projections() ->
11161116
fun register_rabbit_bindings_projection/0,
11171117
fun register_rabbit_index_route_projection/0,
11181118
fun register_rabbit_topic_graph_projection/0],
1119-
[case RegisterFun() of
1120-
ok ->
1121-
ok;
1122-
%% Before Khepri v0.13.0, `khepri:register_projection/1,2,3` would
1123-
%% return `{error, exists}` for projections which already exist.
1124-
{error, exists} ->
1125-
ok;
1126-
%% In v0.13.0+, Khepri returns a `?khepri_error(..)` instead.
1127-
{error, {khepri, projection_already_exists, _Info}} ->
1128-
ok;
1129-
{error, Error} ->
1130-
throw(Error)
1131-
end || RegisterFun <- RegFuns],
1132-
ok.
1119+
rabbit_misc:for_each_while_ok(
1120+
fun(RegisterFun) ->
1121+
case RegisterFun() of
1122+
ok ->
1123+
ok;
1124+
%% Before Khepri v0.13.0, `khepri:register_projection/1,2,3`
1125+
%% would return `{error, exists}` for projections which
1126+
%% already exist.
1127+
{error, exists} ->
1128+
ok;
1129+
%% In v0.13.0+, Khepri returns a `?khepri_error(..)` instead.
1130+
{error, {khepri, projection_already_exists, _Info}} ->
1131+
ok;
1132+
{error, _} = Error ->
1133+
Error
1134+
end
1135+
end, RegFuns).
11331136

11341137
register_rabbit_exchange_projection() ->
11351138
Name = rabbit_khepri_exchange,

0 commit comments

Comments
 (0)