fix(exhook): use error code to replace exception in the URL parse

we should return 400 and why to the API caller, not crash and return 500
This commit is contained in:
firest 2022-04-18 11:54:41 +08:00
parent f20af91ba0
commit af3519698c
1 changed files with 35 additions and 31 deletions

View File

@ -91,7 +91,8 @@ load(_Name, #{enable := false}) ->
disable;
load(Name, #{request_timeout := Timeout, failed_action := FailedAction} = Opts) ->
ReqOpts = #{timeout => Timeout, failed_action => FailedAction},
{SvrAddr, ClientOpts} = channel_opts(Opts),
case channel_opts(Opts) of
{ok, {SvrAddr, ClientOpts}} ->
case
emqx_exhook_sup:start_grpc_client_channel(
Name,
@ -120,6 +121,9 @@ load(Name, #{request_timeout := Timeout, failed_action := FailedAction} = Opts)
end;
{error, _} = E ->
E
end;
Error ->
Error
end.
%% @private
@ -130,7 +134,7 @@ channel_opts(Opts = #{url := URL}) ->
),
case uri_string:parse(URL) of
#{scheme := <<"http">>, host := Host, port := Port} ->
{format_http_uri("http", Host, Port), ClientOpts};
{ok, {format_http_uri("http", Host, Port), ClientOpts}};
#{scheme := <<"https">>, host := Host, port := Port} ->
SslOpts =
case maps:get(ssl, Opts, undefined) of
@ -154,9 +158,9 @@ channel_opts(Opts = #{url := URL}) ->
transport_opts => SslOpts
}
},
{format_http_uri("https", Host, Port), NClientOpts};
{ok, {format_http_uri("https", Host, Port), NClientOpts}};
Error ->
error({bad_server_url, URL, Error})
{error, {bad_server_url, URL, Error}}
end.
format_http_uri(Scheme, Host, Port) ->