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 4, 2021
1 parent fc29465 commit a48a52e
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 a48a52e

Please sign in to comment.