chore(connector): schema validator throw error messages directly
This commit is contained in:
parent
3b41c841a7
commit
aea4180aa0
|
@ -36,6 +36,4 @@
|
||||||
"The " ++ TYPE ++ " default port " ++ DEFAULT_PORT ++ " is used if `[:Port]` is not specified."
|
"The " ++ TYPE ++ " default port " ++ DEFAULT_PORT ++ " is used if `[:Port]` is not specified."
|
||||||
).
|
).
|
||||||
|
|
||||||
-define(THROW_ERROR(Str), erlang:throw({error, Str})).
|
|
||||||
|
|
||||||
-define(CONNECTOR_RESOURCE_GROUP, <<"emqx_connector">>).
|
-define(CONNECTOR_RESOURCE_GROUP, <<"emqx_connector">>).
|
||||||
|
|
|
@ -115,50 +115,48 @@ ip_port_to_string({Ip, Port}) when is_tuple(Ip) ->
|
||||||
iolist_to_binary([inet:ntoa(Ip), ":", integer_to_list(Port)]).
|
iolist_to_binary([inet:ntoa(Ip), ":", integer_to_list(Port)]).
|
||||||
|
|
||||||
parse_server(Str, #{host_type := inet_addr, default_port := DefaultPort}) ->
|
parse_server(Str, #{host_type := inet_addr, default_port := DefaultPort}) ->
|
||||||
try string:tokens(str(Str), ": ") of
|
case string:tokens(str(Str), ": ") of
|
||||||
[Ip, Port] ->
|
[Ip, Port] ->
|
||||||
case parse_ip(Ip) of
|
{parse_ip(Ip), parse_port(Port)};
|
||||||
{ok, R} -> {R, list_to_integer(Port)}
|
|
||||||
end;
|
|
||||||
[Ip] ->
|
[Ip] ->
|
||||||
case parse_ip(Ip) of
|
{parse_ip(Ip), DefaultPort};
|
||||||
{ok, R} -> {R, DefaultPort}
|
|
||||||
end;
|
|
||||||
_ ->
|
_ ->
|
||||||
?THROW_ERROR("Bad server schema.")
|
throw("Bad server schema")
|
||||||
catch
|
|
||||||
error:Reason ->
|
|
||||||
?THROW_ERROR(Reason)
|
|
||||||
end;
|
end;
|
||||||
parse_server(Str, #{host_type := hostname, default_port := DefaultPort}) ->
|
parse_server(Str, #{host_type := hostname, default_port := DefaultPort}) ->
|
||||||
try string:tokens(str(Str), ": ") of
|
case string:tokens(str(Str), ": ") of
|
||||||
[Ip, Port] ->
|
[Hostname, Port] ->
|
||||||
{Ip, list_to_integer(Port)};
|
{Hostname, parse_port(Port)};
|
||||||
[Ip] ->
|
[Hostname] ->
|
||||||
{Ip, DefaultPort};
|
{Hostname, DefaultPort};
|
||||||
_ ->
|
_ ->
|
||||||
?THROW_ERROR("Bad server schema.")
|
throw("Bad server schema")
|
||||||
catch
|
|
||||||
error:Reason ->
|
|
||||||
?THROW_ERROR(Reason)
|
|
||||||
end;
|
end;
|
||||||
parse_server(_, _) ->
|
parse_server(_, _) ->
|
||||||
?THROW_ERROR("Invalid Host").
|
throw("Invalid Host").
|
||||||
|
|
||||||
parse_ip(Str) ->
|
parse_ip(Str) ->
|
||||||
case inet:parse_address(Str) of
|
case inet:parse_address(Str) of
|
||||||
{ok, R} ->
|
{ok, R} ->
|
||||||
{ok, R};
|
R;
|
||||||
_ ->
|
_ ->
|
||||||
%% check is a rfc1035's hostname
|
%% check is a rfc1035's hostname
|
||||||
case inet_parse:domain(Str) of
|
case inet_parse:domain(Str) of
|
||||||
true ->
|
true ->
|
||||||
{ok, Str};
|
Str;
|
||||||
_ ->
|
_ ->
|
||||||
?THROW_ERROR("Bad IP or Host")
|
throw("Bad IP or Host")
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
parse_port(Port) ->
|
||||||
|
try
|
||||||
|
list_to_integer(Port)
|
||||||
|
catch
|
||||||
|
_:_ ->
|
||||||
|
throw("Bad port number")
|
||||||
|
end.
|
||||||
|
|
||||||
str(A) when is_atom(A) ->
|
str(A) when is_atom(A) ->
|
||||||
atom_to_list(A);
|
atom_to_list(A);
|
||||||
str(B) when is_binary(B) ->
|
str(B) when is_binary(B) ->
|
||||||
|
|
Loading…
Reference in New Issue