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,35 +91,39 @@ 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
case {ok, {SvrAddr, ClientOpts}} ->
emqx_exhook_sup:start_grpc_client_channel( case
Name, emqx_exhook_sup:start_grpc_client_channel(
SvrAddr, Name,
ClientOpts SvrAddr,
) ClientOpts
of )
{ok, _ChannPoolPid} -> of
case do_init(Name, ReqOpts) of {ok, _ChannPoolPid} ->
{ok, HookSpecs} -> case do_init(Name, ReqOpts) of
%% Register metrics {ok, HookSpecs} ->
Prefix = lists:flatten(io_lib:format("exhook.~ts.", [Name])), %% Register metrics
ensure_metrics(Prefix, HookSpecs), Prefix = lists:flatten(io_lib:format("exhook.~ts.", [Name])),
%% Ensure hooks ensure_metrics(Prefix, HookSpecs),
ensure_hooks(HookSpecs), %% Ensure hooks
{ok, #{ ensure_hooks(HookSpecs),
name => Name, {ok, #{
options => ReqOpts, name => Name,
channel => _ChannPoolPid, options => ReqOpts,
hookspec => HookSpecs, channel => _ChannPoolPid,
prefix => Prefix hookspec => HookSpecs,
}}; prefix => Prefix
{error, Reason} -> }};
emqx_exhook_sup:stop_grpc_client_channel(Name), {error, Reason} ->
{load_error, Reason} emqx_exhook_sup:stop_grpc_client_channel(Name),
{load_error, Reason}
end;
{error, _} = E ->
E
end; end;
{error, _} = E -> Error ->
E 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) ->