Skip to content

Commit

Permalink
chore: add more details in error context
Browse files Browse the repository at this point in the history
  • Loading branch information
zmstone committed Oct 17, 2023
1 parent bf72afa commit 510d0ad
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/kpro_api_vsn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@

-type range() :: {kpro:vsn(), kpro:vsn()}.

%% @doc Return supported version range of the given API.
%%
%% Majority of the APIs are supported from version 0 up to the
%% latest version when the bnf files are re-generated.
%% With two exceptions.
%%
%% 1. Do not support version 0-1 for offset_commit:
%% version 0: Kafka commits offsets to zookeeper
%% version 1: Thre is a lack of commit retention.
%%
%% 2. Do not support offset_fetch version 0.
%% Version 0: Kafka fetches offsets from zookeeper.
-spec range(kpro:api()) -> false | range().
range(offset_commit) -> {2, 2};
range(offset_fetch) -> {1, 2};
Expand Down
26 changes: 24 additions & 2 deletions src/kpro_brokers.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%

-module(kpro_brokers).

-export([ connect_any/2
Expand All @@ -27,6 +26,7 @@
]).

-include("kpro_private.hrl").
-include_lib("eunit/include/eunit.hrl").

-type endpoint() :: kpro:endpoint().
-type topic() :: kpro:topic().
Expand Down Expand Up @@ -288,7 +288,17 @@ api_vsn_range_intersection(Vsns) ->

%% Intersect received api version range with supported range.
api_vsn_range_intersection(API, Received) ->
kpro_api_vsn:intersect(kpro_api_vsn:range(API), Received).
Expected = kpro_api_vsn:range(API),
try
kpro_api_vsn:intersect(Expected, Received)
catch
error : {no_intersection, _, _} ->
Reason = #{reason => incompatible_version_ranges,
expected => Expected,
received => Received,
api => API},
erlang:error(Reason)
end.

connect_any([], _Config, Errors) ->
{error, lists:reverse(Errors)};
Expand All @@ -306,6 +316,18 @@ random_order(L) ->
RI = lists:sort(lists:zip(RandL, L)),
[I || {_R, I} <- RI].

-ifdef(TEST).

api_vsn_range_intersection_test() ->
API = offset_commit,
Received = {0, 0},
?assertError(#{api := API,
reason := incompatible_version_ranges,
expected := _,
received := Received},
api_vsn_range_intersection(API, Received)).

-endif.
%%%_* Emacs ====================================================================
%%% Local Variables:
%%% allout-layout: t
Expand Down

0 comments on commit 510d0ad

Please sign in to comment.