From bad04e35719f06b615f60b4d422c8d319cb491e3 Mon Sep 17 00:00:00 2001 From: firest Date: Mon, 21 Mar 2022 17:47:04 +0800 Subject: [PATCH] fix(auto_subscribe): make a warning if the topic is empty when auto subscribe --- .../src/emqx_auto_subscribe_placeholder.erl | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/apps/emqx_auto_subscribe/src/emqx_auto_subscribe_placeholder.erl b/apps/emqx_auto_subscribe/src/emqx_auto_subscribe_placeholder.erl index 44e96f610..8814edf62 100644 --- a/apps/emqx_auto_subscribe/src/emqx_auto_subscribe_placeholder.erl +++ b/apps/emqx_auto_subscribe/src/emqx_auto_subscribe_placeholder.erl @@ -16,6 +16,7 @@ -module(emqx_auto_subscribe_placeholder). -include_lib("emqx/include/emqx_placeholder.hrl"). +-include_lib("emqx/include/logger.hrl"). -export([generate/1]). @@ -31,11 +32,23 @@ generate(T0 = #{topic := Topic}) -> -spec(to_topic_table(list(), map(), map()) -> list()). to_topic_table(PHs, ClientInfo, ConnInfo) -> - [begin - Topic0 = to_topic(PlaceHolder, ClientInfo, ConnInfo, []), - {Topic, Opts} = emqx_topic:parse(Topic0), - {Topic, Opts#{qos => Qos, rh => RH, rap => RAP, nl => NL}} - end || #{qos := Qos, rh := RH, rap := RAP, nl := NL, placeholder := PlaceHolder} <- PHs]. + Fold = fun(#{qos := Qos, rh := RH, rap := RAP, nl := NL, placeholder := PlaceHolder}, Acc) -> + case to_topic(PlaceHolder, ClientInfo, ConnInfo, []) of + {error, _} -> + Acc; + <<>> -> + ?SLOG(warning, #{msg => "Topic can't be empty", + clientinfo => ClientInfo, + conninfo => ConnInfo, + placeholder => PlaceHolder + }), + Acc; + Topic0 -> + {Topic, Opts} = emqx_topic:parse(Topic0), + [{Topic, Opts#{qos => Qos, rh => RH, rap => RAP, nl => NL}} | Acc] + end + end, + lists:foldl(Fold, [], PHs). %%-------------------------------------------------------------------- %% internal @@ -63,8 +76,9 @@ to_topic([Binary | PTs], C, Co, Res) when is_binary(Binary) -> to_topic(PTs, C, Co, [Binary | Res]); to_topic([clientid | PTs], C = #{clientid := ClientID}, Co, Res) -> to_topic(PTs, C, Co, [ClientID | Res]); -to_topic([username | PTs], C = #{username := undefined}, Co, Res) -> - to_topic(PTs, C, Co, [?PH_USERNAME | Res]); +to_topic([username | _], #{username := undefined}, _, _) -> + ?SLOG(error, #{msg => "Username undefined when auto subscribe"}), + {error, username_undefined}; to_topic([username | PTs], C = #{username := Username}, Co, Res) -> to_topic(PTs, C, Co, [Username | Res]); to_topic([host | PTs], C, Co = #{peername := {Host, _}}, Res) ->