From 55f8f7277d01fe59a19c78f9311c0910c0d00ca5 Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Mon, 25 Apr 2022 11:47:18 +0800 Subject: [PATCH] fix: imporve hocon check failed error --- apps/emqx/src/emqx_logger_jsonfmt.erl | 8 +++++--- .../src/emqx_dashboard_swagger.erl | 20 +++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/apps/emqx/src/emqx_logger_jsonfmt.erl b/apps/emqx/src/emqx_logger_jsonfmt.erl index f1a9b8ee5..b9f1d4fa8 100644 --- a/apps/emqx/src/emqx_logger_jsonfmt.erl +++ b/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) -> diff --git a/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl b/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl index da590f88d..039847704 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard_swagger.erl +++ b/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, [<<"
\n">>, <<"\n">>], <<"
">>, [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.").