From dd888f90eccff9db963865bf138911d1644afc47 Mon Sep 17 00:00:00 2001 From: Evgeny Khramtsov Date: Mon, 17 Sep 2018 12:08:04 +0300 Subject: [PATCH] Improve error formatting --- src/ejabberd_config.erl | 4 ++-- src/gen_mod.erl | 9 ++++++--- src/misc.erl | 19 ++++++++++++++----- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/ejabberd_config.erl b/src/ejabberd_config.erl index 6146695c2..d1e7a2f45 100644 --- a/src/ejabberd_config.erl +++ b/src/ejabberd_config.erl @@ -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; _ -> diff --git a/src/gen_mod.erl b/src/gen_mod.erl index c15b50443..6575e1e82 100644 --- a/src/gen_mod.erl +++ b/src/gen_mod.erl @@ -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" diff --git a/src/misc.erl b/src/misc.erl index 5ef71c41b..c415e9817 100644 --- a/src/misc.erl +++ b/src/misc.erl @@ -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. -- 2.40.0