Fix Plug.Conn.CookieOverflowError?

I’m trying to add a button/link to initiate an update action in my Phoenix project. The button should update the status in my InjuredReserve resource.

In development, it works as expected. However, in production I get a Plug.Conn.CookieOverflowError with the message cookie named "_ex338_key" exceeds maximum size of 4096 bytes.

Here is the code for the link in my InjuredReserveView file:

      link("Return",
        to:
          Routes.fantasy_league_injured_reserve_path(
            conn,
            :update,
            fantasy_league.id,
            injured_reserve.id,
            %{"injured_reserve" => %{"status" => "returned"}}
          ),
        data: [confirm: "Please confirm to return IR"],
        method: :patch
      )

Here is my InjuredReserveController.update function:

  def update(conn, %{
        "fantasy_league_id" => league_id,
        "id" => id,
        "injured_reserve" => params
      }) do
    injured_reserve = InjuredReserves.get_ir!(id)

    case InjuredReserves.update_injured_reserve(injured_reserve, params) do
      {:ok, %{injured_reserve: _ir}} ->
        conn
        |> put_flash(:info, "IR successfully processed")
        |> redirect(to: Routes.fantasy_league_injured_reserve_path(conn, :index, league_id))

      {:error, _action, error, _} ->
        conn
        |> put_flash(:error, error)
        |> redirect(to: Routes.fantasy_league_injured_reserve_path(conn, :index, league_id))
    end
  end

Is %{"injured_reserve" => %{"status" => "returned"}} too much to send? What is a better way to accomplish this? Thanks!

Axel

I’m looking at this a little more. Should I build a form around the button with a hidden input field for %{“status” => “returned”} rather than a link?

I don’t think that is the issue, but yes. Links should not be used for actions usually.

Are you storing a lot of data in the user session? Plug sessions are stored in the cookie by default.

1 Like

Thanks for helping me focus my efforts! I discovered the error returned from my Ecto.Multi transaction (in the InjuredReserves context) was the changeset struct instead of a string, so adding it to the flash was causing the Plug.Conn.CookieOverflowError.

      {:error, _action, error, _} ->
        conn
        |> put_flash(:error, error)
        |> redirect(to: Routes.fantasy_league_injured_reserve_path(conn, :index, league_id))
1 Like