Skip to content

Commit 0a7c86b

Browse files
authored
Fix the exception logged by Cowboy caused by double reply (#13612)
Issue introduced in 383ddb1. Authored-by: @lhoguin.
1 parent 28a407c commit 0a7c86b

File tree

4 files changed

+41
-49
lines changed

4 files changed

+41
-49
lines changed

deps/rabbitmq_management/src/rabbit_mgmt_util.erl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151

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

54+
-export([set_resp_not_found/2]).
55+
5456
-import(rabbit_misc, [pget/2]).
5557

5658
-include("rabbit_mgmt.hrl").
@@ -1178,3 +1180,18 @@ catch_no_such_user_or_vhost(Fun, Replacement) ->
11781180
%% error is thrown when the request is out of range
11791181
sublist(List, S, L) when is_integer(L), L >= 0 ->
11801182
lists:sublist(lists:nthtail(S-1, List), L).
1183+
1184+
-spec set_resp_not_found(binary(), cowboy_req:req()) -> cowboy_req:req().
1185+
set_resp_not_found(NotFoundBin, ReqData) ->
1186+
ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of
1187+
not_found ->
1188+
<<"vhost_not_found">>;
1189+
_ ->
1190+
NotFoundBin
1191+
end,
1192+
ReqData1 = cowboy_req:set_resp_header(
1193+
<<"content-type">>, <<"application/json">>, ReqData),
1194+
cowboy_req:set_resp_body(rabbit_json:encode(#{
1195+
<<"error">> => <<"not_found">>,
1196+
<<"reason">> => ErrorMessage
1197+
}), ReqData1).

deps/rabbitmq_management/src/rabbit_mgmt_wm_exchange_publish.erl

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ allowed_methods(ReqData, Context) ->
2929
content_types_provided(ReqData, Context) ->
3030
{rabbit_mgmt_util:responder_map(to_json), ReqData, Context}.
3131

32-
resource_exists(ReqData, Context) ->
33-
{case rabbit_mgmt_wm_exchange:exchange(ReqData) of
34-
not_found -> raise_not_found(ReqData, Context);
35-
_ -> true
36-
end, ReqData, Context}.
32+
resource_exists(ReqData0, Context) ->
33+
case rabbit_mgmt_wm_exchange:exchange(ReqData0) of
34+
not_found ->
35+
ReqData1 = rabbit_mgmt_util:set_resp_not_found(<<"exchange_not_found">>, ReqData0),
36+
{false, ReqData1, Context};
37+
_ ->
38+
{true, ReqData0, Context}
39+
end.
3740

3841
allow_missing_post(ReqData, Context) ->
3942
{false, ReqData, Context}.
@@ -104,18 +107,6 @@ bad({{coordinator_unavailable, _}, _}, ReqData, Context) ->
104107
is_authorized(ReqData, Context) ->
105108
rabbit_mgmt_util:is_authorized_vhost(ReqData, Context).
106109

107-
raise_not_found(ReqData, Context) ->
108-
ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of
109-
not_found ->
110-
"vhost_not_found";
111-
_ ->
112-
"exchange_not_found"
113-
end,
114-
rabbit_mgmt_util:not_found(
115-
rabbit_data_coercion:to_binary(ErrorMessage),
116-
ReqData,
117-
Context).
118-
119110
%%--------------------------------------------------------------------
120111

121112
decode(Payload, <<"string">>) -> Payload;

deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_actions.erl

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ variances(Req, Context) ->
2525
allowed_methods(ReqData, Context) ->
2626
{[<<"POST">>, <<"OPTIONS">>], ReqData, Context}.
2727

28-
resource_exists(ReqData, Context) ->
29-
{case rabbit_mgmt_wm_queue:queue(ReqData) of
30-
not_found -> raise_not_found(ReqData, Context);
31-
_ -> true
32-
end, ReqData, Context}.
28+
resource_exists(ReqData0, Context) ->
29+
case rabbit_mgmt_wm_queue:queue(ReqData0) of
30+
not_found ->
31+
ReqData1 = rabbit_mgmt_util:set_resp_not_found(<<"queue_not_found">>, ReqData0),
32+
{false, ReqData1, Context};
33+
_ ->
34+
{true, ReqData0, Context}
35+
end.
3336

3437
allow_missing_post(ReqData, Context) ->
3538
{false, ReqData, Context}.
@@ -54,17 +57,6 @@ do_it(ReqData0, Context) ->
5457
is_authorized(ReqData, Context) ->
5558
rabbit_mgmt_util:is_authorized_admin(ReqData, Context).
5659

57-
raise_not_found(ReqData, Context) ->
58-
ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of
59-
not_found ->
60-
"vhost_not_found";
61-
_ ->
62-
"queue_not_found"
63-
end,
64-
rabbit_mgmt_util:not_found(
65-
rabbit_data_coercion:to_binary(ErrorMessage),
66-
ReqData,
67-
Context).
6860
%%--------------------------------------------------------------------
6961

7062
action(Else, _Q, ReqData, Context) ->

deps/rabbitmq_management/src/rabbit_mgmt_wm_queue_get.erl

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ allowed_methods(ReqData, Context) ->
2929
content_types_provided(ReqData, Context) ->
3030
{rabbit_mgmt_util:responder_map(to_json), ReqData, Context}.
3131

32-
resource_exists(ReqData, Context) ->
33-
{case rabbit_mgmt_wm_queue:queue(ReqData) of
34-
not_found -> raise_not_found(ReqData, Context);
35-
_ -> true
36-
end, ReqData, Context}.
32+
resource_exists(ReqData0, Context) ->
33+
case rabbit_mgmt_wm_queue:queue(ReqData0) of
34+
not_found ->
35+
ReqData1 = rabbit_mgmt_util:set_resp_not_found(<<"queue_not_found">>, ReqData0),
36+
{false, ReqData1, Context};
37+
_ ->
38+
{true, ReqData0, Context}
39+
end.
3740

3841
allow_missing_post(ReqData, Context) ->
3942
{false, ReqData, Context}.
@@ -152,17 +155,6 @@ basic_get(Ch, Q, AckMode, Enc, Trunc) ->
152155
is_authorized(ReqData, Context) ->
153156
rabbit_mgmt_util:is_authorized_vhost(ReqData, Context).
154157

155-
raise_not_found(ReqData, Context) ->
156-
ErrorMessage = case rabbit_mgmt_util:vhost(ReqData) of
157-
not_found ->
158-
"vhost_not_found";
159-
_ ->
160-
"queue_not_found"
161-
end,
162-
rabbit_mgmt_util:not_found(
163-
rabbit_data_coercion:to_binary(ErrorMessage),
164-
ReqData,
165-
Context).
166158
%%--------------------------------------------------------------------
167159

168160
maybe_truncate(Payload, none) -> Payload;

0 commit comments

Comments
 (0)