Browse Source

fix: let crash when plugin not found in cluster

JimMoen 1 năm trước cách đây
mục cha
commit
28293284a7

+ 3 - 0
apps/emqx_management/src/emqx_mgmt_api_plugins.erl

@@ -597,6 +597,9 @@ delete_package(Name) ->
     end.
 
 %% for RPC plugin update
+%% TODO: catch thrown error to return 400
+%% - plugin_not_found
+%% - otp vsn assertion failed
 ensure_action(Name, start) ->
     _ = emqx_plugins:ensure_started(Name),
     _ = emqx_plugins:ensure_enabled(Name),

+ 13 - 8
apps/emqx_plugins/src/emqx_plugins.erl

@@ -279,9 +279,9 @@ ensure_started(NameVsn) ->
     case do_ensure_started(NameVsn) of
         ok ->
             ok;
-        {error, Reason} ->
-            ?SLOG(alert, Reason#{msg => "failed_to_start_plugin"}),
-            {error, Reason}
+        {error, ReasonMap} ->
+            ?SLOG(alert, ReasonMap#{msg => "failed_to_start_plugin"}),
+            {error, ReasonMap}
     end.
 
 %% @doc Stop all plugins before broker stops.
@@ -664,12 +664,12 @@ do_ensure_started(NameVsn) ->
                 ok ->
                     Plugin = do_read_plugin(NameVsn),
                     ok = load_code_start_apps(NameVsn, Plugin);
-                {error, plugin_not_found} ->
+                {error, #{reason := Reason} = ReasonMap} ->
                     ?SLOG(error, #{
-                        error_msg => "plugin_not_found",
+                        error_msg => string_reason(Reason),
                         name_vsn => NameVsn
                     }),
-                    ok
+                    {error, ReasonMap}
             end
         end
     ).
@@ -772,7 +772,7 @@ do_get_from_cluster(NameVsn) ->
                 error_msg => "failed_to_copy_plugin_from_other_nodes",
                 name_vsn => NameVsn,
                 node_errors => NodeErrors,
-                reason => not_found
+                reason => plugin_not_found
             },
             ?SLOG(error, ErrMeta),
             {error, ErrMeta};
@@ -780,7 +780,7 @@ do_get_from_cluster(NameVsn) ->
             ErrMeta = #{
                 error_msg => "no_nodes_to_copy_plugin_from",
                 name_vsn => NameVsn,
-                reason => not_found
+                reason => plugin_not_found
             },
             ?SLOG(error, ErrMeta),
             {error, ErrMeta}
@@ -1497,3 +1497,8 @@ bin(B) when is_binary(B) -> B.
 
 wrap_to_list(Path) ->
     binary_to_list(iolist_to_binary(Path)).
+
+string_reason(plugin_not_found) ->
+    "plugin_not_found";
+string_reason(_) ->
+    "unexpected_error".

+ 1 - 1
apps/emqx_plugins/test/emqx_plugins_SUITE.erl

@@ -390,7 +390,7 @@ t_bad_tar_gz(Config) ->
     ?assertMatch(
         {error, #{
             error_msg := "no_nodes_to_copy_plugin_from",
-            reason := not_found
+            reason := plugin_not_found
         }},
         emqx_plugins:ensure_installed("nonexisting")
     ),