From 785e403370dfec0bf18b9063de580bbf6765a254 Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 6 Nov 2026 15:42:52 +0100 Subject: [PATCH] 404 and 500 drafts --- assets/css/app.css | 1 + assets/css/pages/_error.css | 60 +++++++++++++++ lib/chesstrainer_web/components/layouts.ex | 7 +- .../components/layouts/root.html.heex | 75 +------------------ .../components/layouts/theme_script.html.heex | 73 ++++++++++++++++++ .../controllers/error_html.ex | 49 +++++++++--- .../controllers/error_html/404.html.heex | 40 ++++++++++ .../controllers/error_html/500.html.heex | 41 ++++++++++ .../controllers/error_json.ex | 8 ++ .../controllers/page_controller.ex | 10 +++ .../controllers/page_html/not_found.html.heex | 7 ++ lib/chesstrainer_web/router.ex | 8 ++ .../controllers/error_html_test.exs | 38 ++++++++-- .../controllers/page_controller_test.exs | 13 ++++ 14 files changed, 335 insertions(+), 95 deletions(-) create mode 100644 assets/css/pages/_error.css create mode 100644 lib/chesstrainer_web/components/layouts/theme_script.html.heex create mode 100644 lib/chesstrainer_web/controllers/error_html/404.html.heex create mode 100644 lib/chesstrainer_web/controllers/error_html/500.html.heex create mode 100644 lib/chesstrainer_web/controllers/page_html/not_found.html.heex diff --git a/assets/css/app.css b/assets/css/app.css index 38997b9..9abf639 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -23,6 +23,7 @@ @import "./components/_board.css"; @import "./components/_promotion.css"; @import "./pages/_legal.css"; +@import "./pages/_error.css"; @source "../css"; @source "../js"; diff --git a/assets/css/pages/_error.css b/assets/css/pages/_error.css new file mode 100644 index 0000000..1a9a84c --- /dev/null +++ b/assets/css/pages/_error.css @@ -0,0 +1,60 @@ +.error-page { + width: min(760px, calc(100% - 48px)); + margin: 0 auto; + padding: 100px 0 130px; + text-align: center; +} + +.error-code { + display: inline-flex; + align-items: center; + justify-content: center; + min-width: 68px; + padding: 7px 16px; + border: 1px solid var(--color-hairline); + border-radius: 999px; + color: var(--color-coral-text); + font-size: 12px; + font-weight: 700; + letter-spacing: .16em; +} + +.error-page h1 { + max-width: 640px; + margin: 28px auto 0; + font-size: clamp(40px, 5.5vw, 60px); + line-height: 1.02; +} + +.error-page p { + max-width: 48ch; + margin: 18px auto 0; + color: var(--color-muted); + font-size: 16px; + line-height: 1.7; +} + +.error-page .error-hint { + margin-top: 10px; + font-size: 13px; +} + +.error-actions { + display: flex; + flex-wrap: wrap; + justify-content: center; + align-items: center; + gap: 12px; + margin-top: 34px; +} + +@media (max-width: 650px) { + .error-page { + width: calc(100% - 36px); + padding: 64px 0 90px; + } + + .error-page h1 { + font-size: clamp(34px, 10vw, 44px); + } +} diff --git a/lib/chesstrainer_web/components/layouts.ex b/lib/chesstrainer_web/components/layouts.ex index 1866a6b..c17cbec 100644 --- a/lib/chesstrainer_web/components/layouts.ex +++ b/lib/chesstrainer_web/components/layouts.ex @@ -239,9 +239,10 @@ defmodule ChesstrainerWeb.Layouts do @doc """ Provides the system / light / dark colour-mode switch. - The click handling and `aria-checked` syncing live in the script in - `root.html.heex`, which runs before paint and also on pages without LiveView - (the landing page uses the marketing bundle). + The click handling and `aria-checked` syncing live in `theme_script.html.heex`, + which runs before paint and also on pages without LiveView (the landing page + uses the marketing bundle). Error pages include it explicitly because they do + not render through the root layout. """ def colour_mode_toggle(assigns) do ~H""" diff --git a/lib/chesstrainer_web/components/layouts/root.html.heex b/lib/chesstrainer_web/components/layouts/root.html.heex index 1224b41..680bd10 100644 --- a/lib/chesstrainer_web/components/layouts/root.html.heex +++ b/lib/chesstrainer_web/components/layouts/root.html.heex @@ -3,6 +3,7 @@ + <.live_title @@ -63,79 +64,7 @@ src={assigns[:js_entry] || ~p"/assets/js/app.js"} > - + diff --git a/lib/chesstrainer_web/components/layouts/theme_script.html.heex b/lib/chesstrainer_web/components/layouts/theme_script.html.heex new file mode 100644 index 0000000..bccd1aa --- /dev/null +++ b/lib/chesstrainer_web/components/layouts/theme_script.html.heex @@ -0,0 +1,73 @@ + diff --git a/lib/chesstrainer_web/controllers/error_html.ex b/lib/chesstrainer_web/controllers/error_html.ex index 5d10b04..055f452 100644 --- a/lib/chesstrainer_web/controllers/error_html.ex +++ b/lib/chesstrainer_web/controllers/error_html.ex @@ -3,21 +3,48 @@ defmodule ChesstrainerWeb.ErrorHTML do This module is invoked by your endpoint in case of errors on HTML requests. See config/config.exs. + + Error templates are rendered outside the root layout, so `404.html.heex` and + `500.html.heex` each render a complete document. They share the body markup + through `error_page/1`, which the catch-all 404 route also uses. """ use ChesstrainerWeb, :html - # If you want to customize your error pages, - # uncomment the embed_templates/1 call below - # and add pages to the error directory: - # - # * lib/chesstrainer_web/controllers/error_html/404.html.heex - # * lib/chesstrainer_web/controllers/error_html/500.html.heex - # - # embed_templates "error_html/*" + embed_templates "error_html/*" - # The default is to render a plain text page based on - # the template name. For example, "404.html" becomes - # "Not Found". + @doc """ + The body shared by the standalone error pages and the catch-all 404 route. + + Rendered inside the root layout by `PageController.not_found/2`, and directly + by the `ErrorHTML` templates, which bypass layouts. + """ + attr :status, :integer, required: true + attr :title, :string, required: true + attr :message, :string, required: true + attr :hint, :string, default: nil + attr :puzzles, :boolean, default: true + + def error_page(assigns) do + ~H""" +
+ {@status} +

