Skip to content

Commit 0a46932

Browse files
author
Vlad Ionescu
committed
using #amqp_error{} instead of {amqp, ...}
1 parent 5d2bd75 commit 0a46932

File tree

5 files changed

+49
-42
lines changed

5 files changed

+49
-42
lines changed

include/rabbit.hrl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
-record(ssl_socket, {tcp, ssl}).
6868
-record(delivery, {mandatory, immediate, txn, sender, message}).
6969

70+
-record(amqp_error, {name, expl = "", method = none}).
71+
7072
%%----------------------------------------------------------------------------
7173

7274
-ifdef(use_specs).
@@ -154,7 +156,10 @@
154156
port :: non_neg_integer()}).
155157
-type(not_found() :: {'error', 'not_found'}).
156158
-type(routing_result() :: 'routed' | 'unroutable' | 'not_delivered').
157-
159+
-type(amqp_error() ::
160+
#amqp_error{name :: atom(),
161+
expl :: string(),
162+
method :: atom()}).
158163
-endif.
159164

160165
%%----------------------------------------------------------------------------

include/rabbit_framing_spec.hrl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
%% TODO: make this more precise
5151
-type(amqp_method_name() :: atom()).
5252
-type(channel_number() :: non_neg_integer()).
53-
%% TODO: make this more precise
54-
-type(amqp_error() :: {bool(), non_neg_integer(), binary()}).
5553
-type(resource_name() :: binary()).
5654
-type(routing_key() :: binary()).
5755
-type(username() :: binary()).

src/rabbit_channel.erl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ handle_cast({method, Method, Content}, State) ->
125125
stop ->
126126
{stop, normal, State#ch{state = terminating}}
127127
catch
128-
exit:{amqp, Error, Explanation, none} ->
128+
exit:Reason = #amqp_error{} ->
129129
ok = rollback_and_notify(State),
130-
Reason = {amqp, Error, Explanation,
131-
rabbit_misc:method_record_type(Method)},
132-
State#ch.reader_pid ! {channel_exit, State#ch.channel, Reason},
130+
CompleteReason = Reason#amqp_error{method =
131+
rabbit_misc:method_record_type(Method)},
132+
State#ch.reader_pid !
133+
{channel_exit, State#ch.channel, CompleteReason},
133134
{stop, normal, State#ch{state = terminating}};
134135
exit:normal ->
135136
{stop, normal, State};

src/rabbit_misc.erl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
-include_lib("kernel/include/file.hrl").
3636

3737
-export([method_record_type/1, polite_pause/0, polite_pause/1]).
38-
-export([die/1, frame_error/2, protocol_error/3, protocol_error/4]).
38+
-export([die/1, frame_error/2, amqp_error/4, protocol_error/3, protocol_error/4]).
3939
-export([not_found/1]).
4040
-export([get_config/1, get_config/2, set_config/2]).
4141
-export([dirty_read/1]).
@@ -74,10 +74,9 @@
7474
-spec(polite_pause/1 :: (non_neg_integer()) -> 'done').
7575
-spec(die/1 :: (atom()) -> no_return()).
7676
-spec(frame_error/2 :: (atom(), binary()) -> no_return()).
77-
-spec(protocol_error/3 ::
78-
(atom() | amqp_error(), string(), [any()]) -> no_return()).
79-
-spec(protocol_error/4 ::
80-
(atom() | amqp_error(), string(), [any()], atom()) -> no_return()).
77+
-spec(amqp_error/4 :: (atom(), string(), [any()], atom()) -> amqp_error()).
78+
-spec(protocol_error/3 :: (atom(), string(), [any()]) -> no_return()).
79+
-spec(protocol_error/4 :: (atom(), string(), [any()], atom()) -> no_return()).
8180
-spec(not_found/1 :: (r(atom())) -> no_return()).
8281
-spec(get_config/1 :: (atom()) -> {'ok', any()} | not_found()).
8382
-spec(get_config/2 :: (atom(), A) -> A).
@@ -144,15 +143,17 @@ die(Error) ->
144143
protocol_error(Error, "~w", [Error]).
145144

146145
frame_error(MethodName, BinaryFields) ->
147-
protocol_error(frame_error, "cannot decode ~w",
148-
[BinaryFields], MethodName).
146+
protocol_error(frame_error, "cannot decode ~w", [BinaryFields], MethodName).
149147

150-
protocol_error(Error, Explanation, Params) ->
151-
protocol_error(Error, Explanation, Params, none).
148+
amqp_error(Name, Explanation, Params, Method) ->
149+
#amqp_error{name = Name,
150+
expl = lists:flatten(io_lib:format(Explanation, Params)),
151+
method = Method}.
152152

153-
protocol_error(Error, Explanation, Params, Method) ->
154-
CompleteExplanation = lists:flatten(io_lib:format(Explanation, Params)),
155-
exit({amqp, Error, CompleteExplanation, Method}).
153+
protocol_error(Name, Explanation, Params) ->
154+
protocol_error(Name, Explanation, Params, none).
155+
protocol_error(Name, Explanation, Params, Method) ->
156+
exit(amqp_error(Name, Explanation, Params, Method)).
156157

157158
not_found(R) -> protocol_error(not_found, "no ~s", [rs(R)]).
158159

src/rabbit_reader.erl

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,10 @@ mainloop(Parent, Deb, State = #v1{sock= Sock, recv_ref = Ref}) ->
269269
throw({inet_error, Reason});
270270
{'EXIT', Parent, Reason} ->
271271
if State#v1.connection_state =:= running ->
272-
send_exception(
273-
State, 0,
274-
{amqp, connection_forced,
275-
io_lib:format(
276-
"broker forced connection closure with reason '~w'",
277-
[Reason]), none});
272+
send_exception(State, 0,
273+
rabbit_misc:amqp_error(connection_forced,
274+
"broker forced connection closure with reason '~w'",
275+
[Reason], none));
278276
true -> ok
279277
end,
280278
%% this is what we are expected to do according to
@@ -567,12 +565,13 @@ handle_method0(MethodName, FieldsBin, State) ->
567565
MethodName, FieldsBin),
568566
State)
569567
catch exit:Reason ->
570-
CompleteReason =
571-
case Reason of
572-
{amqp, Error, Explanation, none} ->
573-
{amqp, Error, Explanation, MethodName};
574-
OtherReason -> OtherReason
575-
end,
568+
CompleteReason =
569+
case Reason of
570+
#amqp_error{method = none} ->
571+
Reason#amqp_error{method = MethodName};
572+
_ ->
573+
Reason
574+
end,
576575
case State#v1.connection_state of
577576
running -> send_exception(State, 0, CompleteReason);
578577
Other -> throw({channel0_error, Other, CompleteReason})
@@ -793,18 +792,21 @@ map_exception(Channel, Reason) ->
793792
end,
794793
{ShouldClose, CloseChannel, CloseMethod}.
795794

