Skip to content

Commit b345193

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.
1 parent 0397035 commit b345193

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

deps/rabbit/src/rabbit_khepri.erl

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ wait_for_leader(Timeout, Retries) ->
306306
throw(Reason)
307307
end.
308308

309+
-spec wait_for_register_projections() -> Ret when
310+
Ret :: ok | no_return().
311+
309312
wait_for_register_projections() ->
310313
wait_for_register_projections(retry_timeout(), retry_limit()).
311314

@@ -314,10 +317,10 @@ wait_for_register_projections(_Timeout, 0) ->
314317
wait_for_register_projections(Timeout, Retries) ->
315318
rabbit_log:info("Waiting for Khepri projections for ~tp ms, ~tp retries left",
316319
[Timeout, Retries - 1]),
317-
try
318-
register_projections()
319-
catch
320-
throw : timeout ->
320+
case register_projections() of
321+
ok ->
322+
ok;
323+
{error, timeout} ->
321324
wait_for_register_projections(Timeout, Retries -1)
322325
end.
323326

@@ -1091,6 +1094,9 @@ if_has_data_wildcard() ->
10911094
if_has_data(Conditions) ->
10921095
#if_all{conditions = Conditions ++ [#if_has_data{has_data = true}]}.
10931096

1097+
-spec register_projections() -> Ret when
1098+
Ret :: ok | timeout_error().
1099+
10941100
register_projections() ->
10951101
RegisterFuns = [fun register_rabbit_exchange_projection/0,
10961102
fun register_rabbit_queue_projection/0,
@@ -1101,20 +1107,23 @@ register_projections() ->
11011107
fun register_rabbit_bindings_projection/0,
11021108
fun register_rabbit_index_route_projection/0,
11031109
fun register_rabbit_topic_graph_projection/0],
1104-
[case RegisterFun() of
1105-
ok ->
1106-
ok;
1107-
%% Before Khepri v0.13.0, `khepri:register_projection/1,2,3` would
1108-
%% return `{error, exists}` for projections which already exist.
1109-
{error, exists} ->
1110-
ok;
1111-
%% In v0.13.0+, Khepri returns a `?khepri_error(..)` instead.
1112-
{error, {khepri, projection_already_exists, _Info}} ->
1113-
ok;
1114-
{error, Error} ->
1115-
throw(Error)
1116-
end || RegisterFun <- RegisterFuns],
1117-
ok.
1110+
rabbit_misc:for_each_while_ok(
1111+
fun(RegisterFun) ->
1112+
case RegisterFun() of
1113+
ok ->
1114+
ok;
1115+
%% Before Khepri v0.13.0, `khepri:register_projection/1,2,3`
1116+
%% would return `{error, exists}` for projections which
1117+
%% already exist.
1118+
{error, exists} ->
1119+
ok;
1120+
%% In v0.13.0+, Khepri returns a `?khepri_error(..)` instead.
1121+
{error, {khepri, projection_already_exists, _Info}} ->
1122+
ok;
1123+
{error, _} = Err ->
1124+
Err
1125+
end
1126+
end, RegisterFuns).
11181127

11191128
register_rabbit_exchange_projection() ->
11201129
Name = rabbit_khepri_exchange,

0 commit comments

Comments
 (0)