Skip to content

Commit da10661

Browse files
committed
Merge branch 'issue-1407-show-error-page'
2 parents 7b008d1 + 3736488 commit da10661

12 files changed

+134
-125
lines changed

app/helpers/errors_helper.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module ErrorsHelper
2+
def error_title(code)
3+
case code
4+
when 404 then "ページが見つかりませんでした... 🥺💦"
5+
when 422 then "リクエストが処理できませんでした… 😢"
6+
when 500 then "予期しないエラーが発生しました 😵‍💫"
7+
else "予期せぬエラーが発生しました…😵"
8+
end
9+
end
10+
11+
def error_desc(code)
12+
case code
13+
when 404 then "ページが削除された可能性があります 🤔💭"
14+
when 422 then "入力内容に誤りがあるか、リクエストが正しく送信されなかった可能性があります。"
15+
when 500 then "申し訳ありません。サーバーで問題が発生しています。"
16+
else "改善されない場合は、お手数ですがエラー報告をお願いいたします🙇‍♀️"
17+
end
18+
end
19+
end

app/views/errors/bad_request.html.erb

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1 @@
1-
<% provide(:title, "ページが見つかりませんでした... 🥺💦") %>
2-
<% provide(:desc, "ページが削除された可能性があります 🤔💭") %>
3-
4-
<div id="top" class="title text-center">
5-
<i class="cd cd-logo"></i>
6-
<h1>CoderDojo Japan</h1>
7-
<p>子どものためのプログラミング道場</p>
8-
<br>
9-
</div>
10-
11-
<section class="introduction text-center list">
12-
<h2>
13-
ページが見つかりませんでした... 🥺💦
14-
</h2>
15-
<p>
16-
ページが削除された可能性があります 🤔💭
17-
<br>
18-
</p>
19-
<p>
20-
フィードバックなどあれば [email protected] にメールしていただくか、
21-
<br>
22-
もしくは
23-
<a href="https://github.com/coderdojo-japan/coderdojo.jp/issues/">GitHub Issue</a>
24-
に書いていただけると嬉しいです (>人< )✨
25-
</p>
26-
</section>
27-
28-
<section id="timeline" class="text-center grayscale-bg" style="padding-bottom: 100px;">
29-
<br>
30-
<h3 class="text-center">
31-
<a href="https://twitter.com/search?vertical=default&q=dojocon%20OR%20coderdojo%20OR%20%E3%82%B3%E3%83%BC%E3%83%80%E3%83%BC%E9%81%93%E5%A0%B4%20lang%3Aja" target="_blank" rel="noopener">🐣 最近の情報発信</a>
32-
</h3>
33-
<br>
34-
<div class="twitter-timeline-wrapper">
35-
<a class="twitter-timeline" href="https://twitter.com/CoderDojoJapan?ref_src=twsrc%5Etfw" data-lang="ja" data-chrome="noheader nofooter noborders" data-dnt="true" data-width="500" data-tweet-limit="5">@CoderDojoJapan アカウントのタイムライン</a>
36-
</div>
37-
<script async="" await="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
38-
</section>
39-
1+
<%= render template: "errors/show", locals: { status_code: 400 } %>

app/views/errors/forbidden.html.erb

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1 @@
1-
<% provide(:title, "403 Forbidden エラー 🚧 🏗") %>
2-
<% provide(:desc, "アクセス権限が無いか削除された可能性があります 🤔💭") %>
3-
4-
<div id="top" class="title text-center">
5-
<i class="cd cd-logo"></i>
6-
<h1>CoderDojo Japan</h1>
7-
<p>子どものためのプログラミング道場</p>
8-
<br>
9-
</div>
10-
11-
<section class="introduction text-center list">
12-
<h2>
13-
アクセスできないページのため表示できませんでした 🚧 🏗
14-
</h2>
15-
<p>
16-
ログインし直すことで解決する可能性があります 🤔💭
17-
<br>
18-
</p>
19-
<p>
20-
フィードバックなどあれば [email protected] にメールしていただくか、
21-
<br>
22-
もしくは
23-
<a href="https://github.com/coderdojo-japan/coderdojo.jp/issues/">GitHub Issue</a>
24-
に書いていただけると嬉しいです (>人< )✨
25-
</p>
26-
</section>
27-
28-
<section id="timeline" class="text-center grayscale-bg" style="padding-bottom: 100px;">
29-
<br>
30-
<h3 class="text-center">
31-
<a href="https://twitter.com/search?vertical=default&q=dojocon%20OR%20coderdojo%20OR%20%E3%82%B3%E3%83%BC%E3%83%80%E3%83%BC%E9%81%93%E5%A0%B4%20lang%3Aja" target="_blank" rel="noopener">🐣 最近の情報発信</a>
32-
</h3>
33-
<br>
34-
<div class="twitter-timeline-wrapper">
35-
<a class="twitter-timeline" href="https://twitter.com/CoderDojoJapan?ref_src=twsrc%5Etfw" data-lang="ja" data-chrome="noheader nofooter noborders" data-dnt="true" data-width="500" data-tweet-limit="5">@CoderDojoJapan アカウントのタイムライン</a>
36-
</div>
37-
<script async="" await="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
38-
</section>
1+
<%= render template: "errors/show", locals: { status_code: 403 } %>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= render template: "errors/show", locals: { status_code: 500 } %>

