-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: create consistent interface 'with_node' for API access #10237
Merged
sstrigler
merged 1 commit into
emqx:master
from
sstrigler:EMQX-9138-e-5-0-2-ensure-that-the-error-messages-returned-are-consistent
Mar 28, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
%%-------------------------------------------------------------------- | ||
%% Copyright (c) 2021-2023 EMQ Technologies Co., Ltd. All Rights Reserved. | ||
%% | ||
%% Licensed under the Apache License, Version 2.0 (the "License"); | ||
%% you may not use this file except in compliance with the License. | ||
%% You may obtain a copy of the License at | ||
%% | ||
%% http://www.apache.org/licenses/LICENSE-2.0 | ||
%% | ||
%% Unless required by applicable law or agreed to in writing, software | ||
%% distributed under the License is distributed on an "AS IS" BASIS, | ||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
%% See the License for the specific language governing permissions and | ||
%% limitations under the License. | ||
%%-------------------------------------------------------------------- | ||
|
||
-ifndef(EMQX_API_LIB_HRL). | ||
-define(EMQX_API_LIB_HRL, true). | ||
|
||
-define(ERROR_MSG(CODE, REASON), #{code => CODE, message => emqx_misc:readable_error_msg(REASON)}). | ||
|
||
-define(OK(CONTENT), {200, CONTENT}). | ||
sstrigler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
-define(NO_CONTENT, 204). | ||
|
||
-define(BAD_REQUEST(CODE, REASON), {400, ?ERROR_MSG(CODE, REASON)}). | ||
-define(BAD_REQUEST(REASON), ?BAD_REQUEST('BAD_REQUEST', REASON)). | ||
|
||
-define(NOT_FOUND(REASON), {404, ?ERROR_MSG('NOT_FOUND', REASON)}). | ||
|
||
-define(INTERNAL_ERROR(REASON), {500, ?ERROR_MSG('INTERNAL_ERROR', REASON)}). | ||
|
||
-define(NOT_IMPLEMENTED, 501). | ||
|
||
-define(SERVICE_UNAVAILABLE(REASON), {503, ?ERROR_MSG('SERVICE_UNAVAILABLE', REASON)}). | ||
-endif. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
%%-------------------------------------------------------------------- | ||
%% Copyright (c) 2020-2023 EMQ Technologies Co., Ltd. All Rights Reserved. | ||
%% | ||
%% Licensed under the Apache License, Version 2.0 (the "License"); | ||
%% you may not use this file except in compliance with the License. | ||
%% You may obtain a copy of the License at | ||
%% | ||
%% http://www.apache.org/licenses/LICENSE-2.0 | ||
%% | ||
%% Unless required by applicable law or agreed to in writing, software | ||
%% distributed under the License is distributed on an "AS IS" BASIS, | ||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
%% See the License for the specific language governing permissions and | ||
%% limitations under the License. | ||
%%-------------------------------------------------------------------- | ||
|
||
-module(emqx_api_lib). | ||
|
||
-export([ | ||
with_node/2, | ||
with_node_or_cluster/2 | ||
]). | ||
|
||
-include("emqx_api_lib.hrl"). | ||
|
||
-define(NODE_NOT_FOUND(NODE), ?NOT_FOUND(<<"Node not found: ", NODE/binary>>)). | ||
|
||
%%-------------------------------------------------------------------- | ||
%% exported API | ||
%%-------------------------------------------------------------------- | ||
-spec with_node(binary(), fun((atom()) -> {ok, term()} | {error, term()})) -> | ||
?OK(term()) | ?NOT_FOUND(binary()) | ?BAD_REQUEST(term()). | ||
with_node(BinNode, Fun) -> | ||
case lookup_node(BinNode) of | ||
{ok, Node} -> | ||
handle_result(Fun(Node)); | ||
not_found -> | ||
?NODE_NOT_FOUND(BinNode) | ||
end. | ||
|
||
-spec with_node_or_cluster(binary(), fun((atom()) -> {ok, term()} | {error, term()})) -> | ||
?OK(term()) | ?NOT_FOUND(iolist()) | ?BAD_REQUEST(term()). | ||
with_node_or_cluster(<<"all">>, Fun) -> | ||
handle_result(Fun(all)); | ||
with_node_or_cluster(Node, Fun) -> | ||
with_node(Node, Fun). | ||
|
||
%%-------------------------------------------------------------------- | ||
%% Internal | ||
%%-------------------------------------------------------------------- | ||
|
||
-spec lookup_node(binary()) -> {ok, atom()} | not_found. | ||
lookup_node(BinNode) -> | ||
case emqx_misc:safe_to_existing_atom(BinNode, utf8) of | ||
{ok, Node} -> | ||
case lists:member(Node, mria:running_nodes()) of | ||
true -> | ||
{ok, Node}; | ||
false -> | ||
not_found | ||
end; | ||
_Error -> | ||
not_found | ||
end. | ||
|
||
handle_result({ok, Result}) -> | ||
?OK(Result); | ||
handle_result({error, Reason}) -> | ||
?BAD_REQUEST(Reason). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
%%-------------------------------------------------------------------- | ||
%% Copyright (c) 2020-2023 EMQ Technologies Co., Ltd. All Rights Reserved. | ||
%% | ||
%% Licensed under the Apache License, Version 2.0 (the "License"); | ||
%% you may not use this file except in compliance with the License. | ||
%% You may obtain a copy of the License at | ||
%% | ||
%% http://www.apache.org/licenses/LICENSE-2.0 | ||
%% | ||
%% Unless required by applicable law or agreed to in writing, software | ||
%% distributed under the License is distributed on an "AS IS" BASIS, | ||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
%% See the License for the specific language governing permissions and | ||
%% limitations under the License. | ||
%%-------------------------------------------------------------------- | ||
|
||
-module(emqx_api_lib_SUITE). | ||
|
||
-compile(export_all). | ||
-compile(nowarn_export_all). | ||
|
||
-include("emqx_api_lib.hrl"). | ||
-include_lib("eunit/include/eunit.hrl"). | ||
|
||
-define(DUMMY, dummy_module). | ||
|
||
all() -> emqx_common_test_helpers:all(?MODULE). | ||
|
||
init_per_suite(Config) -> | ||
emqx_common_test_helpers:boot_modules(all), | ||
emqx_common_test_helpers:start_apps([]), | ||
Config. | ||
|
||
end_per_suite(_Config) -> | ||
emqx_common_test_helpers:stop_apps([]). | ||
|
||
init_per_testcase(_Case, Config) -> | ||
meck:new(?DUMMY, [non_strict]), | ||
meck:expect(?DUMMY, expect_not_called, 1, fun(Node) -> throw({blow_this_up, Node}) end), | ||
meck:expect(?DUMMY, expect_success, 1, {ok, success}), | ||
meck:expect(?DUMMY, expect_error, 1, {error, error}), | ||
Config. | ||
|
||
end_per_testcase(_Case, _Config) -> | ||
meck:unload(?DUMMY). | ||
|
||
t_with_node(_) -> | ||
test_with(fun emqx_api_lib:with_node/2, [<<"all">>]). | ||
|
||
t_with_node_or_cluster(_) -> | ||
test_with(fun emqx_api_lib:with_node_or_cluster/2, []), | ||
meck:reset(?DUMMY), | ||
?assertEqual( | ||
?OK(success), | ||
emqx_api_lib:with_node_or_cluster( | ||
<<"all">>, | ||
fun ?DUMMY:expect_success/1 | ||
) | ||
), | ||
?assertMatch([{_, {?DUMMY, expect_success, [all]}, {ok, success}}], meck:history(?DUMMY)). | ||
|
||
%% helpers | ||
test_with(TestFun, ExtraBadNodes) -> | ||
% make sure this is an atom | ||
'unknownnode@unknownnohost', | ||
BadNodes = | ||
[ | ||
<<"undefined">>, | ||
<<"this_should_not_be_an_atom">>, | ||
<<"unknownnode@unknownnohost">> | ||
] ++ ExtraBadNodes, | ||
[ensure_not_found(TestFun(N, fun ?DUMMY:expect_not_called/1)) || N <- BadNodes], | ||
ensure_not_called(?DUMMY, expect_not_called), | ||
ensure_not_existing_atom(<<"this_should_not_be_an_atom">>), | ||
|
||
GoodNode = node(), | ||
|
||
?assertEqual( | ||
?OK(success), | ||
TestFun(GoodNode, fun ?DUMMY:expect_success/1) | ||
), | ||
|
||
?assertEqual( | ||
?BAD_REQUEST(error), | ||
TestFun(GoodNode, fun ?DUMMY:expect_error/1) | ||
), | ||
ok. | ||
|
||
ensure_not_found(Result) -> | ||
?assertMatch({404, _}, Result). | ||
|
||
ensure_not_called(Mod, Fun) -> | ||
?assert(not meck:called(Mod, Fun, '_')). | ||
|
||
ensure_not_existing_atom(Bin) -> | ||
try binary_to_existing_atom(Bin) of | ||
_ -> throw(is_atom) | ||
catch | ||
error:badarg -> | ||
ok | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Maybe for future discussion): I always wondered why we should have all the responses as macros. We do not match against them anywhere, and the performance impact should be neglectable 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it vastly improves readability and consistency, my goal rather is to ONLY use those macros throughout than constructing all those responses by hand.
Or is it, that you would favor function calls instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Also I match against them in my tests)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I mean
vs