fix: flatten error message on resource validator
Fixes https://emqx.atlassian.net/browse/EMQX-10864
This commit is contained in:
parent
cb1e105e19
commit
42877e282d
|
@ -28,10 +28,18 @@ max(Type, Max) ->
|
||||||
min(Type, Min) ->
|
min(Type, Min) ->
|
||||||
limit(Type, '>=', Min).
|
limit(Type, '>=', Min).
|
||||||
|
|
||||||
not_empty(ErrMsg) ->
|
not_empty(ErrMsg0) ->
|
||||||
|
ErrMsg =
|
||||||
|
try
|
||||||
|
lists:flatten(ErrMsg0)
|
||||||
|
catch
|
||||||
|
_:_ ->
|
||||||
|
ErrMsg0
|
||||||
|
end,
|
||||||
fun
|
fun
|
||||||
(undefined) -> {error, ErrMsg};
|
(undefined) -> {error, ErrMsg};
|
||||||
(<<>>) -> {error, ErrMsg};
|
(<<>>) -> {error, ErrMsg};
|
||||||
|
("") -> {error, ErrMsg};
|
||||||
(_) -> ok
|
(_) -> ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -50,7 +58,8 @@ len(string) -> fun string:length/1;
|
||||||
len(_Type) -> fun(Val) -> Val end.
|
len(_Type) -> fun(Val) -> Val end.
|
||||||
|
|
||||||
err_limit({Type, {Op, Expected}, {got, Got}}) ->
|
err_limit({Type, {Op, Expected}, {got, Got}}) ->
|
||||||
io_lib:format("Expect the ~ts value ~ts ~p but got: ~p", [Type, Op, Expected, Got]).
|
Msg = io_lib:format("Expect the ~ts value ~ts ~p but got: ~p", [Type, Op, Expected, Got]),
|
||||||
|
lists:flatten(Msg).
|
||||||
|
|
||||||
return(true, _) -> ok;
|
return(true, _) -> ok;
|
||||||
return(false, Error) -> {error, Error}.
|
return(false, Error) -> {error, Error}.
|
||||||
|
|
|
@ -1121,10 +1121,58 @@ t_create_dry_run_local_failed(_) ->
|
||||||
).
|
).
|
||||||
|
|
||||||
t_test_func(_) ->
|
t_test_func(_) ->
|
||||||
|
IsErrorMsgPlainString = fun({error, Msg}) -> io_lib:printable_list(Msg) end,
|
||||||
?assertEqual(ok, erlang:apply(emqx_resource_validator:not_empty("not_empty"), [<<"someval">>])),
|
?assertEqual(ok, erlang:apply(emqx_resource_validator:not_empty("not_empty"), [<<"someval">>])),
|
||||||
?assertEqual(ok, erlang:apply(emqx_resource_validator:min(int, 3), [4])),
|
?assertEqual(ok, erlang:apply(emqx_resource_validator:min(int, 3), [4])),
|
||||||
?assertEqual(ok, erlang:apply(emqx_resource_validator:max(array, 10), [[a, b, c, d]])),
|
?assertEqual(ok, erlang:apply(emqx_resource_validator:max(array, 10), [[a, b, c, d]])),
|
||||||
?assertEqual(ok, erlang:apply(emqx_resource_validator:max(string, 10), ["less10"])).
|
?assertEqual(ok, erlang:apply(emqx_resource_validator:max(string, 10), ["less10"])),
|
||||||
|
?assertEqual(
|
||||||
|
true, IsErrorMsgPlainString(erlang:apply(emqx_resource_validator:min(int, 66), [42]))
|
||||||
|
),
|
||||||
|
?assertEqual(
|
||||||
|
true, IsErrorMsgPlainString(erlang:apply(emqx_resource_validator:max(int, 42), [66]))
|
||||||
|
),
|
||||||
|
?assertEqual(
|
||||||
|
true, IsErrorMsgPlainString(erlang:apply(emqx_resource_validator:min(array, 3), [[1, 2]]))
|
||||||
|
),
|
||||||
|
?assertEqual(
|
||||||
|
true,
|
||||||
|
IsErrorMsgPlainString(erlang:apply(emqx_resource_validator:max(array, 3), [[1, 2, 3, 4]]))
|
||||||
|
),
|
||||||
|
?assertEqual(
|
||||||
|
true, IsErrorMsgPlainString(erlang:apply(emqx_resource_validator:min(string, 3), ["1"]))
|
||||||
|
),
|
||||||
|
?assertEqual(
|
||||||
|
true, IsErrorMsgPlainString(erlang:apply(emqx_resource_validator:max(string, 3), ["1234"]))
|
||||||
|
),
|
||||||
|
NestedMsg = io_lib:format("The answer: ~p", [42]),
|
||||||
|
ExpectedMsg = "The answer: 42",
|
||||||
|
BinMsg = <<"The answer: 42">>,
|
||||||
|
MapMsg = #{question => "The question", answer => 42},
|
||||||
|
?assertEqual(
|
||||||
|
{error, ExpectedMsg},
|
||||||
|
erlang:apply(emqx_resource_validator:not_empty(NestedMsg), [""])
|
||||||
|
),
|
||||||
|
?assertEqual(
|
||||||
|
{error, ExpectedMsg},
|
||||||
|
erlang:apply(emqx_resource_validator:not_empty(NestedMsg), [<<>>])
|
||||||
|
),
|
||||||
|
?assertEqual(
|
||||||
|
{error, ExpectedMsg},
|
||||||
|
erlang:apply(emqx_resource_validator:not_empty(NestedMsg), [undefined])
|
||||||
|
),
|
||||||
|
?assertEqual(
|
||||||
|
{error, ExpectedMsg},
|
||||||
|
erlang:apply(emqx_resource_validator:not_empty(NestedMsg), [undefined])
|
||||||
|
),
|
||||||
|
?assertEqual(
|
||||||
|
{error, BinMsg},
|
||||||
|
erlang:apply(emqx_resource_validator:not_empty(BinMsg), [undefined])
|
||||||
|
),
|
||||||
|
?assertEqual(
|
||||||
|
{error, MapMsg},
|
||||||
|
erlang:apply(emqx_resource_validator:not_empty(MapMsg), [""])
|
||||||
|
).
|
||||||
|
|
||||||
t_reset_metrics(_) ->
|
t_reset_metrics(_) ->
|
||||||
{ok, _} = emqx_resource:create(
|
{ok, _} = emqx_resource:create(
|
||||||
|
|
Loading…
Reference in New Issue