Merge pull request #11995 from thalesmg/fix-action-api-bad-name-r53-20231121

fix(actions_api): don't crash on validation errors
This commit is contained in:
Thales Macedo Garitezi 2023-11-21 17:27:38 -03:00 committed by GitHub
commit 01895fd660
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -791,6 +791,8 @@ do_create_or_update_bridge(BridgeType, BridgeName, Conf, HttpStatusCode) ->
PreOrPostConfigUpdate =:= pre_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)))
end.

View File

@ -1021,6 +1021,28 @@ t_action_types(Config) ->
?assert(lists:all(fun is_binary/1, Types), #{types => Types}),
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
listen_on_random_port() ->
SockOpts = [binary, {active, false}, {packet, raw}, {reuseaddr, true}, {backlog, 1000}],