fix(api): emqx_connector_api error code format

This commit is contained in:
DDDHuang 2022-02-18 10:43:32 +08:00
parent 90ee450a84
commit 98a11f3c15
3 changed files with 28 additions and 20 deletions

View File

@ -27,6 +27,7 @@
-define(INVALID_PARAMETER, 'INVALID_PARAMETER'). -define(INVALID_PARAMETER, 'INVALID_PARAMETER').
-define(CONFLICT, 'CONFLICT'). -define(CONFLICT, 'CONFLICT').
-define(NO_DEFAULT_VALUE, 'NO_DEFAULT_VALUE'). -define(NO_DEFAULT_VALUE, 'NO_DEFAULT_VALUE').
-define(DEPENDENCY_EXISTS, 'DEPENDENCY_EXISTS').
-define(MESSAGE_ID_SCHEMA_ERROR, 'MESSAGE_ID_SCHEMA_ERROR'). -define(MESSAGE_ID_SCHEMA_ERROR, 'MESSAGE_ID_SCHEMA_ERROR').
%% Resource Not Found %% Resource Not Found
@ -58,6 +59,7 @@
, {'INVALID_PARAMETER', <<"Request parameters is not legal and exceeds the boundary value">>} , {'INVALID_PARAMETER', <<"Request parameters is not legal and exceeds the boundary value">>}
, {'CONFLICT', <<"Conflicting request resources">>} , {'CONFLICT', <<"Conflicting request resources">>}
, {'NO_DEFAULT_VALUE', <<"Request parameters do not use default values">>} , {'NO_DEFAULT_VALUE', <<"Request parameters do not use default values">>}
, {'DEPENDENCY_EXISTS', <<"Resource is dependent by another resource">>}
, {'MESSAGE_ID_SCHEMA_ERROR', <<"Message ID parsing error">>} , {'MESSAGE_ID_SCHEMA_ERROR', <<"Message ID parsing error">>}
, {'MESSAGE_ID_NOT_FOUND', <<"Message ID does not exist">>} , {'MESSAGE_ID_NOT_FOUND', <<"Message ID does not exist">>}
, {'NOT_FOUND', <<"Resource was not found or does not exist">>} , {'NOT_FOUND', <<"Resource was not found or does not exist">>}

View File

