Skip to content

Commit

Permalink
Merge pull request #3383 from esl/mu-error-404-in-admin-api
Browse files Browse the repository at this point in the history
MIM-1536 Return error 404 instead of 500 if GET command not found
  • Loading branch information
NelsonVides committed Nov 4, 2021
2 parents 7b9379d + a48a52e commit 0abfc12
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions big_tests/tests/rest_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ blank_auth_testcases() ->
test_cases() ->
[commands_are_listed,
non_existent_command_returns404,
existent_command_with_missing_arguments_returns404,
user_can_be_registered_and_removed,
sessions_are_listed,
session_can_be_kicked,
Expand Down Expand Up @@ -206,6 +207,9 @@ commands_are_listed(_C) ->
non_existent_command_returns404(_C) ->
{?NOT_FOUND, _} = gett(admin, <<"/isitthereornot">>).

existent_command_with_missing_arguments_returns404(_C) ->
{?NOT_FOUND, _} = gett(admin, <<"/contacts/">>).

user_can_be_registered_and_removed(_Config) ->
% list users
{?OK, Lusers} = gett(admin, path("users")),
Expand Down
8 changes: 6 additions & 2 deletions src/mongoose_api_admin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,12 @@ to_json(Req, #http_api_state{command_category = Category,
bindings = B} = State) ->
Cmds = mongoose_commands:list(admin, Category, method_to_action(<<"GET">>), SubCategory),
Arity = length(B),
[Command] = [C || C <- Cmds, mongoose_commands:arity(C) == Arity],
process_request(<<"GET">>, Command, Req, State).
case [C || C <- Cmds, mongoose_commands:arity(C) == Arity] of
[Command] ->
process_request(<<"GET">>, Command, Req, State);
[] ->
error_response(not_found, ?ARGS_LEN_ERROR, Req, State)
end.

%% @doc Called for a method of type "POST" and "PUT"
from_json(Req, #http_api_state{command_category = Category,
Expand Down

0 comments on commit 0abfc12

Please sign in to comment.