From 1b79b3f3c967b1300bf6810aa0e2ec261434a017 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Tue, 19 Apr 2022 19:45:00 +0800 Subject: [PATCH] fix: improve the error logs if update bridge failed --- apps/emqx/src/emqx_config_handler.erl | 2 +- apps/emqx/test/emqx_config_handler_SUITE.erl | 2 +- .../src/mqtt/emqx_connector_mqtt_mod.erl | 14 +++++++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/apps/emqx/src/emqx_config_handler.erl b/apps/emqx/src/emqx_config_handler.erl index 30eec029d..be42db57e 100644 --- a/apps/emqx/src/emqx_config_handler.erl +++ b/apps/emqx/src/emqx_config_handler.erl @@ -205,7 +205,7 @@ handle_update_request(SchemaModule, ConfKeyPath, Handlers, UpdateArgs) -> key_path => ConfKeyPath, stacktrace => ST }), - {error, config_update_crashed} + {error, {config_update_crashed, Reason}} end. do_handle_update_request(SchemaModule, ConfKeyPath, Handlers, UpdateArgs) -> diff --git a/apps/emqx/test/emqx_config_handler_SUITE.erl b/apps/emqx/test/emqx_config_handler_SUITE.erl index 6861a0be1..72e3ed62f 100644 --- a/apps/emqx/test/emqx_config_handler_SUITE.erl +++ b/apps/emqx/test/emqx_config_handler_SUITE.erl @@ -223,7 +223,7 @@ t_callback_crash(_Config) -> Opts = #{rawconf_with_defaults => true}, ok = emqx_config_handler:add_handler(CrashPath, ?MODULE), Old = emqx:get_raw_config(CrashPath), - ?assertEqual({error, config_update_crashed}, emqx:update_config(CrashPath, <<"89%">>, Opts)), + ?assertMatch({error, {config_update_crashed, _}}, emqx:update_config(CrashPath, <<"89%">>, Opts)), New = emqx:get_raw_config(CrashPath), ?assertEqual(Old, New), ok = emqx_config_handler:remove_handler(CrashPath), diff --git a/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_mod.erl b/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_mod.erl index fcad0ef7a..1a9c55ced 100644 --- a/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_mod.erl +++ b/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_mod.erl @@ -52,7 +52,8 @@ start(Config) -> Mountpoint = maps:get(receive_mountpoint, Config, undefined), Subscriptions = maps:get(subscriptions, Config, undefined), Vars = emqx_connector_mqtt_msg:make_pub_vars(Mountpoint, Subscriptions), - Handlers = make_hdlr(Parent, Vars, #{server => ip_port_to_server(Host, Port)}), + ServerStr = ip_port_to_server_str(Host, Port), + Handlers = make_hdlr(Parent, Vars, #{server => ServerStr}), Config1 = Config#{ msg_handler => Handlers, host => Host, @@ -70,16 +71,19 @@ start(Config) -> catch throw : Reason -> ok = stop(#{client_pid => Pid}), - {error, Reason} + {error, error_reason(Reason, ServerStr)} end; {error, Reason} -> ok = stop(#{client_pid => Pid}), - {error, Reason} + {error, error_reason(Reason, ServerStr)} end; {error, Reason} -> - {error, Reason} + {error, error_reason(Reason, ServerStr)} end. +error_reason(Reason, ServerStr) -> + #{reason => Reason, server => ServerStr}. + stop(#{client_pid := Pid}) -> safe_stop(Pid, fun() -> emqtt:stop(Pid) end, 1000), ok. @@ -238,7 +242,7 @@ printable_maps(Headers) -> (K, V0, AccIn) -> AccIn#{K => V0} end, #{}, Headers). -ip_port_to_server(Host, Port) -> +ip_port_to_server_str(Host, Port) -> HostStr = case inet:ntoa(Host) of {error, einval} -> Host; IPStr -> IPStr