@ -50,10 +50,10 @@ api_spec() ->
paths() -> ["/connectors_test", "/connectors", "/connectors/:id"]. paths() -> ["/connectors_test", "/connectors", "/connectors/:id"].
error_schema(Code, Message) -> error_schema(Codes, Message) when is_list(Message) ->
[ {code, mk(string(), #{example => Code})} error_schema(Codes, list_to_binary(Message));
, {message, mk(string(), #{example => Message})} error_schema(Codes, Message) when is_binary(Message) ->
]. emqx_dashboard_swagger:error_codes(Codes, Message).
put_request_body_schema() -> put_request_body_schema() ->
emqx_dashboard_swagger:schema_with_examples( emqx_dashboard_swagger:schema_with_examples(
@ -134,8 +134,8 @@ schema("/connectors_test") ->
summary => <<"Test creating connector">>, summary => <<"Test creating connector">>,
requestBody => post_request_body_schema(), requestBody => post_request_body_schema(),
responses => #{ responses => #{
200 => <<"Test connector OK">>, 204 => <<"Test connector OK">>,
400 => error_schema('TEST_FAILED', "connector test failed") 400 => error_schema(['TEST_FAILED'], "connector test failed")
} }
} }
}; };
@ -160,7 +160,7 @@ schema("/connectors") ->
requestBody => post_request_body_schema(), requestBody => post_request_body_schema(),
responses => #{ responses => #{
201 => get_response_body_schema(), 201 => get_response_body_schema(),
400 => error_schema('ALREADY_EXISTS', "connector already exists") 400 => error_schema(['ALREADY_EXISTS'], "connector already exists")
} }
} }
}; };
@ -175,7 +175,7 @@ schema("/connectors/:id") ->
parameters => param_path_id(), parameters => param_path_id(),
responses => #{ responses => #{
200 => get_response_body_schema(), 200 => get_response_body_schema(),
404 => error_schema('NOT_FOUND', "Connector not found") 404 => error_schema(['NOT_FOUND'], "Connector not found")
} }
}, },
put => #{ put => #{
@ -186,8 +186,7 @@ schema("/connectors/:id") ->
requestBody => put_request_body_schema(), requestBody => put_request_body_schema(),
responses => #{ responses => #{
200 => get_response_body_schema(), 200 => get_response_body_schema(),
400 => error_schema('UPDATE_FAIL', "Update failed"), 404 => error_schema(['NOT_FOUND'], "Connector not found")
404 => error_schema('NOT_FOUND', "Connector not found")
}}, }},
delete => #{ delete => #{
tags => [<<"connectors">>], tags => [<<"connectors">>],
@ -196,15 +195,17 @@ schema("/connectors/:id") ->
parameters => param_path_id(), parameters => param_path_id(),
responses => #{ responses => #{
204 => <<"Delete connector successfully">>, 204 => <<"Delete connector successfully">>,
400 => error_schema('DELETE_FAIL', "Delete failed") 403 => error_schema(['DEPENDENCY_EXISTS'], "Cannot remove dependent connector"),
404 => error_schema(['NOT_FOUND'], "Delete failed, not found")
}} }}
}. }.
'/connectors_test'(post, #{body := #{<<"type">> := ConnType} = Params}) -> '/connectors_test'(post, #{body := #{<<"type">> := ConnType} = Params}) ->
case emqx_connector:create_dry_run(ConnType, maps:remove(<<"type">>, Params)) of case emqx_connector:create_dry_run(ConnType, maps:remove(<<"type">>, Params)) of
ok -> {200}; ok ->
{204};
{error, Error} -> {error, Error} ->
{400, error_msg('BAD_ARG', Error)} {400, error_msg(['TEST_FAILED'], Error)}
end. end.
'/connectors'(get, _Request) -> '/connectors'(get, _Request) ->
@ -221,14 +222,16 @@ schema("/connectors/:id") ->
{ok, #{raw_config := RawConf}} -> {ok, #{raw_config := RawConf}} ->
Id = emqx_connector:connector_id(ConnType, ConnName), Id = emqx_connector:connector_id(ConnType, ConnName),
{201, format_resp(Id, RawConf)}; {201, format_resp(Id, RawConf)};
{error, Error} -> {400, error_msg('BAD_ARG', Error)} {error, Error} ->
{400, error_msg('ALREADY_EXISTS', Error)}
end end
end. end.
'/connectors/:id'(get, #{bindings := #{id := Id}}) -> '/connectors/:id'(get, #{bindings := #{id := Id}}) ->
?TRY_PARSE_ID(Id, ?TRY_PARSE_ID(Id,
case emqx_connector:lookup(ConnType, ConnName) of case emqx_connector:lookup(ConnType, ConnName) of
{ok, Conf} -> {200, format_resp(Id, Conf)}; {ok, Conf} ->
{200, format_resp(Id, Conf)};
{error, not_found} -> {error, not_found} ->
{404, error_msg('NOT_FOUND', <<"connector not found">>)} {404, error_msg('NOT_FOUND', <<"connector not found">>)}
end); end);
@ -241,7 +244,8 @@ schema("/connectors/:id") ->
case emqx_connector:update(ConnType, ConnName, Params) of case emqx_connector:update(ConnType, ConnName, Params) of
{ok, #{raw_config := RawConf}} -> {ok, #{raw_config := RawConf}} ->
{200, format_resp(Id, RawConf)}; {200, format_resp(Id, RawConf)};
{error, Error} -> {400, error_msg('BAD_ARG', Error)} {error, Error} ->
{500, error_msg('INTERNAL_ERROR', Error)}
end; end;
{error, not_found} -> {error, not_found} ->
{404, error_msg('NOT_FOUND', <<"connector not found">>)} {404, error_msg('NOT_FOUND', <<"connector not found">>)}
@ -252,12 +256,14 @@ schema("/connectors/:id") ->
case emqx_connector:lookup(ConnType, ConnName) of case emqx_connector:lookup(ConnType, ConnName) of
{ok, _} -> {ok, _} ->
case emqx_connector:delete(ConnType, ConnName) of case emqx_connector:delete(ConnType, ConnName) of
{ok, _} -> {204}; {ok, _} ->
{204};
{error, {post_config_update, _, {dependency_bridges_exist, BridgeID}}} -> {error, {post_config_update, _, {dependency_bridges_exist, BridgeID}}} ->
{403, error_msg('DEPENDENCY_EXISTS', {403, error_msg('DEPENDENCY_EXISTS',
<<"Cannot remove the connector as it's in use by a bridge: ", <<"Cannot remove the connector as it's in use by a bridge: ",
BridgeID/binary>>)}; BridgeID/binary>>)};
{error, Error} -> {400, error_msg('BAD_ARG', Error)} {error, Error} ->
{500, error_msg('INTERNAL_ERROR', Error)}
end; end;
{error, not_found} -> {error, not_found} ->
{404, error_msg('NOT_FOUND', <<"connector not found">>)} {404, error_msg('NOT_FOUND', <<"connector not found">>)}

View File

@ -377,7 +377,7 @@ t_mqtt_conn_update(_) ->
%% then we try to update 'server' of the connector, to an unavailable IP address %% then we try to update 'server' of the connector, to an unavailable IP address
%% the update should fail because of 'unreachable' or 'connrefused' %% the update should fail because of 'unreachable' or 'connrefused'
{ok, 400, _ErrorMsg} = request(put, uri(["connectors", ConnctorID]), {ok, 500, _ErrorMsg} = request(put, uri(["connectors", ConnctorID]),
?MQTT_CONNECTOR2(<<"127.0.0.1:2603">>)), ?MQTT_CONNECTOR2(<<"127.0.0.1:2603">>)),
%% we fix the 'server' parameter to a normal one, it should work %% we fix the 'server' parameter to a normal one, it should work
{ok, 200, _} = request(put, uri(["connectors", ConnctorID]), {ok, 200, _} = request(put, uri(["connectors", ConnctorID]),
@ -468,7 +468,7 @@ t_mqtt_conn_update3(_) ->
t_mqtt_conn_testing(_) -> t_mqtt_conn_testing(_) ->
%% APIs for testing the connectivity %% APIs for testing the connectivity
%% then we add a mqtt connector, using POST %% then we add a mqtt connector, using POST
{ok, 200, <<>>} = request(post, uri(["connectors_test"]), {ok, 204, <<>>} = request(post, uri(["connectors_test"]),
?MQTT_CONNECTOR2(<<"127.0.0.1:1883">>)#{ ?MQTT_CONNECTOR2(<<"127.0.0.1:1883">>)#{
<<"type">> => ?CONNECTR_TYPE, <<"type">> => ?CONNECTR_TYPE,
<<"name">> => ?BRIDGE_NAME_EGRESS <<"name">> => ?BRIDGE_NAME_EGRESS