Skip to content

Commit 9414701

Browse files
committed
Fix exception classes in estdlib
Replace all erroneous uses of throw with calls to `error/1`. Also update etest to properly test for exception classes, thus asserting the compatibility with Erlang/OTP Also remove duplicate etest.hrl header Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent 010a480 commit 9414701

File tree

12 files changed

+153
-202
lines changed

12 files changed

+153
-202
lines changed

libs/estdlib/src/erlang.erl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ apply(Module, Function, Args) ->
319319
[Arg1, Arg2, Arg3, Arg4, Arg5, Arg6] ->
320320
Module:Function(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6);
321321
_ ->
322-
throw(badarg)
322+
error(badarg)
323323
end.
324324

325325
%%-----------------------------------------------------------------------------
@@ -337,10 +337,9 @@ is_map(_Map) ->
337337
%%-----------------------------------------------------------------------------
338338
%% @param Map the map
339339
%% @returns the size of the map
340-
%% @throws {badmap, Map}
341340
%% @doc Returns the size of (i.e., the number of entries in) the map
342341
%%
343-
%% This function throws a `{badmap, Map}' exception if `Map' is not a map.
342+
%% This function raises a `{badmap, Map}' error if `Map' is not a map.
344343
%%
345344
%% This function may be used in a guard expression.
346345
%% @end
@@ -353,11 +352,10 @@ map_size(_Map) ->
353352
%% @param Key the key to get
354353
%% @param Map the map from which to get the value
355354
%% @returns the value in `Map' associated with `Key', if it exists.
356-
%% @throws {badkey, Key} | {badmap, Map}
357355
%% @doc Get the value in `Map' associated with `Key', if it exists.
358356
%%
359-
%% This function throws a `{badkey, Key}' exception if 'Key' does not occur in `Map' or
360-
%% a `{badmap, Map}' if `Map' is not a map.
357+
%% This function raises a `{badkey, Key}' error if 'Key' does not occur in
358+
%% `Map' or a `{badmap, Map}' if `Map' is not a map.
361359
%%
362360
%% This function may be used in a guard expression.
363361
%% @end
@@ -370,10 +368,9 @@ map_get(_Key, _Map) ->
370368
%% @param Key the key
371369
%% @param Map the map
372370
%% @returns `true' if `Key' is associated with a value in `Map'; `false', otherwise.
373-
%% @throws {badmap, Map}
374371
%% @doc Return `true' if `Key' is associated with a value in `Map'; `false', otherwise.
375372
%%
376-
%% This function throws a `{badmap, Map}' exception if `Map' is not a map.
373+
%% This function raises a `{badmap, Map}' error if `Map' is not a map.
377374
%%
378375
%% This function may be used in a guard expression.
379376
%% @end

libs/estdlib/src/io_lib.erl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ format(Format, Args) ->
4747
true ->
4848
interleave(FormatTokens, Instr, Args, []);
4949
false ->
50-
throw(badarg)
50+
error(badarg)
5151
end.
5252

5353
%%
@@ -145,7 +145,7 @@ parse_format_control([$+ | Rest], Format) -> {Format#format{control = '+'}, Rest
145145
parse_format_control([$e | Rest], Format) -> {Format#format{control = e}, Rest};
146146
parse_format_control([$f | Rest], Format) -> {Format#format{control = f}, Rest};
147147
parse_format_control([$g | Rest], Format) -> {Format#format{control = g}, Rest};
148-
parse_format_control(_String, _Format) -> throw({badarg, _String}).
148+
parse_format_control(_String, _Format) -> error({badarg, _String}).
149149

150150
%% @private
151151
parse_integer([$- | Tail]) ->
@@ -251,8 +251,8 @@ format_spw(#format{control = Control, mod = undefined}, T) when is_binary(T) ->
251251
format_spw(#format{control = s, mod = Mod}, L) when is_list(L) ->
252252
Flatten = lists:flatten(L),
253253
case {Mod, test_string_class(Flatten)} of
254-
{_, not_a_string} -> throw(badarg);
255-
{undefined, unicode} -> throw(badarg);
254+
{_, not_a_string} -> error(badarg);
255+
{undefined, unicode} -> error(badarg);
256256
{_, _} -> Flatten
257257
end;
258258
format_spw(#format{control = p} = Format, L) when is_list(L) ->
@@ -263,7 +263,7 @@ format_spw(#format{control = p} = Format, L) when is_list(L) ->
263263
format_spw(#format{control = w} = Format, L) when is_list(L) ->
264264
[$[, lists:join($,, [format_spw(Format, E) || E <- L]), $]];
265265
format_spw(#format{control = s}, _) ->
266-
throw(badarg);
266+
error(badarg);
267267
format_spw(_Format, T) when is_integer(T) ->
268268
erlang:integer_to_list(T);
269269
format_spw(_Format, T) when is_float(T) ->
@@ -317,7 +317,7 @@ format_integer(#format{control = 'B', precision = Base}, T) when is_integer(T) -
317317
format_integer(#format{control = b, precision = Base}, T) when is_integer(T) ->
318318
string:to_lower(integer_to_list(T, Base));
319319
format_integer(_Format, _) ->
320-
throw(badarg).
320+
error(badarg).
321321

322322
%% @private
323323
format_float(#format{control = f, precision = undefined}, T) when is_float(T) ->
@@ -341,7 +341,7 @@ format_float(#format{control = C, precision = Precision}, T) when
341341
->
342342
format_scientific(T, Precision, 0);
343343
format_float(_Format, _) ->
344-
throw(badarg).
344+
error(badarg).
345345

346346
%% @private
347347
format_scientific(T, Precision, E) when (T < 1 andalso T > 0) orelse (T > -1 andalso T < 0) ->
@@ -366,7 +366,7 @@ format_char(#format{precision = Precision} = Format, T) when Precision =/= undef
366366
[Ch] = format_char(Format#format{field_width = undefined, precision = undefined}, T),
367367
lists:duplicate(Precision, Ch);
368368
format_char(_, _) ->
369-
throw(badarg).
369+
error(badarg).
370370

371371
%% @private
372372
%% String classes:

libs/estdlib/src/lists.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ seq(From, To, Incr) when
461461
(To > (From - Incr) andalso Incr < 0) orelse
462462
(Incr =:= 0 andalso From =/= To)
463463
->
464-
throw(badarg);
464+
error(badarg);
465465
seq(To, To, 0) ->
466466
[To];
467467
seq(From, To, Incr) ->

libs/estdlib/src/logger.erl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,7 @@ validate_level(Level) when
544544
Level == info orelse
545545
Level == debug
546546
->
547-
Level;
548-
validate_level(Level) ->
549-
throw({unsupported_level, Level}).
547+
Level.
550548

551549
%% @private
552550
to_int_level(emergency) ->

0 commit comments

Comments
 (0)