Преглед изворни кода

Merge pull request #7751 from zhongwencool/improve-http-api-error-msg

fix: imporve hocon check failed error
zhongwencool пре 3 година
родитељ
комит
ae20beeb58

+ 5 - 3
apps/emqx/src/emqx_logger_jsonfmt.erl

@@ -31,8 +31,8 @@
 
 -export([format/2]).
 
-%% For CLI outputs
--export([best_effort_json/1]).
+%% For CLI HTTP API outputs
+-export([best_effort_json/1, best_effort_json/2]).
 
 -ifdef(TEST).
 -include_lib("proper/include/proper.hrl").
@@ -62,9 +62,11 @@
 %% The JSON object is pretty-printed.
 %% NOTE: do not use this function for logging.
 best_effort_json(Input) ->
+    best_effort_json(Input, [space, {indent, 4}]).
+best_effort_json(Input, Opts) ->
     Config = #{depth => unlimited, single_line => true},
     JsonReady = best_effort_json_obj(Input, Config),
-    jsx:encode(JsonReady, [space, {indent, 4}]).
+    jsx:encode(JsonReady, Opts).
 
 -spec format(logger:log_event(), config()) -> iodata().
 format(#{level := Level, msg := Msg, meta := Meta}, Config0) when is_map(Config0) ->

+ 10 - 10
apps/emqx_dashboard/src/emqx_dashboard_swagger.erl

@@ -408,7 +408,9 @@ trans_description(Spec, Hocon) ->
         end,
     case Desc of
         undefined -> Spec;
-        Desc -> Spec#{description => Desc}
+        Desc ->
+            Desc1 = binary:replace(Desc, [<<"</br>\n">>, <<"\n">>], <<"</br>">>, [global]),
+            Spec#{description => Desc1}
     end.
 
 get_i18n(Key, Struct, Default) ->
@@ -813,17 +815,15 @@ schema_converter(Options) ->
 
 serialize_hocon_error_msg({_Schema, Errors}) ->
     Msg = lists:map(fun hocon_error/1, Errors),
-    iolist_to_binary(string:join(Msg, ","));
+    iolist_to_binary(Msg);
 serialize_hocon_error_msg(Error) ->
     iolist_to_binary(io_lib:format("~p", [Error])).
 
-hocon_error({validation_error, #{reason := #{exception := Exception}, path := Path}}) ->
-    io_lib:format("~ts: ~p", [sub_path(Path), Exception]);
-hocon_error({validation_error, #{reason := Reason, path := Path, value := Value}}) ->
-    io_lib:format("~ts: ~p ~p", [sub_path(Path), Value, Reason]);
-hocon_error({validation_error, #{reason := Reason, path := Path}}) ->
-    io_lib:format("~ts: ~p", [sub_path(Path), Reason]);
-hocon_error({translation_error, #{reason := Reason, value_path := Path}}) ->
-    io_lib:format("~ts: ~p", [sub_path(Path), Reason]).
+hocon_error({validation_error, #{path := Path} = Error}) ->
+    Error1 = maps:without([path, stacktrace], Error),
+    emqx_logger_jsonfmt:best_effort_json(Error1#{<<"path">> => sub_path(Path)}, []);
+hocon_error({translation_error, #{value_path := Path} = Error}) ->
+    Error1 = maps:without([value_path, stacktrace], Error),
+    emqx_logger_jsonfmt:best_effort_json(Error1#{<<"path">> => sub_path(Path)}, []).
 
 sub_path(Path) -> string:trim(Path, leading, "root.").