mirror of
https://github.com/processone/ejabberd.git
synced 2024-12-26 17:38:45 +01:00
Allow {mod_}opt_type to transform values passed to it, and for better error reporting
This commit is contained in:
parent
b2f53fb962
commit
4b9613e8fe
@ -770,22 +770,32 @@ add_option(Opt, Val) ->
|
|||||||
-spec prepare_opt_val(any(), any(), check_fun(), any()) -> any().
|
-spec prepare_opt_val(any(), any(), check_fun(), any()) -> any().
|
||||||
|
|
||||||
prepare_opt_val(Opt, Val, F, Default) ->
|
prepare_opt_val(Opt, Val, F, Default) ->
|
||||||
Res = case F of
|
Call = case F of
|
||||||
{Mod, Fun} ->
|
{Mod, Fun} ->
|
||||||
catch Mod:Fun(Val);
|
fun() -> Mod:Fun(Val) end;
|
||||||
_ ->
|
_ ->
|
||||||
catch F(Val)
|
fun() -> F(Val) end
|
||||||
end,
|
end,
|
||||||
case Res of
|
try Call() of
|
||||||
{'EXIT', _} ->
|
Res ->
|
||||||
|
Res
|
||||||
|
catch {replace_with, NewRes} ->
|
||||||
|
NewRes;
|
||||||
|
{invalid_syntax, Error} ->
|
||||||
|
?WARNING_MSG("incorrect value '~s' of option '~s', "
|
||||||
|
"using '~s' as fallback: ~s",
|
||||||
|
[format_term(Val),
|
||||||
|
format_term(Opt),
|
||||||
|
format_term(Default),
|
||||||
|
Error]),
|
||||||
|
Default;
|
||||||
|
_:_ ->
|
||||||
?WARNING_MSG("incorrect value '~s' of option '~s', "
|
?WARNING_MSG("incorrect value '~s' of option '~s', "
|
||||||
"using '~s' as fallback",
|
"using '~s' as fallback",
|
||||||
[format_term(Val),
|
[format_term(Val),
|
||||||
format_term(Opt),
|
format_term(Opt),
|
||||||
format_term(Default)]),
|
format_term(Default)]),
|
||||||
Default;
|
Default
|
||||||
_ ->
|
|
||||||
Res
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-type check_fun() :: fun((any()) -> any()) | {module(), atom()}.
|
-type check_fun() :: fun((any()) -> any()) | {module(), atom()}.
|
||||||
@ -908,19 +918,26 @@ get_modules_with_options() ->
|
|||||||
|
|
||||||
validate_opts(#state{opts = Opts} = State) ->
|
validate_opts(#state{opts = Opts} = State) ->
|
||||||
ModOpts = get_modules_with_options(),
|
ModOpts = get_modules_with_options(),
|
||||||
NewOpts = lists:filter(
|
NewOpts = lists:filtermap(
|
||||||
fun(#local_config{key = {Opt, _Host}, value = Val}) ->
|
fun(#local_config{key = {Opt, _Host}, value = Val} = In) ->
|
||||||
case dict:find(Opt, ModOpts) of
|
case dict:find(Opt, ModOpts) of
|
||||||
{ok, [Mod|_]} ->
|
{ok, [Mod|_]} ->
|
||||||
VFun = Mod:opt_type(Opt),
|
VFun = Mod:opt_type(Opt),
|
||||||
case catch VFun(Val) of
|
try VFun(Val) of
|
||||||
{'EXIT', _} ->
|
_ ->
|
||||||
|
true
|
||||||
|
catch {replace_with, NewVal} ->
|
||||||
|
{true, In#local_config{value = NewVal}};
|
||||||
|
{invalid_syntax, Error} ->
|
||||||
|
?ERROR_MSG("ignoring option '~s' with "
|
||||||
|
"invalid value: ~p: ~s",
|
||||||
|
[Opt, Val, Error]),
|
||||||
|
false;
|
||||||
|
_:_ ->
|
||||||
?ERROR_MSG("ignoring option '~s' with "
|
?ERROR_MSG("ignoring option '~s' with "
|
||||||
"invalid value: ~p",
|
"invalid value: ~p",
|
||||||
[Opt, Val]),
|
[Opt, Val]),
|
||||||
false;
|
false
|
||||||
_ ->
|
|
||||||
true
|
|
||||||
end;
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
?ERROR_MSG("unknown option '~s' will be likely"
|
?ERROR_MSG("unknown option '~s' will be likely"
|
||||||
|
@ -266,18 +266,25 @@ get_opt_host(Host, Opts, Default) ->
|
|||||||
ejabberd_regexp:greplace(Val, <<"@HOST@">>, Host).
|
ejabberd_regexp:greplace(Val, <<"@HOST@">>, Host).
|
||||||
|
|
||||||
validate_opts(Module, Opts) ->
|
validate_opts(Module, Opts) ->
|
||||||
lists:filter(
|
lists:filtermap(
|
||||||
fun({Opt, Val}) ->
|
fun({Opt, Val}) ->
|
||||||
case catch Module:mod_opt_type(Opt) of
|
case catch Module:mod_opt_type(Opt) of
|
||||||
VFun when is_function(VFun) ->
|
VFun when is_function(VFun) ->
|
||||||
case catch VFun(Val) of
|
try VFun(Val) of
|
||||||
{'EXIT', _} ->
|
_ ->
|
||||||
|
true
|
||||||
|
catch {replace_with, NewVal} ->
|
||||||
|
{true, {Opt, NewVal}};
|
||||||
|
{invalid_syntax, Error} ->
|
||||||
|
?ERROR_MSG("ignoring invalid value '~p' for "
|
||||||
|
"option '~s' of module '~s': ~s",
|
||||||
|
[Val, Opt, Module, Error]),
|
||||||
|
false;
|
||||||
|
_:_ ->
|
||||||
?ERROR_MSG("ignoring invalid value '~p' for "
|
?ERROR_MSG("ignoring invalid value '~p' for "
|
||||||
"option '~s' of module '~s'",
|
"option '~s' of module '~s'",
|
||||||
[Val, Opt, Module]),
|
[Val, Opt, Module]),
|
||||||
false;
|
false
|
||||||
_ ->
|
|
||||||
true
|
|
||||||
end;
|
end;
|
||||||
L when is_list(L) ->
|
L when is_list(L) ->
|
||||||
SOpts = str:join([[$', atom_to_list(A), $'] || A <- L], <<", ">>),
|
SOpts = str:join([[$', atom_to_list(A), $'] || A <- L], <<", ">>),
|
||||||
|
Loading…
Reference in New Issue
Block a user