feat(emqx_rule_engine): API format errors in a human readable way
This commit is contained in:
parent
806503e36e
commit
64b5e9585e
|
@ -423,7 +423,12 @@ param_path_id() ->
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
err_msg(Msg) -> emqx_misc:readable_error_msg(Msg).
|
err_msg({RuleError, {_E, R, _S}}) when is_tuple(R) ->
|
||||||
|
emqx_misc:readable_error_msg(emqx_json:encode([{RuleError, element(1, R)}]));
|
||||||
|
err_msg({RuleError, {_E, R, _S}}) ->
|
||||||
|
emqx_misc:readable_error_msg(emqx_json:encode([{RuleError, R}]));
|
||||||
|
err_msg(Msg) ->
|
||||||
|
emqx_misc:readable_error_msg(Msg).
|
||||||
|
|
||||||
format_rule_resp(Rules) when is_list(Rules) ->
|
format_rule_resp(Rules) when is_list(Rules) ->
|
||||||
[format_rule_resp(R) || R <- Rules];
|
[format_rule_resp(R) || R <- Rules];
|
||||||
|
|
|
@ -119,12 +119,31 @@ t_crud_rule_api(_Config) ->
|
||||||
emqx_rule_engine_api:'/rules/:id'(get, #{bindings => #{id => RuleID}})
|
emqx_rule_engine_api:'/rules/:id'(get, #{bindings => #{id => RuleID}})
|
||||||
),
|
),
|
||||||
|
|
||||||
?assertMatch(
|
|
||||||
{400, #{
|
{400, #{
|
||||||
code := 'BAD_REQUEST',
|
code := 'BAD_REQUEST',
|
||||||
message := <<"{select_and_transform_error,{error,{decode_json_failed,", _/binary>>
|
message := SelectAndTransformJsonError
|
||||||
}},
|
}} =
|
||||||
emqx_rule_engine_api:'/rule_test'(post, test_rule_params())
|
emqx_rule_engine_api:'/rule_test'(
|
||||||
|
post,
|
||||||
|
test_rule_params(<<"SELECT\n payload.msg\nFROM\n \"t/#\"">>, <<"{\"msg\": \"hel">>)
|
||||||
|
),
|
||||||
|
?assertMatch(
|
||||||
|
#{<<"select_and_transform_error">> := <<"decode_json_failed">>},
|
||||||
|
emqx_json:decode(SelectAndTransformJsonError, [return_maps])
|
||||||
|
),
|
||||||
|
{400, #{
|
||||||
|
code := 'BAD_REQUEST',
|
||||||
|
message := SelectAndTransformBadArgError
|
||||||
|
}} =
|
||||||
|
emqx_rule_engine_api:'/rule_test'(
|
||||||
|
post,
|
||||||
|
test_rule_params(
|
||||||
|
<<"SELECT\n payload.msg > 1\nFROM\n \"t/#\"">>, <<"{\"msg\": \"hello\"}">>
|
||||||
|
)
|
||||||
|
),
|
||||||
|
?assertMatch(
|
||||||
|
#{<<"select_and_transform_error">> := <<"badarg">>},
|
||||||
|
emqx_json:decode(SelectAndTransformBadArgError, [return_maps])
|
||||||
),
|
),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
@ -221,19 +240,18 @@ t_reset_metrics_on_disable(_Config) ->
|
||||||
?assertMatch(#{passed := 0, matched := 0}, Metrics1),
|
?assertMatch(#{passed := 0, matched := 0}, Metrics1),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
test_rule_params() ->
|
test_rule_params(Sql, Payload) ->
|
||||||
#{
|
#{
|
||||||
body => #{
|
body => #{
|
||||||
<<"context">> =>
|
<<"context">> =>
|
||||||
#{
|
#{
|
||||||
<<"clientid">> => <<"c_emqx">>,
|
<<"clientid">> => <<"c_emqx">>,
|
||||||
<<"event_type">> => <<"message_publish">>,
|
<<"event_type">> => <<"message_publish">>,
|
||||||
<<"payload">> => <<"{\"msg\": \"hel">>,
|
<<"payload">> => Payload,
|
||||||
<<"qos">> => 1,
|
<<"qos">> => 1,
|
||||||
<<"topic">> => <<"t/a">>,
|
<<"topic">> => <<"t/a">>,
|
||||||
<<"username">> => <<"u_emqx">>
|
<<"username">> => <<"u_emqx">>
|
||||||
},
|
},
|
||||||
<<"sql">> =>
|
<<"sql">> => Sql
|
||||||
<<"SELECT\n payload.msg\nFROM\n \"t/#\"">>
|
|
||||||
}
|
}
|
||||||
}.
|
}.
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Errors returned by rule engine API are formatted in a more human readable way rather than dumping the raw error including the stacktrace.
|
|
@ -0,0 +1 @@
|
||||||
|
由规则引擎 API 返回的错误以更人性化的方式格式化,而不是转储包括堆栈跟踪的原始错误。
|
Loading…
Reference in New Issue