Skip to content

Commit

Permalink
Update error handling to generate binaries instead of lists
Browse files Browse the repository at this point in the history
  • Loading branch information
saleyn committed Oct 28, 2023
1 parent edcb9ab commit 27d42fb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 7 additions & 5 deletions src/str.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
%%% ```
%%% str(Fmt, Args) -> lists:flatten(io_lib:format(Fmt, Args))
%%% bin(Fmt, Args) -> list_to_binary(lists:flatten(io_lib:format(Fmt, Args)))
%%% throw(Fmt, Args) -> erlang:throw(lists:flatten(io_lib:format(Fmt, Args))
%%% error(Fmt, Args) -> erlang:error(lists:flatten(io_lib:format(Fmt, Args))
%%% throw(Fmt, Args) -> erlang:throw(list_to_binary(lists:flatten(io_lib:format(Fmt, Args)))
%%% error(Fmt, Args) -> erlang:error(list_to_binary(lists:flatten(io_lib:format(Fmt, Args)))
%%% i2l(Int) -> integer_to_list(Int) % Enabled with compiled with
%%% % the `{d,str_i2l}' option
%%% b2l(Bin) -> binary_to_list(Bin) % Enabled with compiled with
Expand Down Expand Up @@ -168,12 +168,14 @@ update4(Arg, Node, Line, _Opt) when Arg==str; Arg==bin ->
Node
end;
update4(I, Node, Line, _Opt) when I==throw; I==error ->
%% Replace throw(A, B) -> throw(lists:flatten(io_lib:format(A, B))).
%% Replace error(A, B) -> error(lists:flatten(io_lib:format(A, B))).
%% Replace throw(A, B) -> throw(list_to_binary(lists:flatten(io_lib:format(A, B)))).
%% Replace error(A, B) -> error(list_to_binary(lists:flatten(io_lib:format(A, B)))).
put(line, Line),
case erl_syntax:application_arguments(Node) of
[A,B] ->
syn_call(I, [syn_call(lists, flatten, [syn_call(io_lib, format, [A,B])])]);
syn_call(I, [syn_call(erlang, list_to_binary,
[syn_call(lists, flatten, [
syn_call(io_lib, format, [A,B])])])]);
_ ->
Node
end;
Expand Down
8 changes: 4 additions & 4 deletions test/str_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ str_test() ->
?assertEqual(<<"{a,1}">>, bin({a, 1})).

throw_test() ->
?assertEqual("Test: 1", try throw("Test: ~w", [1]) catch throw:E -> E end),
?assertEqual(ok, try throw(ok) catch throw:E -> E end).
?assertEqual(<<"Test: 1">>, try throw("Test: ~w", [1]) catch throw:E -> E end),
?assertEqual(ok, try throw(ok) catch throw:E -> E end).

error_test() ->
?assertEqual("Test: 1", try error("Test: ~w", [1]) catch error:E -> E end),
?assertEqual(ok, try error(ok) catch error:E -> E end).
?assertEqual(<<"Test: 1">>, try error("Test: ~w", [1]) catch error:E -> E end),
?assertEqual(ok, try error(ok) catch error:E -> E end).

-endif.

0 comments on commit 27d42fb

Please sign in to comment.