Merge pull request #11480 from thalesmg/rule-funcs-func-clause-r52-20230818

fix(rule_engine): capture function_clause errors
This commit is contained in:
Thales Macedo Garitezi 2023-08-21 11:16:25 -03:00 committed by GitHub
commit 4163f06611
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 5 deletions

View File

@ -493,11 +493,20 @@ apply_func(Other, _, _) ->
}).
do_apply_func(Module, Name, Args, Columns) ->
case erlang:apply(Module, Name, Args) of
Func when is_function(Func) ->
erlang:apply(Func, [Columns]);
Result ->
Result
try
case erlang:apply(Module, Name, Args) of
Func when is_function(Func) ->
erlang:apply(Func, [Columns]);
Result ->
Result
end
catch
error:function_clause ->
?RAISE_BAD_SQL(#{
reason => bad_sql_function_argument,
arguments => Args,
function_name => Name
})
end.
add_metadata(Columns, Metadata) when is_map(Columns), is_map(Metadata) ->

View File

@ -293,6 +293,66 @@ t_kv_store(_) ->
emqx_rule_funcs:kv_store_del(<<"abc">>),
undefined = emqx_rule_funcs:kv_store_get(<<"abc">>).
t_function_clause_errors(_Config) ->
SQL0 = <<"select upper(xxxx) from \"t/a\"">>,
Payload = <<"{}">>,
?assertMatch(
{error,
{select_and_transform_error,
{throw,
#{
arguments := [undefined],
reason := bad_sql_function_argument,
function_name := upper
},
_Stack}}},
emqx_rule_sqltester:test(
#{
sql => SQL0,
context => #{payload => Payload, topic => <<"t/a">>}
}
)
),
SQL1 = <<"foreach xs as x do upper(xxxx) from \"t/a\"">>,
?assertMatch(
{error, {
{doeach_error,
{throw,
#{
arguments := [undefined],
reason := bad_sql_function_argument,
function_name := upper
},
_Stack0}},
_Stack1
}},
emqx_rule_sqltester:test(
#{
sql => SQL1,
context => #{payload => Payload, xs => [1, 2, 3], topic => <<"t/a">>}
}
)
),
SQL2 = <<"foreach upper(xxxx) as x from \"t/a\"">>,
?assertMatch(
{error,
{select_and_collect_error,
{throw,
#{
arguments := [undefined],
reason := bad_sql_function_argument,
function_name := upper
},
_Stack}}},
emqx_rule_sqltester:test(
#{
sql => SQL2,
context => #{payload => Payload, topic => <<"t/a">>}
}
)
),
ok.
%%------------------------------------------------------------------------------
%% Test cases for rule registry
%%------------------------------------------------------------------------------

View File

@ -0,0 +1 @@
Return more user-friendly messages when rule functions are fed bad arguments.