796-
lookup_amqp_exception({amqp, {ShouldClose, Code, Text}, Expl, Method}) ->
795+
lookup_amqp_exception(#amqp_error{name = Name,
796+
expl = Expl,
797+
method = Method}) ->
798+
{ShouldClose, Code, Text} = rabbit_framing:lookup_amqp_exception(Name),
797799
ExplBin = list_to_binary(Expl),
798800
CompleteTextBin = <<Text/binary, " - ", ExplBin/binary>>,
799-
SafeTextBin = if size(CompleteTextBin) > 255 ->
800-
<<CompleteTextBin:252/binary, "...">>;
801-
true ->
802-
CompleteTextBin
803-
end,
804-
{ShouldClose, Code, SafeTextBin, Method};
805-
lookup_amqp_exception({amqp, ErrorName, Expl, Method}) ->
806-
Details = rabbit_framing:lookup_amqp_exception(ErrorName),
807-
lookup_amqp_exception({amqp, Details, Expl, Method});
801+
SafeTextBin =
802+
if
803+
size(CompleteTextBin) > 255 ->
804+
<<CompleteTextBin:252/binary, "...">>;
805+
true ->
806+
CompleteTextBin
807+
end,
808+
{ShouldClose, Code, SafeTextBin, Method};
809+
808810
lookup_amqp_exception(Other) ->
809811
rabbit_log:warning("Non-AMQP exit reason '~p'~n", [Other]),
810-
{true, ?INTERNAL_ERROR, <<"INTERNAL_ERROR">>, none}.
812+
lookup_amqp_exception(#amqp_error{name = internal_error}).

0 commit comments

Comments
 (0)