Skip to content

Commit

Permalink
Add 404 error to user not found (#957)
Browse files Browse the repository at this point in the history
* Add 404 error to user not found

* Add default case for fallback controller
  • Loading branch information
agustinesco authored Sep 13, 2024
1 parent c56927f commit fab9fc1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 2 additions & 3 deletions apps/gateway/lib/gateway/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule Gateway.Controllers.AuthController do
alias Gateway.Auth.GoogleTokenManager
alias GameBackend.Users

action_fallback Gateway.Controllers.FallbackController

def validate_token(conn, %{"provider" => "google", "token_id" => token_id, "client_id" => client_id}) do
case GoogleTokenManager.verify_and_validate(token_id) do
{:ok, claims} ->
Expand Down Expand Up @@ -42,9 +44,6 @@ defmodule Gateway.Controllers.AuthController do
{:ok, user} <- Users.get_user_by_id_and_game_id(claims["sub"], curse_id) do
new_gateway_jwt = TokenManager.generate_user_token(user, client_id)
send_resp(conn, 200, Jason.encode!(%{gateway_jwt: new_gateway_jwt, user_id: user.id}))
else
_ ->
send_resp(conn, 400, Jason.encode!(%{error: "bad_request"}))
end
end
end
8 changes: 8 additions & 0 deletions apps/gateway/lib/gateway/controllers/fallback_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,19 @@ defmodule Gateway.Controllers.FallbackController do
send_resp(conn, 400, Jason.encode!(%{"error" => "the user doesn't have that quest"}))
end

def call(conn, {:error, _failed_operation, :not_found, _changes_so_far}) do
send_resp(conn, 404, Jason.encode!(%{"error" => "not found"}))
end

def call(conn, {:error, failed_operation, _failed_value, _changes_so_far}) when is_binary(failed_operation) do
send_resp(conn, 400, Jason.encode!(%{"error" => failed_operation}))
end

def call(conn, {:error, failed_operation, _failed_value, _changes_so_far}) do
send_resp(conn, 400, Jason.encode!(%{"error" => Atom.to_string(failed_operation)}))
end

def call(conn, _) do
send_resp(conn, 400, Jason.encode!(%{"error" => "Bad request"}))
end
end

0 comments on commit fab9fc1

Please sign in to comment.