app/views/errors/internal_server_error.json.jbuilder

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/views/errors/not_found.html.erb

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1 @@
1-
<% provide(:title, "ページが見つかりませんでした... 🥺💦") %>
2-
<% provide(:desc, "ページが削除された可能性があります 🤔💭") %>
3-
4-
<div id="top" class="title text-center">
5-
<i class="cd cd-logo"></i>
6-
<h1>CoderDojo Japan</h1>
7-
<p>子どものためのプログラミング道場</p>
8-
<br>
9-
</div>
10-
11-
<section class="introduction text-center list">
12-
<h2>
13-
ページが見つかりませんでした... 🥺💦
14-
</h2>
15-
<p>
16-
ページが削除された可能性があります 🤔💭
17-
<br>
18-
</p>
19-
<p>
20-
フィードバックなどあれば [email protected] にメールしていただくか、
21-
<br>
22-
もしくは
23-
<a href="https://github.com/coderdojo-japan/coderdojo.jp/issues/">GitHub Issue</a>
24-
に書いていただけると嬉しいです (>人< )✨
25-
</p>
26-
</section>
27-
28-
<section id="timeline" class="text-center grayscale-bg" style="padding-bottom: 100px;">
29-
<br>
30-
<h3 class="text-center">
31-
<a href="https://twitter.com/search?vertical=default&q=dojocon%20OR%20coderdojo%20OR%20%E3%82%B3%E3%83%BC%E3%83%80%E3%83%BC%E9%81%93%E5%A0%B4%20lang%3Aja" target="_blank" rel="noopener">🐣 最近の情報発信</a>
32-
</h3>
33-
<br>
34-
<div class="twitter-timeline-wrapper">
35-
<a class="twitter-timeline" href="https://twitter.com/CoderDojoJapan?ref_src=twsrc%5Etfw" data-lang="ja" data-chrome="noheader nofooter noborders" data-dnt="true" data-width="500" data-tweet-limit="5">@CoderDojoJapan アカウントのタイムライン</a>
36-
</div>
37-
<script async="" await="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
38-
</section>
1+
<%= render template: "errors/show", locals: { status_code: 404 } %>

app/views/errors/not_found.json.jbuilder

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/views/errors/show.html.erb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<%# The status_code local variable is passed from the parent template %>
2+
<% title = error_title(status_code) %>
3+
<% desc = error_desc(status_code) %>
4+
5+
<% provide(:title, title) %>
6+
<% provide(:desc, desc) %>
7+
8+
<div id="top" class="title text-center">
9+
<i class="cd cd-logo"></i>
10+
<h1>CoderDojo Japan</h1>
11+
<p>子どものためのプログラミング道場</p>
12+
<br>
13+
</div>
14+
15+
<section class="introduction text-center list">
16+
<h2><%= title %></h2>
17+
<p>
18+
<%= desc %>
19+
<br>
20+
</p>
21+
<p>
22+
フィードバックなどあれば [email protected] にメールしていただくか、
23+
<br>
24+
もしくは
25+
<a href="https://github.com/coderdojo-japan/coderdojo.jp/issues/" target="_blank" rel="noopener">
26+
GitHub Issue
27+
</a>
28+
に書いていただけると嬉しいです (>人< )✨
29+
</p>
30+
</section>
31+
32+
<section id="timeline" class="text-center grayscale-bg" style="padding-bottom: 100px;">
33+
<br>
34+
<h3 class="text-center">
35+
<a href="https://twitter.com/search?vertical=default&q=dojocon%20OR%20coderdojo%20OR%20%E3%82%B3%E3%83%BC%E3%83%80%E3%83%BC%E9%81%93%E5%A0%B4%20lang%3Aja"
36+
target="_blank" rel="noopener">
37+
🐣 最近の情報発信
38+
</a>
39+
</h3>
40+
<br>
41+
<div class="twitter-timeline-wrapper">
42+
<a class="twitter-timeline"
43+
href="https://twitter.com/CoderDojoJapan?ref_src=twsrc%5Etfw"
44+
data-lang="ja"
45+
data-chrome="noheader nofooter noborders"
46+
data-dnt="true"
47+
data-width="500"
48+
data-tweet-limit="5">
49+
@CoderDojoJapan アカウントのタイムライン
50+
</a>
51+
</div>
52+
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
53+
</section>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= render template: "errors/show", locals: { status_code: 422 } %>

