Explorar el Código

fix(auto_subscribe): make a warning if the topic is empty when auto subscribe

firest hace 3 años
padre
commit
bad04e3571

+ 21 - 7
apps/emqx_auto_subscribe/src/emqx_auto_subscribe_placeholder.erl

@@ -16,6 +16,7 @@
 -module(emqx_auto_subscribe_placeholder).
 -module(emqx_auto_subscribe_placeholder).
 
 
 -include_lib("emqx/include/emqx_placeholder.hrl").
 -include_lib("emqx/include/emqx_placeholder.hrl").
+-include_lib("emqx/include/logger.hrl").
 
 
 -export([generate/1]).
 -export([generate/1]).
 
 
@@ -31,11 +32,23 @@ generate(T0 = #{topic := Topic}) ->
 
 
 -spec(to_topic_table(list(), map(), map()) -> list()).
 -spec(to_topic_table(list(), map(), map()) -> list()).
 to_topic_table(PHs, ClientInfo, ConnInfo) ->
 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
 %% 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(PTs, C, Co, [Binary | Res]);
 to_topic([clientid | PTs], C = #{clientid := ClientID}, Co, Res) ->
 to_topic([clientid | PTs], C = #{clientid := ClientID}, Co, Res) ->
     to_topic(PTs, C, Co, [ClientID | 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([username | PTs], C = #{username := Username}, Co, Res) ->
     to_topic(PTs, C, Co, [Username | Res]);
     to_topic(PTs, C, Co, [Username | Res]);
 to_topic([host | PTs], C, Co = #{peername := {Host, _}}, Res) ->
 to_topic([host | PTs], C, Co = #{peername := {Host, _}}, Res) ->