Skip to content

Instantly share code, notes, and snippets.

@zecho
Forked from srcrip/error_html.ex
Created February 16, 2024 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zecho/a314a60e77d3cad31eac72205e13e6ee to your computer and use it in GitHub Desktop.
Save zecho/a314a60e77d3cad31eac72205e13e6ee to your computer and use it in GitHub Desktop.
Custom Phoenix error pages
# goes in lib/your_app_web/controllers/error_html.ex
defmodule ExampleWeb.ErrorHTML do
use ExampleWeb, :html
def render("404.html" = template, assigns) do
assigns =
assigns
|> Map.merge(%{__changed__: %{}})
|> assign(:message, Phoenix.Controller.status_message_from_template(template))
~H"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>My Site - <%= @status %> <%= @message %></title>
<link rel="stylesheet" href="/assets/app.css" />
</head>
<body>
<div class="h-screen flex flex-col items-center justify-center mx-auto max-w-xl px-4 py-8 text-center">
<h1 class="text-2xl font-bold tracking-tight text-gray-900 sm:text-4xl">
<%= @status %> <%= @message %>
</h1>
<p class="mt-4 text-gray-500">
We can't find that page.
</p>
<.link
href={~p"/"}
class="mt-6 inline-block rounded bg-indigo-600 px-5 py-3 text-sm font-medium text-white hover:bg-indigo-700"
>
Go Back Home
</.link>
</div>
</body>
</html>
"""
end
def render(template, _assigns) do
Phoenix.Controller.status_message_from_template(template)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment