Improve error formatting

This commit is contained in:
Evgeny Khramtsov 2018-09-17 12:08:04 +03:00
parent bb9593dd12
commit dd888f90ec
3 changed files with 22 additions and 10 deletions

View File

@ -1081,12 +1081,12 @@ validate_opts(#state{opts = Opts} = State, ModOpts) ->
?ERROR_MSG("Invalid value for "
"option '~s' (~s): ~s",
[Opt, Error,
misc:format_val(Val)]),
misc:format_val({yaml, Val})]),
erlang:error(invalid_option);
_:_ ->
?ERROR_MSG("Invalid value for "
"option '~s': ~s",
[Opt, misc:format_val(Val)]),
[Opt, misc:format_val({yaml, Val})]),
erlang:error(invalid_option)
end;
_ ->

View File

@ -67,7 +67,7 @@
-type opts() :: [{atom(), any()}].
-type db_type() :: atom().
-callback start(binary(), opts()) -> ok | {ok, pid()}.
-callback start(binary(), opts()) -> ok | {ok, pid()} | {error, term()}.
-callback stop(binary()) -> any().
-callback reload(binary(), opts(), opts()) -> ok | {ok, pid()}.
-callback mod_opt_type(atom()) -> fun((term()) -> term()) | [atom()].
@ -548,12 +548,12 @@ validate_opts(Host, Module, Opts0) ->
_:{invalid_option, Opt, Val} ->
ErrTxt = io_lib:format("Invalid value for option '~s' of "
"module ~s: ~s",
[Opt, Module, misc:format_val(Val)]),
[Opt, Module, misc:format_val({yaml, Val})]),
module_error(ErrTxt);
_:{invalid_option, Opt, Val, Reason} ->
ErrTxt = io_lib:format("Invalid value for option '~s' of "
"module ~s (~s): ~s",
[Opt, Module, Reason, misc:format_val(Val)]),
[Opt, Module, Reason, misc:format_val({yaml, Val})]),
module_error(ErrTxt);
_:{unknown_option, Opt, []} ->
ErrTxt = io_lib:format("Unknown option '~s' of module '~s': "
@ -730,6 +730,9 @@ format_module_error(Module, Fun, Arity, Opts, Class, Reason, St) ->
"it doesn't export ~s/~B callback: "
"is it really an ejabberd module?",
[Fun, Module, Fun, Arity]);
{error, {bad_return, Module, {error, _} = Err}} ->
io_lib:format("Failed to ~s module ~s: ~s",
[Fun, Module, misc:format_val(Err)]);
{error, {bad_return, Module, Ret}} ->
io_lib:format("Module ~s returned unexpected value from ~s/~B:~n"
"** Error: ~p~n"

View File

@ -297,15 +297,24 @@ intersection(L1, L2) ->
end, L1).
-spec format_val(any()) -> iodata().
format_val({yaml, S}) when is_integer(S); is_binary(S); is_atom(S) ->
format_val(S);
format_val({yaml, YAML}) ->
S = try fast_yaml:encode(YAML)
catch _:_ -> YAML
end,
format_val(S);
format_val(I) when is_integer(I) ->
integer_to_list(I);
format_val(S) when is_binary(S) ->
<<$", S/binary, $">>;
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])
format_val(Term) ->
S = try iolist_to_binary(Term)
catch _:_ -> list_to_binary(io_lib:format("~p", [Term]))
end,
case binary:match(S, <<"\n">>) of
nomatch -> S;
_ -> [io_lib:nl(), S]
end.
-spec cancel_timer(reference()) -> ok.