Skip to content

Commit

Permalink
Return size when package exceeds max size
Browse files Browse the repository at this point in the history
 - change error tuple in hex_tarball:create/2 to include an identifiable
 atom and size respective to compressed or uncompressed max size exceeded.
  • Loading branch information
starbelly committed Oct 19, 2020
1 parent bf1e304 commit ff9d6c7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
21 changes: 15 additions & 6 deletions src/hex_tarball.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ create(Metadata, Files, Config) ->

UncompressedSize = byte_size(ContentsTarball),

case(byte_size(Tarball) > TarballMaxSize) or (UncompressedSize > TarballMaxUncompressedSize) of
true ->
{error, {tarball, too_big}};
case {(byte_size(Tarball) > TarballMaxSize), (UncompressedSize > TarballMaxUncompressedSize)} of
{_, true} ->
{error, {tarball, {too_big_uncompressed, TarballMaxUncompressedSize}}};

false ->
{true, _} ->
{error, {tarball, {too_big_compressed, TarballMaxSize}}};

{false, false} ->
{ok, #{tarball => Tarball, outer_checksum => OuterChecksum, inner_checksum => InnerChecksum}}
end.

Expand Down Expand Up @@ -196,8 +199,11 @@ format_checksum(Checksum) ->
%% @doc
%% Converts an error reason term to a human-readable error message string.
-spec format_error(term()) -> string().
format_error({tarball, empty}) -> "empty tarball";
format_error({tarball, too_big}) -> "tarball is too big";
format_error({tarball, {too_big_uncompressed, Size}}) ->
io_lib:format("package exceeds max uncompressed size ~w ~s", [format_byte_size(Size), "MB"]);
format_error({tarball, {too_big_compressed, Size}}) ->
io_lib:format("package exceeds max compressed size ~w ~s", [format_byte_size(Size), "MB"]);

format_error({tarball, {missing_files, Files}}) -> io_lib:format("missing files: ~p", [Files]);
format_error({tarball, {bad_version, Vsn}}) -> io_lib:format("unsupported version: ~p", [Vsn]);
format_error({tarball, invalid_checksum}) -> "invalid tarball checksum";
Expand All @@ -214,6 +220,9 @@ format_error({checksum_mismatch, ExpectedChecksum, ActualChecksum}) ->
"Actual (base16-encoded): ~s",
[encode_base16(ExpectedChecksum), encode_base16(ActualChecksum)]).

format_byte_size(Size) ->
Size / 1000000.

%%====================================================================
%% Internal functions
%%====================================================================
Expand Down
4 changes: 3 additions & 1 deletion test/hex_tarball_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ too_big_to_create_test(_Config) ->
},
Contents = [{"src/foo.erl", <<"-module(foo).">>}],
Config = maps:put(tarball_max_size, 5100, hex_core:default_config()),
{error, {tarball, too_big}} = hex_tarball:create(Metadata, Contents, Config),
{error, {tarball, {too_big_compressed, 5100}}} = hex_tarball:create(Metadata, Contents, Config),
Config1 = maps:put(tarball_max_uncompressed_size, 100, hex_core:default_config()),
{error, {tarball,{too_big_uncompressed, 100}}} = hex_tarball:create(Metadata, Contents, Config1),
ok.

too_big_to_unpack_test(_Config) ->
Expand Down

0 comments on commit ff9d6c7

Please sign in to comment.