Skip to content

Commit f040510

Browse files
the-mikedavismergify[bot]
authored andcommitted
rabbit_db_vhost: Bubble up database errors in delete/1
We need to bubble up the error through the caller `rabbit_vhost:delete/2`. The CLI calls `rabbit_vhost:delete/2` and already handles the `{error, timeout}` but the management UI needs an update so that an HTTP DELETE returns an error code when the deletion times out. (cherry picked from commit 8399450)
1 parent 0f82c30 commit f040510

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

deps/rabbit/src/rabbit_db_vhost.erl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,10 @@ with_fun_in_khepri_tx(VHostName, Thunk) ->
441441
%% delete().
442442
%% -------------------------------------------------------------------
443443

444-
-spec delete(VHostName) -> Existed when
444+
-spec delete(VHostName) -> Ret when
445445
VHostName :: vhost:name(),
446-
Existed :: boolean().
446+
Existed :: boolean(),
447+
Ret :: Existed | rabbit_khepri:timeout_error().
447448
%% @doc Deletes a virtual host record from the database.
448449
%%
449450
%% @returns a boolean indicating if the vhost existed or not. It throws an
@@ -470,7 +471,7 @@ delete_in_khepri(VHostName) ->
470471
case rabbit_khepri:delete_or_fail(Path) of
471472
ok -> true;
472473
{error, {node_not_found, _}} -> false;
473-
_ -> false
474+
{error, _} = Err -> Err
474475
end.
475476

476477
%% -------------------------------------------------------------------

deps/rabbit/src/rabbit_vhost.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ delete(VHost, ActingUser) ->
311311
[{name, VHost},
312312
{user_who_performed_action, ActingUser}]);
313313
false ->
314-
{error, {no_such_vhost, VHost}}
314+
{error, {no_such_vhost, VHost}};
315+
{error, _} = Err ->
316+
Err
315317
end,
316318
%% After vhost was deleted from the database, we try to stop vhost
317319
%% supervisors on all the nodes.

deps/rabbitmq_management/src/rabbit_mgmt_wm_vhost.erl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,22 @@ accept_content(ReqData0, Context = #context{user = #user{username = Username}})
9292

9393
delete_resource(ReqData, Context = #context{user = #user{username = Username}}) ->
9494
VHost = id(ReqData),
95-
_ = rabbit_vhost:delete(VHost, Username),
96-
{true, ReqData, Context}.
95+
case rabbit_vhost:delete(VHost, Username) of
96+
ok ->
97+
{true, ReqData, Context};
98+
{error, timeout} ->
99+
rabbit_mgmt_util:internal_server_error(
100+
timeout,
101+
"Timed out waiting for the vhost to be deleted",
102+
ReqData, Context);
103+
{error, E} ->
104+
Reason = iolist_to_binary(
105+
io_lib:format(
106+
"Error occurred while deleting vhost: ~tp",
107+
[E])),
108+
rabbit_mgmt_util:internal_server_error(
109+
Reason, ReqData, Context)
110+
end.
97111

98112
is_authorized(ReqData, Context) ->
99113
rabbit_mgmt_util:is_authorized_admin(ReqData, Context).

0 commit comments

Comments
 (0)