fix(rule-engine): reply 500 when update resource failed (#4120)
This commit is contained in:
parent
a3206f5e0b
commit
6eff70a646
|
@ -300,7 +300,7 @@ do_update_resource(#{id := Id, type := Type, description := NewDescription, conf
|
|||
cluster_call(init_resource, [Module, Create, Id, Config]),
|
||||
emqx_rule_registry:add_resource(Resource);
|
||||
{error, Reason} ->
|
||||
{error, Reason}
|
||||
error({error, Reason})
|
||||
end
|
||||
end.
|
||||
|
||||
|
|
|
@ -273,6 +273,8 @@ do_create_resource(Create, ParsedParams) ->
|
|||
return({ok, record_to_map(Resource)});
|
||||
{error, {resource_type_not_found, Type}} ->
|
||||
return({error, 400, ?ERR_NO_RESOURCE_TYPE(Type)});
|
||||
{error, {init_resource_failure, _}} ->
|
||||
return({error, 500, <<"Init resource failure!">>});
|
||||
{error, Reason} ->
|
||||
?LOG(error, "~p failed: ~0p", [?FUNCTION_NAME, Reason]),
|
||||
return({error, 400, ?ERR_BADARGS(Reason)})
|
||||
|
|
|
@ -196,6 +196,10 @@ on_resource_create(ResId, Conf) ->
|
|||
{ok, _} = application:ensure_all_started(ehttpc),
|
||||
Options = pool_opts(Conf),
|
||||
PoolName = pool_name(ResId),
|
||||
case test_http_connect(Conf) of
|
||||
true -> ok;
|
||||
false -> error({error, check_http_connectivity_failed})
|
||||
end,
|
||||
start_resource(ResId, PoolName, Options),
|
||||
Conf#{<<"pool">> => PoolName, options => Options}.
|
||||
|
||||
|
@ -214,15 +218,8 @@ start_resource(ResId, PoolName, Options) ->
|
|||
end.
|
||||
|
||||
-spec(on_get_resource_status(binary(), map()) -> map()).
|
||||
on_get_resource_status(ResId, #{<<"url">> := Url}) ->
|
||||
#{is_alive =>
|
||||
case emqx_rule_utils:http_connectivity(Url) of
|
||||
ok -> true;
|
||||
{error, Reason} ->
|
||||
?LOG(error, "Connectivity Check for ~p failed, ResId: ~p, ~0p",
|
||||
[?RESOURCE_TYPE_WEBHOOK, ResId, Reason]),
|
||||
false
|
||||
end}.
|
||||
on_get_resource_status(_ResId, Conf) ->
|
||||
#{is_alive => test_http_connect(Conf)}.
|
||||
|
||||
-spec(on_resource_destroy(binary(), map()) -> ok | {error, Reason::term()}).
|
||||
on_resource_destroy(ResId, #{<<"pool">> := PoolName}) ->
|
||||
|
@ -386,3 +383,18 @@ parse_host(Host) ->
|
|||
{error, _} -> {inet, Host}
|
||||
end
|
||||
end.
|
||||
|
||||
test_http_connect(Conf) ->
|
||||
Url = fun() -> maps:get(<<"url">>, Conf) end,
|
||||
try
|
||||
emqx_rule_utils:http_connectivity(Url())
|
||||
of
|
||||
ok -> true;
|
||||
{error, _Reason} ->
|
||||
?LOG(error, "check http_connectivity failed: ~p", [Url()]),
|
||||
false
|
||||
catch
|
||||
Err:Reason:ST ->
|
||||
?LOG(error, "check http_connectivity failed: ~p, ~0p", [Conf, {Err, Reason, ST}]),
|
||||
false
|
||||
end.
|
Loading…
Reference in New Issue