From f479be57287006eacf567c68d3083dab7410cf7d Mon Sep 17 00:00:00 2001 From: jamey Date: Sat, 28 Feb 2026 08:35:01 +0000 Subject: [PATCH] show proper 404 page for missing custom pages instead of redirecting Co-Authored-By: Claude Opus 4.6 --- lib/berrypod_web/live/shop/custom_page.ex | 16 +++------------- lib/berrypod_web/not_found_error.ex | 10 ++++++++++ test/berrypod_web/live/shop/custom_page_test.exs | 12 ++++++++---- 3 files changed, 21 insertions(+), 17 deletions(-) create mode 100644 lib/berrypod_web/not_found_error.ex diff --git a/lib/berrypod_web/live/shop/custom_page.ex b/lib/berrypod_web/live/shop/custom_page.ex index a255c42..d624fa5 100644 --- a/lib/berrypod_web/live/shop/custom_page.ex +++ b/lib/berrypod_web/live/shop/custom_page.ex @@ -15,23 +15,13 @@ defmodule BerrypodWeb.Shop.CustomPage do cond do is_nil(page) -> record_broken_url("/#{slug}") - - {:noreply, - socket - |> put_flash(:error, "Page not found") - |> push_navigate(to: ~p"/")} + raise BerrypodWeb.NotFoundError page.type != "custom" -> - {:noreply, - socket - |> put_flash(:error, "Page not found") - |> push_navigate(to: ~p"/")} + raise BerrypodWeb.NotFoundError page.published != true and not socket.assigns.is_admin -> - {:noreply, - socket - |> put_flash(:error, "Page not found") - |> push_navigate(to: ~p"/")} + raise BerrypodWeb.NotFoundError true -> extra = Pages.load_block_data(page.blocks, socket.assigns) diff --git a/lib/berrypod_web/not_found_error.ex b/lib/berrypod_web/not_found_error.ex new file mode 100644 index 0000000..c6bd26f --- /dev/null +++ b/lib/berrypod_web/not_found_error.ex @@ -0,0 +1,10 @@ +defmodule BerrypodWeb.NotFoundError do + @moduledoc """ + Raised in LiveViews when a resource can't be found. + + Implements `Plug.Exception` so Phoenix renders the 404 error page + instead of a 500. + """ + + defexception message: "not found", plug_status: 404 +end diff --git a/test/berrypod_web/live/shop/custom_page_test.exs b/test/berrypod_web/live/shop/custom_page_test.exs index 33ed05e..0bc6f2b 100644 --- a/test/berrypod_web/live/shop/custom_page_test.exs +++ b/test/berrypod_web/live/shop/custom_page_test.exs @@ -49,8 +49,10 @@ defmodule BerrypodWeb.Shop.CustomPageTest do :ok end - test "redirects anonymous users to home", %{conn: conn} do - {:error, {:live_redirect, %{to: "/"}}} = live(conn, "/draft-page") + test "returns 404 for anonymous users", %{conn: conn} do + assert_raise BerrypodWeb.NotFoundError, fn -> + live(conn, "/draft-page") + end end test "renders for admin users", %{conn: conn, user: user} do @@ -61,8 +63,10 @@ defmodule BerrypodWeb.Shop.CustomPageTest do end describe "nonexistent page" do - test "redirects to home with flash", %{conn: conn} do - {:error, {:live_redirect, %{to: "/"}}} = live(conn, "/does-not-exist") + test "returns 404", %{conn: conn} do + assert_raise BerrypodWeb.NotFoundError, fn -> + live(conn, "/does-not-exist") + end end end