show proper 404 page for missing custom pages instead of redirecting
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
ad2f2517e5
commit
f479be5728
@ -15,23 +15,13 @@ defmodule BerrypodWeb.Shop.CustomPage do
|
|||||||
cond do
|
cond do
|
||||||
is_nil(page) ->
|
is_nil(page) ->
|
||||||
record_broken_url("/#{slug}")
|
record_broken_url("/#{slug}")
|
||||||
|
raise BerrypodWeb.NotFoundError
|
||||||
{:noreply,
|
|
||||||
socket
|
|
||||||
|> put_flash(:error, "Page not found")
|
|
||||||
|> push_navigate(to: ~p"/")}
|
|
||||||
|
|
||||||
page.type != "custom" ->
|
page.type != "custom" ->
|
||||||
{:noreply,
|
raise BerrypodWeb.NotFoundError
|
||||||
socket
|
|
||||||
|> put_flash(:error, "Page not found")
|
|
||||||
|> push_navigate(to: ~p"/")}
|
|
||||||
|
|
||||||
page.published != true and not socket.assigns.is_admin ->
|
page.published != true and not socket.assigns.is_admin ->
|
||||||
{:noreply,
|
raise BerrypodWeb.NotFoundError
|
||||||
socket
|
|
||||||
|> put_flash(:error, "Page not found")
|
|
||||||
|> push_navigate(to: ~p"/")}
|
|
||||||
|
|
||||||
true ->
|
true ->
|
||||||
extra = Pages.load_block_data(page.blocks, socket.assigns)
|
extra = Pages.load_block_data(page.blocks, socket.assigns)
|
||||||
|
|||||||
10
lib/berrypod_web/not_found_error.ex
Normal file
10
lib/berrypod_web/not_found_error.ex
Normal file
@ -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
|
||||||
@ -49,8 +49,10 @@ defmodule BerrypodWeb.Shop.CustomPageTest do
|
|||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
|
||||||
test "redirects anonymous users to home", %{conn: conn} do
|
test "returns 404 for anonymous users", %{conn: conn} do
|
||||||
{:error, {:live_redirect, %{to: "/"}}} = live(conn, "/draft-page")
|
assert_raise BerrypodWeb.NotFoundError, fn ->
|
||||||
|
live(conn, "/draft-page")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "renders for admin users", %{conn: conn, user: user} do
|
test "renders for admin users", %{conn: conn, user: user} do
|
||||||
@ -61,8 +63,10 @@ defmodule BerrypodWeb.Shop.CustomPageTest do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "nonexistent page" do
|
describe "nonexistent page" do
|
||||||
test "redirects to home with flash", %{conn: conn} do
|
test "returns 404", %{conn: conn} do
|
||||||
{:error, {:live_redirect, %{to: "/"}}} = live(conn, "/does-not-exist")
|
assert_raise BerrypodWeb.NotFoundError, fn ->
|
||||||
|
live(conn, "/does-not-exist")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user