From f5286d0927ecdf76333fda0d7439573821e64455 Mon Sep 17 00:00:00 2001 From: Brian Manifold Date: Fri, 22 Mar 2024 11:26:00 -0400 Subject: [PATCH] feat(portal): Add styled errors pages (404, 422, 500) (#4231) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #2136 ## Screenshots for `404` and `500` Screenshot 2024-03-20 at 1 16 46 PM Screenshot 2024-03-20 at 1 16 30 PM --- .../lib/web/controllers/error_controller.ex | 13 ++++++ .../web/lib/web/controllers/error_html.ex | 2 +- .../web/controllers/error_html/404.html.heex | 42 ++++++++++++++++++ .../web/controllers/error_html/422.html.heex | 44 +++++++++++++++++++ .../web/controllers/error_html/500.html.heex | 44 +++++++++++++++++++ elixir/apps/web/lib/web/router.ex | 6 +++ .../web/test/web/acceptance/auth_test.exs | 2 +- .../test/web/controllers/error_html_test.exs | 30 ++++++++++--- 8 files changed, 175 insertions(+), 8 deletions(-) create mode 100644 elixir/apps/web/lib/web/controllers/error_controller.ex create mode 100644 elixir/apps/web/lib/web/controllers/error_html/404.html.heex create mode 100644 elixir/apps/web/lib/web/controllers/error_html/422.html.heex create mode 100644 elixir/apps/web/lib/web/controllers/error_html/500.html.heex diff --git a/elixir/apps/web/lib/web/controllers/error_controller.ex b/elixir/apps/web/lib/web/controllers/error_controller.ex new file mode 100644 index 000000000..5fe119d1d --- /dev/null +++ b/elixir/apps/web/lib/web/controllers/error_controller.ex @@ -0,0 +1,13 @@ +defmodule Web.ErrorController do + use Web, :controller + + def show(_conn, params) do + case params["code"] do + "404" -> raise Web.LiveErrors.NotFoundError + "422" -> raise Web.LiveErrors.InvalidRequestError + "500" -> raise "internal server error" + end + + raise "unknown error" + end +end diff --git a/elixir/apps/web/lib/web/controllers/error_html.ex b/elixir/apps/web/lib/web/controllers/error_html.ex index 4edc23f26..9db65969d 100644 --- a/elixir/apps/web/lib/web/controllers/error_html.ex +++ b/elixir/apps/web/lib/web/controllers/error_html.ex @@ -8,7 +8,7 @@ defmodule Web.ErrorHTML do # * lib/web_web/controllers/error_html/404.html.heex # * lib/web_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 diff --git a/elixir/apps/web/lib/web/controllers/error_html/404.html.heex b/elixir/apps/web/lib/web/controllers/error_html/404.html.heex new file mode 100644 index 000000000..546b6465d --- /dev/null +++ b/elixir/apps/web/lib/web/controllers/error_html/404.html.heex @@ -0,0 +1,42 @@ + + + + + + + + + + + + + 404 Not Found + + + + +
+
+
+ Firezone Logo +

+ Error404 +

+

Sorry, we couldn't find this page.

+
+
+
+ + diff --git a/elixir/apps/web/lib/web/controllers/error_html/422.html.heex b/elixir/apps/web/lib/web/controllers/error_html/422.html.heex new file mode 100644 index 000000000..86ca63921 --- /dev/null +++ b/elixir/apps/web/lib/web/controllers/error_html/422.html.heex @@ -0,0 +1,44 @@ + + + + + + + + + + + + + 422 Error + + + + +
+
+
+ Firezone Logo +

+ Error422 +

+

+ Something went wrong. We've already been notified and will get it fixed as soon as possible. +

+
+
+
+ + diff --git a/elixir/apps/web/lib/web/controllers/error_html/500.html.heex b/elixir/apps/web/lib/web/controllers/error_html/500.html.heex new file mode 100644 index 000000000..861223dfc --- /dev/null +++ b/elixir/apps/web/lib/web/controllers/error_html/500.html.heex @@ -0,0 +1,44 @@ + + + + + + + + + + + + + 500 Error + + + + +
+
+
+ Firezone Logo +

+ Error500 +

+

+ Something went wrong. We've already been notified and will get it fixed as soon as possible. +

+
+
+
+ + diff --git a/elixir/apps/web/lib/web/router.ex b/elixir/apps/web/lib/web/router.ex index a245e3763..a56150b13 100644 --- a/elixir/apps/web/lib/web/router.ex +++ b/elixir/apps/web/lib/web/router.ex @@ -56,6 +56,12 @@ defmodule Web.Router do pipe_through [:public] forward "/mailbox", Plug.Swoosh.MailboxPreview end + + scope "/error", Web do + pipe_through [:public] + + get "/:code", ErrorController, :show + end end scope "/sign_up", Web do diff --git a/elixir/apps/web/test/web/acceptance/auth_test.exs b/elixir/apps/web/test/web/acceptance/auth_test.exs index 38abb9f77..ccf796f2b 100644 --- a/elixir/apps/web/test/web/acceptance/auth_test.exs +++ b/elixir/apps/web/test/web/acceptance/auth_test.exs @@ -118,7 +118,7 @@ defmodule Web.Acceptance.AuthTest do |> Auth.authenticate(identity) |> visit(~p"/#{account}/actors") - assert text(session) == "Not Found" + assert text(session) =~ "Sorry, we couldn't find this page" assert_path(session, ~p"/#{account}/actors") end diff --git a/elixir/apps/web/test/web/controllers/error_html_test.exs b/elixir/apps/web/test/web/controllers/error_html_test.exs index 7dceb5966..2c7895d57 100644 --- a/elixir/apps/web/test/web/controllers/error_html_test.exs +++ b/elixir/apps/web/test/web/controllers/error_html_test.exs @@ -1,14 +1,32 @@ defmodule Web.ErrorHTMLTest do use Web.ConnCase, async: true - # Bring render_to_string/4 for testing custom views - import Phoenix.Template + test "renders 404.html", %{conn: conn} do + {_code, _headers, body} = + assert_error_sent 404, fn -> + get(conn, ~p"/error/404") + end - test "renders 404.html" do - assert render_to_string(Web.ErrorHTML, "404", "html", []) == "Not Found" + assert body =~ "Sorry, we couldn't find this page" end - test "renders 500.html" do - assert render_to_string(Web.ErrorHTML, "500", "html", []) == "Internal Server Error" + test "renders 422.html", %{conn: conn} do + {_code, _headers, body} = + assert_error_sent 422, fn -> + get(conn, ~p"/error/422") + end + + assert body =~ "Something went wrong" + assert body =~ "We've already been notified and will get it fixed as soon as possible" + end + + test "renders 500.html", %{conn: conn} do + {_code, _headers, body} = + assert_error_sent 500, fn -> + get(conn, ~p"/error/500") + end + + assert body =~ "Something went wrong" + assert body =~ "We've already been notified and will get it fixed as soon as possible" end end