Gayan Hewa
Gayan Hewa

Reputation: 2397

Phoenix Fallback action error

Elixir / Phoenix Framework noob here.

I am trying to add a new route that would resolve a string that I pass in as a URL segment.

router.ex

# Ping endpoint
scope "/", AppWeb do
 pipe_through :api # Use the default browser stack

 get "/:hash", ListnerController, :ping
end

listner_controller.ex

  def ping(conn, %{"hash" => hash}) do
    with {:ok, resp} <- Monitors.is_valid_hash(hash) do
      render(conn, "hash.json", %{:data => resp})
    end
  end

context - monitor.ex

  def is_valid_hash(hash) do
    hash
  end

I end up getting an error with the Fallback action. The code works fine as long as I don't use the with macro.

enter image description here

Upvotes: 0

Views: 787

Answers (1)

Dogbert
Dogbert

Reputation: 222040

Your request is going to the fallback controller because your with pattern fails which ends up returning hash which is not a Plug.Conn. Returning a value that's not a Plug.Conn is forwarded to the fallback controller by Phoenix. You probably meant to return {:ok, hash} from is_valid_hash:

def is_valid_hash(hash) do
  {:ok, hash}
end

Upvotes: 1

Related Questions