Skip to content

Fix the errors logged by Cowboy caused by double reply in a few endpoints #13612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions deps/rabbitmq_management/src/rabbit_mgmt_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

-export([disable_stats/1, enable_queue_totals/1]).

-export([set_resp_not_found/2]).

-import(rabbit_misc, [pget/2]).

-include("rabbit_mgmt.hrl").
Expand Down Expand Up @@ -1178,3 +1180,18 @@ catch_no_such_user_or_vhost(Fun, Replacement) ->
%% error is thrown when the request is out of range
sublist(List, S, L) when is_integer(L), L >= 0 ->
lists:sublist(lists:nthtail(S-1, List), L).

-spec set_resp_not_found(binary(), cowboy_req:req()) -> cowboy_req:req().
set_resp_not_found(NotFoundBin, ReqData) ->
ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of
not_found ->
<<"vhost_not_found">>;
_ ->
NotFoundBin
end,
ReqData1 = cowboy_req:set_resp_header(
<<"content-type">>, <<"application/json">>, ReqData),
cowboy_req:set_resp_body(rabbit_json:encode(#{
<<"error">> => <<"not_found">>,
<<"reason">> => ErrorMessage
}), ReqData1).
25 changes: 8 additions & 17 deletions deps/rabbitmq_management/src/rabbit_mgmt_wm_exchange_publish.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ allowed_methods(ReqData, Context) ->
content_types_provided(ReqData, Context) ->
{rabbit_mgmt_util:responder_map(to_json), ReqData, Context}.

resource_exists(ReqData, Context) ->
{case rabbit_mgmt_wm_exchange:exchange(ReqData) of
not_found -> raise_not_found(ReqData, Context);
_ -> true
end, ReqData, Context}.
resource_exists(ReqData0, Context) ->
case rabbit_mgmt_wm_exchange:exchange(ReqData0) of
not_found ->
ReqData1 = rabbit_mgmt_util:set_resp_not_found(<<"exchange_not_found">>, ReqData0),
{false, ReqData1, Context};
_ ->
{true, ReqData0, Context}
end.

allow_missing_post(ReqData, Context) ->
{false, ReqData, Context}.
Expand Down Expand Up @@ -104,18 +107,6 @@ bad({{coordinator_unavailable, _}, _}, ReqData, Context) ->
is_authorized(ReqData, Context) ->
rabbit_mgmt_util:is_authorized_vhost(ReqData, Context).

raise_not_found(ReqData, Context) ->
ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of
not_found ->
"vhost_not_found";
_ ->
"exchange_not_found"
end,
rabbit_mgmt_util:not_found(
rabbit_data_coercion:to_binary(ErrorMessage),
ReqData,
Context).

%%--------------------------------------------------------------------

decode(Payload, <<"string">>) -> Payload;
Expand Down
24 changes: 8 additions & 16 deletions deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_actions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ variances(Req, Context) ->
allowed_methods(ReqData, Context) ->
{[<<"POST">>, <<"OPTIONS">>], ReqData, Context}.

resource_exists(ReqData, Context) ->
{case rabbit_mgmt_wm_queue:queue(ReqData) of
not_found -> raise_not_found(ReqData, Context);
_ -> true
end, ReqData, Context}.
resource_exists(ReqData0, Context) ->
case rabbit_mgmt_wm_queue:queue(ReqData0) of
not_found ->
ReqData1 = rabbit_mgmt_util:set_resp_not_found(<<"queue_not_found">>, ReqData0),
{false, ReqData1, Context};
_ ->
{true, ReqData0, Context}
end.

allow_missing_post(ReqData, Context) ->
{false, ReqData, Context}.
Expand All @@ -54,17 +57,6 @@ do_it(ReqData0, Context) ->
is_authorized(ReqData, Context) ->
rabbit_mgmt_util:is_authorized_admin(ReqData, Context).

raise_not_found(ReqData, Context) ->
ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of
not_found ->
"vhost_not_found";
_ ->
"queue_not_found"
end,
rabbit_mgmt_util:not_found(
rabbit_data_coercion:to_binary(ErrorMessage),
ReqData,
Context).
%%--------------------------------------------------------------------

action(Else, _Q, ReqData, Context) ->
Expand Down
24 changes: 8 additions & 16 deletions deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_get.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ allowed_methods(ReqData, Context) ->
content_types_provided(ReqData, Context) ->
{rabbit_mgmt_util:responder_map(to_json), ReqData, Context}.

resource_exists(ReqData, Context) ->
{case rabbit_mgmt_wm_queue:queue(ReqData) of
not_found -> raise_not_found(ReqData, Context);
_ -> true
end, ReqData, Context}.
resource_exists(ReqData0, Context) ->
case rabbit_mgmt_wm_queue:queue(ReqData0) of
not_found ->
ReqData1 = rabbit_mgmt_util:set_resp_not_found(<<"queue_not_found">>, ReqData0),
{false, ReqData1, Context};
_ ->
{true, ReqData0, Context}
end.

allow_missing_post(ReqData, Context) ->
{false, ReqData, Context}.
Expand Down Expand Up @@ -152,17 +155,6 @@ basic_get(Ch, Q, AckMode, Enc, Trunc) ->
is_authorized(ReqData, Context) ->
rabbit_mgmt_util:is_authorized_vhost(ReqData, Context).

raise_not_found(ReqData, Context) ->
ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of
not_found ->
"vhost_not_found";
_ ->
"queue_not_found"
end,
rabbit_mgmt_util:not_found(
rabbit_data_coercion:to_binary(ErrorMessage),
ReqData,
Context).
%%--------------------------------------------------------------------

maybe_truncate(Payload, none) -> Payload;
Expand Down