Better format invalid values when logging them

This commit is contained in:
Evgeniy Khramtsov 2018-07-08 20:42:53 +03:00
parent 68d12017cc
commit 8c796ed027
3 changed files with 27 additions and 11 deletions

View File

@ -1078,14 +1078,15 @@ validate_opts(#state{opts = Opts} = State, ModOpts) ->
NewVal ->
In#local_config{value = NewVal}
catch {invalid_syntax, Error} ->
?ERROR_MSG("Invalid value '~p' for "
"option '~s': ~s",
[Val, Opt, Error]),
?ERROR_MSG("Invalid value for "
"option '~s' (~s): ~s",
[Opt, Error,
misc:format_val(Val)]),
erlang:error(invalid_option);
_:_ ->
?ERROR_MSG("Invalid value '~p' for "
"option '~s'",
[Val, Opt]),
?ERROR_MSG("Invalid value for "
"option '~s': ~s",
[Opt, misc:format_val(Val)]),
erlang:error(invalid_option)
end;
_ ->

View File

@ -546,12 +546,14 @@ validate_opts(Host, Module, Opts0) ->
[Module, Opt]),
module_error(ErrTxt);
_:{invalid_option, Opt, Val} ->
ErrTxt = io_lib:format("Invalid value '~p' for option '~s' of "
"module '~s'", [Val, Opt, Module]),
ErrTxt = io_lib:format("Invalid value for option '~s' of "
"module ~s: ~s",
[Opt, Module, misc:format_val(Val)]),
module_error(ErrTxt);
_:{invalid_option, Opt, Val, Reason} ->
ErrTxt = io_lib:format("Invalid value '~p' for option '~s' of "
"module '~s': ~s", [Val, Opt, Module, Reason]),
ErrTxt = io_lib:format("Invalid value for option '~s' of "
"module ~s (~s): ~s",
[Opt, Module, Reason, misc:format_val(Val)]),
module_error(ErrTxt);
_:{unknown_option, Opt, []} ->
ErrTxt = io_lib:format("Unknown option '~s' of module '~s': "

View File

@ -35,7 +35,8 @@
now_to_usec/1, usec_to_now/1, encode_pid/1, decode_pid/2,
compile_exprs/2, join_atoms/2, try_read_file/1, get_descr/2,
css_dir/0, img_dir/0, js_dir/0, msgs_dir/0, sql_dir/0,
read_css/1, read_img/1, read_js/1, try_url/1, intersection/2]).
read_css/1, read_img/1, read_js/1, try_url/1, intersection/2,
format_val/1]).
%% Deprecated functions
-export([decode_base64/1, encode_base64/1]).
@ -287,6 +288,18 @@ intersection(L1, L2) ->
lists:member(E, L2)
end, L1).
-spec format_val(any()) -> iodata().
format_val(I) when is_integer(I) ->
integer_to_list(I);
format_val(S) when is_binary(S) ->
S;
format_val(B) when is_atom(B) ->
erlang:atom_to_binary(B, utf8);
format_val(YAML) ->
try [io_lib:nl(), fast_yaml:encode(YAML)]
catch _:_ -> io_lib:format("~p", [YAML])
end.
%%%===================================================================
%%% Internal functions
%%%===================================================================