fix(rule apply test): do not crash when the rule is not found

This commit is contained in:
Kjell Winblad 2024-05-02 11:22:29 +02:00
parent 3ba5cb7858
commit 9576efb716
2 changed files with 14 additions and 1 deletions

View File

@ -274,6 +274,7 @@ schema("/rules/:id/test") ->
responses => #{ responses => #{
400 => error_schema('BAD_REQUEST', "Invalid Parameters"), 400 => error_schema('BAD_REQUEST', "Invalid Parameters"),
412 => error_schema('NOT_MATCH', "SQL Not Match"), 412 => error_schema('NOT_MATCH', "SQL Not Match"),
404 => error_schema('RULE_NOT_FOUND', "The rule could not be found"),
200 => <<"Rule Applied">> 200 => <<"Rule Applied">>
} }
} }
@ -424,6 +425,8 @@ param_path_id() ->
{400, #{code => 'BAD_REQUEST', message => err_msg(Reason)}}; {400, #{code => 'BAD_REQUEST', message => err_msg(Reason)}};
{error, nomatch} -> {error, nomatch} ->
{412, #{code => 'NOT_MATCH', message => <<"SQL Not Match">>}}; {412, #{code => 'NOT_MATCH', message => <<"SQL Not Match">>}};
{error, rule_not_found} ->
{404, #{code => 'RULE_NOT_FOUND', message => <<"The rule could not be found">>}};
{error, Reason} -> {error, Reason} ->
{400, #{code => 'BAD_REQUEST', message => err_msg(Reason)}} {400, #{code => 'BAD_REQUEST', message => err_msg(Reason)}}
end end

View File

@ -26,12 +26,22 @@
apply_rule( apply_rule(
RuleId, RuleId,
Parameters
) ->
case emqx_rule_engine:get_rule(RuleId) of
{ok, Rule} ->
do_apply_rule(Rule, Parameters);
not_found ->
{error, rule_not_found}
end.
do_apply_rule(
Rule,
#{ #{
context := Context, context := Context,
stop_action_after_template_rendering := StopAfterRender stop_action_after_template_rendering := StopAfterRender
} }
) -> ) ->
{ok, Rule} = emqx_rule_engine:get_rule(RuleId),
InTopic = get_in_topic(Context), InTopic = get_in_topic(Context),
EventTopics = maps:get(from, Rule, []), EventTopics = maps:get(from, Rule, []),
case lists:all(fun is_publish_topic/1, EventTopics) of case lists:all(fun is_publish_topic/1, EventTopics) of