Skip to content

Commit 3484e0c

Browse files
committed
Add Custom Error Pages
1 parent 8a9094a commit 3484e0c

6 files changed

Lines changed: 50 additions & 10 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
defmodule DevRoundWeb.ErrorComponents do
2+
use DevRoundWeb, :html
3+
4+
attr :code, :integer, required: true
5+
attr :title, :string, required: true
6+
slot :inner_block
7+
8+
def error_page(assigns) do
9+
~H"""
10+
<!DOCTYPE html>
11+
<html lang="en" data-theme="dark">
12+
<head>
13+
<meta charset="utf-8" />
14+
<meta name="viewport" content="width=device-width, initial-scale=1" />
15+
<meta name="csrf-token" content={get_csrf_token()} />
16+
<title>{@code} {@title}</title>
17+
<link rel="stylesheet" href={~p"/assets/css/app.css"} />
18+
</head>
19+
<body>
20+
<div class="card card-xl bg-black w-128 shadow-xl shadow-black mx-auto my-24">
21+
<div class="card-body items-center text-center">
22+
<h1 class="card-title">
23+
<span class="text-primary">{@code}</span> {@title}
24+
</h1>
25+
{render_slot(@inner_block)}
26+
</div>
27+
<figure>
28+
<img
29+
src={~p"/images/error.png"}
30+
alt="A rendering of an Apple MacBook which displays a pixelated broken heart which melts onto its keyboard."
31+
/>
32+
</figure>
33+
</div>
34+
</body>
35+
</html>
36+
"""
37+
end
38+
end

‎lib/dev_round_web/controllers/error_html.ex‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@ defmodule DevRoundWeb.ErrorHTML do
66
"""
77
use DevRoundWeb, :html
88

9-
# If you want to customize your error pages,
10-
# uncomment the embed_templates/1 call below
11-
# and add pages to the error directory:
12-
#
13-
# * lib/dev_round_web/controllers/error_html/404.html.heex
14-
# * lib/dev_round_web/controllers/error_html/500.html.heex
15-
#
16-
# embed_templates "error_html/*"
9+
embed_templates "error_html/*"
1710

1811
# The default is to render a plain text page based on
1912
# the template name. For example, "404.html" becomes
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<DevRoundWeb.ErrorComponents.error_page code={404} title="DevRound Not Found">
2+
<p class="text-base-content/60">We are terribly sorry.</p>
3+
<div class="card-actions mt-4">
4+
<.button href={~p"/"} variant="primary">Return Home</.button>
5+
</div>
6+
</DevRoundWeb.ErrorComponents.error_page>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<DevRoundWeb.ErrorComponents.error_page code={500} title="Internal DevRound Error">
2+
<p class="text-base-content/60">We apologize the inconvenience.</p>
3+
</DevRoundWeb.ErrorComponents.error_page>

‎priv/static/images/error.png‎

1.17 MB
Loading

‎test/dev_round_web/controllers/error_html_test.exs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ defmodule DevRoundWeb.ErrorHTMLTest do
55
import Phoenix.Template
66

77
test "renders 404.html" do
8-
assert render_to_string(DevRoundWeb.ErrorHTML, "404", "html", []) == "Not Found"
8+
assert render_to_string(DevRoundWeb.ErrorHTML, "404", "html", []) =~ "DevRound Not Found"
99
end
1010

1111
test "renders 500.html" do
12-
assert render_to_string(DevRoundWeb.ErrorHTML, "500", "html", []) == "Internal Server Error"
12+
assert render_to_string(DevRoundWeb.ErrorHTML, "500", "html", []) =~ "Internal DevRound Error"
1313
end
1414
end

0 commit comments

Comments
 (0)