Jelajahi Sumber

fix(actions_api): don't crash on validation errors

Fixes https://emqx.atlassian.net/browse/EMQX-11394
Thales Macedo Garitezi 2 tahun lalu
induk
melakukan
3a8c332805

+ 2 - 0
apps/emqx_bridge/src/emqx_bridge_v2_api.erl

@@ -791,6 +791,8 @@ do_create_or_update_bridge(BridgeType, BridgeName, Conf, HttpStatusCode) ->
             PreOrPostConfigUpdate =:= pre_config_update;
             PreOrPostConfigUpdate =:= pre_config_update;
             PreOrPostConfigUpdate =:= post_config_update
             PreOrPostConfigUpdate =:= post_config_update
         ->
         ->
+            ?BAD_REQUEST(map_to_json(redact(Reason)));
+        {error, Reason} when is_map(Reason) ->
             ?BAD_REQUEST(map_to_json(redact(Reason)))
             ?BAD_REQUEST(map_to_json(redact(Reason)))
     end.
     end.
 
 

+ 22 - 0
apps/emqx_bridge/test/emqx_bridge_v2_api_SUITE.erl

@@ -1021,6 +1021,28 @@ t_action_types(Config) ->
     ?assert(lists:all(fun is_binary/1, Types), #{types => Types}),
     ?assert(lists:all(fun is_binary/1, Types), #{types => Types}),
     ok.
     ok.
 
 
+t_bad_name(Config) ->
+    Name = <<"_bad_name">>,
+    Res = request_json(
+        post,
+        uri([?ROOT]),
+        ?KAFKA_BRIDGE(Name),
+        Config
+    ),
+    ?assertMatch({ok, 400, #{<<"message">> := _}}, Res),
+    {ok, 400, #{<<"message">> := Msg0}} = Res,
+    Msg = emqx_utils_json:decode(Msg0, [return_maps]),
+    ?assertMatch(
+        #{
+            <<"got">> := [<<"_bad_name">>],
+            <<"kind">> := <<"validation_error">>,
+            <<"path">> := <<"actions.kafka_producer">>,
+            <<"reason">> := <<"invalid_map_key">>
+        },
+        Msg
+    ),
+    ok.
+
 %%% helpers
 %%% helpers
 listen_on_random_port() ->
 listen_on_random_port() ->
     SockOpts = [binary, {active, false}, {packet, raw}, {reuseaddr, true}, {backlog, 1000}],
     SockOpts = [binary, {active, false}, {packet, raw}, {reuseaddr, true}, {backlog, 1000}],