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