app/views/errors/unprocessable_entity.json.jbuilder

Lines changed: 0 additions & 1 deletion
This file was deleted.

config/initializers/rambulance.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@
2929
# If Rambulance receives an exception that is not listed here, it'll render
3030
# the internal server error template and return 500 as http status.
3131
config.rescue_responses = {
32-
ActionController::RoutingError => :not_found,
33-
AbstractController::ActionNotFound => :not_found,
34-
ActionController::BadRequest => :bad_request,
32+
"ActionController::RoutingError" => :not_found,
33+
"AbstractController::ActionNotFound" => :not_found,
34+
"ActionController::BadRequest" => :bad_request,
35+
"ActionController::InvalidAuthenticityToken" => :unprocessable_entity,
3536

3637
# "ActiveRecord::RecordNotUnique" => :unprocessable_entity,
3738
# "CanCan::AccessDenied" => :forbidden,

spec/requests/errors_spec.rb

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,64 @@
11
require 'rails_helper'
22

3-
RSpec.describe "Errors", type: :request do
3+
RSpec.describe 'Errors', type: :request do
44
include Rambulance::TestHelper
5+
include ErrorsHelper
56

6-
describe "Error requests" do
7-
it 'should render a corresponding error page' do
7+
before do
8+
# テスト用のルーティングを直接定義
9+
Rails.application.routes.draw do
10+
get '/trigger_400', to: ->(env) { raise ActionController::BadRequest }
11+
get '/trigger_422', to: ->(env) { raise ActionController::InvalidAuthenticityToken }
12+
get '/trigger_500', to: ->(env) { raise 'This is a test 500 error' }
13+
end
14+
15+
# ビューのレンダリングをスタブして、レイアウト起因のエラーを回避
16+
allow_any_instance_of(Rambulance::ExceptionsApp).to receive(:render).and_wrap_original do |m, *args|
17+
options = args.last.is_a?(Hash) ? args.pop : {}
18+
m.call(*args, **options.merge(layout: false))
19+
end
20+
end
21+
22+
after do
23+
# テスト後に追加したルーティングを元に戻す
24+
Rails.application.reload_routes!
25+
end
26+
27+
describe 'Error requests' do
28+
it 'renders the 404 error page' do
829
with_exceptions_app do
30+
# どのルーティングにもマッチしないパスをリクエスト
931
get '/does_not_exist'
1032
end
33+
expect(response.status).to eq(404)
34+
expect(response.body).to include(error_title(404))
35+
expect(response.body).to include(error_desc(404))
36+
end
1137

12-
assert_equal 404, response.status
38+
it 'renders the 422 error page' do
39+
with_exceptions_app do
40+
get '/trigger_422'
41+
end
42+
expect(response.status).to eq(422)
43+
expect(response.body).to include(error_title(422))
44+
expect(response.body).to include(error_desc(422))
45+
end
46+
47+
it 'renders the 500 error page' do
48+
with_exceptions_app do
49+
get '/trigger_500'
50+
end
51+
expect(response.status).to eq(500)
52+
expect(response.body).to include(error_title(500))
53+
expect(response.body).to include(error_desc(500))
54+
end
55+
it 'renders the 400 error page' do
56+
with_exceptions_app do
57+
get '/trigger_400'
58+
end
59+
expect(response.status).to eq(400)
60+
expect(response.body).to include(error_title(400))
61+
expect(response.body).to include(error_desc(400))
1362
end
1463
end
1564
end

0 commit comments

Comments
 (0)