Skip to content

Commit

Permalink
Return error 404 instead of 500 if GET command not found
Browse files Browse the repository at this point in the history
This is already done for POST commands though
  • Loading branch information
arcusfelis committed Nov 3, 2021
1 parent fc29465 commit 0f75db5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion big_tests/tests/rest_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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 +206,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 0f75db5

Please sign in to comment.