{@title}

+

{@message}

+

{@hint}

+ +
+ <.pill_button variant={:primary} href={~p"/"}> + Back to home <.icon_arrow /> + + <.pill_button :if={@puzzles} variant={:light} href={~p"/endgames"}> + Browse puzzles + +
+
+ """ + end + + # Statuses without a template keep the previous plain-text behaviour. def render(template, _assigns) do Phoenix.Controller.status_message_from_template(template) end diff --git a/lib/chesstrainer_web/controllers/error_html/404.html.heex b/lib/chesstrainer_web/controllers/error_html/404.html.heex new file mode 100644 index 0000000..d337bea --- /dev/null +++ b/lib/chesstrainer_web/controllers/error_html/404.html.heex @@ -0,0 +1,40 @@ + + + + + + + {Branding.title("Page not found")} + + + + + + + + + + + + <.error_page + status={404} + title="This page isn't on the board" + message="The page you were looking for moved, or never existed. Check the address, or head back to the trainer." + /> + + + + diff --git a/lib/chesstrainer_web/controllers/error_html/500.html.heex b/lib/chesstrainer_web/controllers/error_html/500.html.heex new file mode 100644 index 0000000..8a15e9b --- /dev/null +++ b/lib/chesstrainer_web/controllers/error_html/500.html.heex @@ -0,0 +1,41 @@ + + + + + + + {Branding.title("Something went wrong")} + + + + + + + + + + + + <.error_page + status={500} + title="Something went wrong" + message="An unexpected error stopped this page from loading. It's on us, not you — please try again in a moment." + hint={"If it keeps happening, email #{Branding.support_email()}."} + /> + + + + diff --git a/lib/chesstrainer_web/controllers/error_json.ex b/lib/chesstrainer_web/controllers/error_json.ex index 6fa16de..c75c50e 100644 --- a/lib/chesstrainer_web/controllers/error_json.ex +++ b/lib/chesstrainer_web/controllers/error_json.ex @@ -12,6 +12,14 @@ defmodule ChesstrainerWeb.ErrorJSON do # %{errors: %{detail: "Internal Server Error"}} # end + def render("404.json", _assigns) do + %{errors: %{detail: "Not Found"}} + end + + def render("500.json", _assigns) do + %{errors: %{detail: "Internal Server Error"}} + end + # By default, Phoenix returns the status message from # the template name. For example, "404.json" becomes # "Not Found". diff --git a/lib/chesstrainer_web/controllers/page_controller.ex b/lib/chesstrainer_web/controllers/page_controller.ex index 708b77b..c3b22a8 100644 --- a/lib/chesstrainer_web/controllers/page_controller.ex +++ b/lib/chesstrainer_web/controllers/page_controller.ex @@ -49,4 +49,14 @@ defmodule ChesstrainerWeb.PageController do |> assign(:canonical_url, current_url(conn)) |> render(:terms_conditions) end + + def not_found(conn, _params) do + conn + |> put_status(:not_found) + |> assign(:js_entry, ~p"/assets/js/marketing.js") + |> assign(:page_title, Branding.title("Page not found")) + |> assign(:title_suffix, "") + |> assign(:noindex, true) + |> render(:not_found) + end end diff --git a/lib/chesstrainer_web/controllers/page_html/not_found.html.heex b/lib/chesstrainer_web/controllers/page_html/not_found.html.heex new file mode 100644 index 0000000..558d065 --- /dev/null +++ b/lib/chesstrainer_web/controllers/page_html/not_found.html.heex @@ -0,0 +1,7 @@ + + + diff --git a/lib/chesstrainer_web/router.ex b/lib/chesstrainer_web/router.ex index 423ccc3..5f32d02 100644 --- a/lib/chesstrainer_web/router.ex +++ b/lib/chesstrainer_web/router.ex @@ -93,4 +93,12 @@ defmodule ChesstrainerWeb.Router do post "/users/log-in", UserSessionController, :create delete "/users/log-out", UserSessionController, :delete end + + # Keep this last: routes match in definition order. It gives unmatched paths a + # branded 404 page with the site chrome instead of Phoenix's bare status page. + scope "/", ChesstrainerWeb do + pipe_through :browser + + match :*, "/*path", PageController, :not_found + end end diff --git a/test/chesstrainer_web/controllers/error_html_test.exs b/test/chesstrainer_web/controllers/error_html_test.exs index 594ac85..60f20e8 100644 --- a/test/chesstrainer_web/controllers/error_html_test.exs +++ b/test/chesstrainer_web/controllers/error_html_test.exs @@ -1,15 +1,37 @@ defmodule ChesstrainerWeb.ErrorHTMLTest do - use ChesstrainerWeb.ConnCase, async: true + use ExUnit.Case, async: true - # Bring render_to_string/4 for testing custom views - import Phoenix.Template, only: [render_to_string: 4] + alias Chesstrainer.Branding + alias ChesstrainerWeb.ErrorHTML - test "renders 404.html" do - assert render_to_string(ChesstrainerWeb.ErrorHTML, "404", "html", []) == "Not Found" + defp render(template) do + Phoenix.Template.render_to_string(ErrorHTML, template, "html", %{current_scope: nil}) end - test "renders 500.html" do - assert render_to_string(ChesstrainerWeb.ErrorHTML, "500", "html", []) == - "Internal Server Error" + test "404 renders a complete document with the site chrome" do + html = render("404") + + assert html =~ "" + assert html =~ "#{Branding.title("Page not found")}" + assert html =~ ~s(class="error-code">404) + assert html =~ "The page you were looking for moved" + assert html =~ "Browse puzzles" + assert html =~ ~s(class="site-footer") + assert html =~ "/assets/js/marketing.js" + refute html =~ ~s(name="csrf-token") + end + + test "500 renders a complete document and the support address" do + html = render("500") + + assert html =~ "#{Branding.title("Something went wrong")}" + assert html =~ ~s(class="error-code">500) + assert html =~ "An unexpected error stopped this page" + assert html =~ Branding.support_email() + assert html =~ ~s(class="site-footer") + end + + test "unknown statuses fall back to the plain status message" do + assert ErrorHTML.render("503.html", %{}) == "Service Unavailable" end end diff --git a/test/chesstrainer_web/controllers/page_controller_test.exs b/test/chesstrainer_web/controllers/page_controller_test.exs index f299198..a859886 100644 --- a/test/chesstrainer_web/controllers/page_controller_test.exs +++ b/test/chesstrainer_web/controllers/page_controller_test.exs @@ -263,4 +263,17 @@ defmodule ChesstrainerWeb.PageControllerTest do "/endgames" ] end + + test "unknown routes render the branded 404 page", %{conn: conn} do + html = conn |> get("/this-page-does-not-exist") |> html_response(404) + document = LazyHTML.from_document(html) + + assert LazyHTML.text(LazyHTML.query(document, "title")) == Branding.title("Page not found") + assert LazyHTML.text(LazyHTML.query(document, ".error-code")) == "404" + assert html =~ "The page you were looking for moved" + assert html =~ "Browse puzzles" + assert html =~ ~s(class="site-footer") + assert html =~ "/assets/js/marketing.js" + assert html =~ ~s(name="robots") + end end