refactor: move catch to dry run fn
This commit is contained in:
parent
4b8cea4498
commit
29e706c83d
|
@ -541,30 +541,23 @@ schema("/bridges_probe") ->
|
||||||
case emqx_dashboard_swagger:filter_check_request_and_translate_body(Request, RequestMeta) of
|
case emqx_dashboard_swagger:filter_check_request_and_translate_body(Request, RequestMeta) of
|
||||||
{ok, #{body := #{<<"type">> := ConnType} = Params}} ->
|
{ok, #{body := #{<<"type">> := ConnType} = Params}} ->
|
||||||
Params1 = maybe_deobfuscate_bridge_probe(Params),
|
Params1 = maybe_deobfuscate_bridge_probe(Params),
|
||||||
try emqx_bridge_resource:create_dry_run(ConnType, maps:remove(<<"type">>, Params1)) of
|
case emqx_bridge_resource:create_dry_run(ConnType, maps:remove(<<"type">>, Params1)) of
|
||||||
ok ->
|
ok ->
|
||||||
?NO_CONTENT;
|
?NO_CONTENT;
|
||||||
{error, #{kind := validation_error} = Reason} ->
|
{error, #{kind := validation_error} = Reason0} ->
|
||||||
|
Reason = redact(Reason0),
|
||||||
?BAD_REQUEST('TEST_FAILED', map_to_json(Reason));
|
?BAD_REQUEST('TEST_FAILED', map_to_json(Reason));
|
||||||
{error, Reason0} when not is_tuple(Reason0); element(1, Reason0) =/= 'exit' ->
|
{error, Reason0} when not is_tuple(Reason0); element(1, Reason0) =/= 'exit' ->
|
||||||
Reason =
|
Reason1 =
|
||||||
case Reason0 of
|
case Reason0 of
|
||||||
{unhealthy_target, Message} -> Message;
|
{unhealthy_target, Message} -> Message;
|
||||||
_ -> Reason0
|
_ -> Reason0
|
||||||
end,
|
end,
|
||||||
|
Reason = redact(Reason1),
|
||||||
?BAD_REQUEST('TEST_FAILED', Reason)
|
?BAD_REQUEST('TEST_FAILED', Reason)
|
||||||
catch
|
|
||||||
%% We need to catch hocon validation errors here as well because,
|
|
||||||
%% currently, when defining the API union member selector, we can only use
|
|
||||||
%% references to fields, and they don't share whole-bridge validators if
|
|
||||||
%% they exist. Such validators will only be triggered by
|
|
||||||
%% `create_dry_run'...
|
|
||||||
throw:{_Schema, [#{kind := _} = Reason0 | _]} ->
|
|
||||||
Reason = redact(Reason0),
|
|
||||||
?BAD_REQUEST('TEST_FAILED', map_to_json(Reason))
|
|
||||||
end;
|
end;
|
||||||
BadRequest ->
|
BadRequest ->
|
||||||
BadRequest
|
redact(BadRequest)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
maybe_deobfuscate_bridge_probe(#{<<"type">> := BridgeType, <<"name">> := BridgeName} = Params) ->
|
maybe_deobfuscate_bridge_probe(#{<<"type">> := BridgeType, <<"name">> := BridgeName} = Params) ->
|
||||||
|
@ -999,7 +992,9 @@ call_operation(NodeOrAll, OperFunc, Args = [_Nodes, BridgeType, BridgeName]) ->
|
||||||
{error, timeout} ->
|
{error, timeout} ->
|
||||||
?BAD_REQUEST(<<"Request timeout">>);
|
?BAD_REQUEST(<<"Request timeout">>);
|
||||||
{error, {start_pool_failed, Name, Reason}} ->
|
{error, {start_pool_failed, Name, Reason}} ->
|
||||||
Msg = bin(io_lib:format("Failed to start ~p pool for reason ~p", [Name, Reason])),
|
Msg = bin(
|
||||||
|
io_lib:format("Failed to start ~p pool for reason ~p", [Name, redact(Reason)])
|
||||||
|
),
|
||||||
?BAD_REQUEST(Msg);
|
?BAD_REQUEST(Msg);
|
||||||
{error, not_found} ->
|
{error, not_found} ->
|
||||||
BridgeId = emqx_bridge_resource:bridge_id(BridgeType, BridgeName),
|
BridgeId = emqx_bridge_resource:bridge_id(BridgeType, BridgeName),
|
||||||
|
@ -1016,7 +1011,7 @@ call_operation(NodeOrAll, OperFunc, Args = [_Nodes, BridgeType, BridgeName]) ->
|
||||||
{error, {unhealthy_target, Message}} ->
|
{error, {unhealthy_target, Message}} ->
|
||||||
?BAD_REQUEST(Message);
|
?BAD_REQUEST(Message);
|
||||||
{error, Reason} when not is_tuple(Reason); element(1, Reason) =/= 'exit' ->
|
{error, Reason} when not is_tuple(Reason); element(1, Reason) =/= 'exit' ->
|
||||||
?BAD_REQUEST(Reason)
|
?BAD_REQUEST(redact(Reason))
|
||||||
end.
|
end.
|
||||||
|
|
||||||
maybe_try_restart(all, start_bridges_to_all_nodes, Args) ->
|
maybe_try_restart(all, start_bridges_to_all_nodes, Args) ->
|
||||||
|
@ -1089,6 +1084,6 @@ map_to_json(M0) ->
|
||||||
emqx_utils_json:encode(M1)
|
emqx_utils_json:encode(M1)
|
||||||
catch
|
catch
|
||||||
error:_ ->
|
error:_ ->
|
||||||
M2 = maps:without([value], M1),
|
M2 = maps:without([value, <<"value">>], M1),
|
||||||
emqx_utils_json:encode(M2)
|
emqx_utils_json:encode(M2)
|
||||||
end.
|
end.
|
||||||
|
|
|
@ -266,6 +266,7 @@ create_dry_run(Type, Conf0) ->
|
||||||
TypeAtom = safe_atom(Type),
|
TypeAtom = safe_atom(Type),
|
||||||
Conf1 = maps:without([<<"name">>], Conf0),
|
Conf1 = maps:without([<<"name">>], Conf0),
|
||||||
RawConf = #{<<"bridges">> => #{TypeBin => #{<<"temp_name">> => Conf1}}},
|
RawConf = #{<<"bridges">> => #{TypeBin => #{<<"temp_name">> => Conf1}}},
|
||||||
|
try
|
||||||
#{bridges := #{TypeAtom := #{temp_name := Conf}}} =
|
#{bridges := #{TypeAtom := #{temp_name := Conf}}} =
|
||||||
hocon_tconf:check_plain(
|
hocon_tconf:check_plain(
|
||||||
emqx_bridge_schema,
|
emqx_bridge_schema,
|
||||||
|
@ -276,16 +277,15 @@ create_dry_run(Type, Conf0) ->
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
{error, Reason};
|
{error, Reason};
|
||||||
{ok, ConfNew} ->
|
{ok, ConfNew} ->
|
||||||
try
|
|
||||||
ParseConf = parse_confs(bin(Type), TmpName, ConfNew),
|
ParseConf = parse_confs(bin(Type), TmpName, ConfNew),
|
||||||
emqx_resource:create_dry_run_local(bridge_to_resource_type(Type), ParseConf)
|
emqx_resource:create_dry_run_local(bridge_to_resource_type(Type), ParseConf)
|
||||||
|
end
|
||||||
catch
|
catch
|
||||||
%% validation errors
|
%% validation errors
|
||||||
throw:Reason ->
|
throw:Reason1 ->
|
||||||
{error, Reason}
|
{error, Reason1}
|
||||||
after
|
after
|
||||||
_ = file:del_dir_r(emqx_tls_lib:pem_dir(TmpPath))
|
_ = file:del_dir_r(emqx_tls_lib:pem_dir(TmpPath))
|
||||||
end
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
remove(BridgeId) ->
|
remove(BridgeId) ->
|
||||||
|
|
|
@ -1090,7 +1090,7 @@ t_strategy_key_validation(Config) ->
|
||||||
<<"kind">> := <<"validation_error">>,
|
<<"kind">> := <<"validation_error">>,
|
||||||
<<"reason">> := <<"Message key cannot be empty", _/binary>>
|
<<"reason">> := <<"Message key cannot be empty", _/binary>>
|
||||||
} = Msg
|
} = Msg
|
||||||
}}} when not is_map_key(<<"value">>, Msg),
|
}}},
|
||||||
probe_bridge_api(
|
probe_bridge_api(
|
||||||
Config,
|
Config,
|
||||||
#{<<"strategy">> => <<"key_dispatch">>, <<"message">> => #{<<"key">> => <<>>}}
|
#{<<"strategy">> => <<"key_dispatch">>, <<"message">> => #{<<"key">> => <<>>}}
|
||||||
|
@ -1104,7 +1104,7 @@ t_strategy_key_validation(Config) ->
|
||||||
<<"kind">> := <<"validation_error">>,
|
<<"kind">> := <<"validation_error">>,
|
||||||
<<"reason">> := <<"Message key cannot be empty", _/binary>>
|
<<"reason">> := <<"Message key cannot be empty", _/binary>>
|
||||||
} = Msg
|
} = Msg
|
||||||
}}} when not is_map_key(<<"value">>, Msg),
|
}}},
|
||||||
create_bridge_api(
|
create_bridge_api(
|
||||||
Config,
|
Config,
|
||||||
#{<<"strategy">> => <<"key_dispatch">>, <<"message">> => #{<<"key">> => <<>>}}
|
#{<<"strategy">> => <<"key_dispatch">>, <<"message">> => #{<<"key">> => <<>>}}
|
||||||
|
|
Loading…
Reference in New Issue