fix: ensure config update error text is readable
This commit is contained in:
parent
363fe4fd96
commit
96d21d4dbe
|
@ -354,8 +354,12 @@ configs(get, #{query_string := QueryStr, headers := Headers}, _Req) ->
|
||||||
end;
|
end;
|
||||||
configs(put, #{body := Conf, query_string := #{<<"mode">> := Mode}}, _Req) ->
|
configs(put, #{body := Conf, query_string := #{<<"mode">> := Mode}}, _Req) ->
|
||||||
case emqx_conf_cli:load_config(Conf, #{mode => Mode, log => none}) of
|
case emqx_conf_cli:load_config(Conf, #{mode => Mode, log => none}) of
|
||||||
ok -> {200};
|
ok ->
|
||||||
{error, Msg} -> {400, #{<<"content-type">> => <<"text/plain">>}, Msg}
|
{200};
|
||||||
|
{error, MsgList} ->
|
||||||
|
JsonFun = fun(K, V) -> {K, emqx_utils_maps:binary_string(V)} end,
|
||||||
|
JsonMap = emqx_utils_maps:jsonable_map(maps:from_list(MsgList), JsonFun),
|
||||||
|
{400, #{<<"content-type">> => <<"text/plain">>}, JsonMap}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
find_suitable_accept(Headers, Preferences) when is_list(Preferences), length(Preferences) > 0 ->
|
find_suitable_accept(Headers, Preferences) when is_list(Preferences), length(Preferences) > 0 ->
|
||||||
|
|
|
@ -70,7 +70,7 @@ t_update(_Config) ->
|
||||||
%% update failed
|
%% update failed
|
||||||
ErrorSysMon = emqx_utils_maps:deep_put([<<"vm">>, <<"busy_port">>], SysMon, "123"),
|
ErrorSysMon = emqx_utils_maps:deep_put([<<"vm">>, <<"busy_port">>], SysMon, "123"),
|
||||||
?assertMatch(
|
?assertMatch(
|
||||||
{error, {"HTTP/1.1", 400, _}},
|
{error, {"HTTP/1.1", 400, "Bad Request"}},
|
||||||
update_config(<<"sysmon">>, ErrorSysMon)
|
update_config(<<"sysmon">>, ErrorSysMon)
|
||||||
),
|
),
|
||||||
{ok, SysMon2} = get_config(<<"sysmon">>),
|
{ok, SysMon2} = get_config(<<"sysmon">>),
|
||||||
|
@ -328,6 +328,18 @@ t_configs_key(_Config) ->
|
||||||
Log1 = emqx_utils_maps:deep_put([<<"log">>, <<"console">>, <<"level">>], Log, <<"error">>),
|
Log1 = emqx_utils_maps:deep_put([<<"log">>, <<"console">>, <<"level">>], Log, <<"error">>),
|
||||||
?assertEqual([], update_configs_with_binary(iolist_to_binary(hocon_pp:do(Log1, #{})))),
|
?assertEqual([], update_configs_with_binary(iolist_to_binary(hocon_pp:do(Log1, #{})))),
|
||||||
?assertEqual(<<"error">>, read_conf([<<"log">>, <<"console">>, <<"level">>])),
|
?assertEqual(<<"error">>, read_conf([<<"log">>, <<"console">>, <<"level">>])),
|
||||||
|
BadLog = emqx_utils_maps:deep_put([<<"log">>, <<"console">>, <<"level">>], Log, <<"erro1r">>),
|
||||||
|
{error, Error} = update_configs_with_binary(iolist_to_binary(hocon_pp:do(BadLog, #{}))),
|
||||||
|
ExpectError = #{
|
||||||
|
<<"log">> =>
|
||||||
|
#{
|
||||||
|
<<"kind">> => <<"validation_error">>,
|
||||||
|
<<"path">> => <<"log.console.level">>,
|
||||||
|
<<"reason">> => <<"unable_to_convert_to_enum_symbol">>,
|
||||||
|
<<"value">> => <<"erro1r">>
|
||||||
|
}
|
||||||
|
},
|
||||||
|
?assertEqual(ExpectError, emqx_utils_json:decode(Error, [return_maps])),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
t_get_configs_in_different_accept(_Config) ->
|
t_get_configs_in_different_accept(_Config) ->
|
||||||
|
@ -348,7 +360,7 @@ t_get_configs_in_different_accept(_Config) ->
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
%% returns text/palin if text/plain is acceptable
|
%% returns text/plain if text/plain is acceptable
|
||||||
?assertMatch({200, "text/plain", _}, Request(<<"text/plain">>)),
|
?assertMatch({200, "text/plain", _}, Request(<<"text/plain">>)),
|
||||||
?assertMatch({200, "text/plain", _}, Request(<<"*/*">>)),
|
?assertMatch({200, "text/plain", _}, Request(<<"*/*">>)),
|
||||||
?assertMatch(
|
?assertMatch(
|
||||||
|
@ -416,9 +428,14 @@ update_configs_with_binary(Bin) ->
|
||||||
Auth = emqx_mgmt_api_test_util:auth_header_(),
|
Auth = emqx_mgmt_api_test_util:auth_header_(),
|
||||||
Headers = [{"accept", "text/plain"}, Auth],
|
Headers = [{"accept", "text/plain"}, Auth],
|
||||||
case httpc:request(put, {Path, Headers, "text/plain", Bin}, [], []) of
|
case httpc:request(put, {Path, Headers, "text/plain", Bin}, [], []) of
|
||||||
{ok, {_, _, Res}} -> Res;
|
{ok, {{"HTTP/1.1", Code, _}, _Headers, Body}} when
|
||||||
{ok, Res} -> Res;
|
Code >= 200 andalso Code =< 299
|
||||||
Error -> Error
|
->
|
||||||
|
Body;
|
||||||
|
{ok, {{"HTTP/1.1", _Code, _}, _Headers, Body}} ->
|
||||||
|
{error, Body};
|
||||||
|
Error ->
|
||||||
|
Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
update_config(Name, Change) ->
|
update_config(Name, Change) ->
|
||||||
|
|
Loading…
Reference in New Issue