-
-
Notifications
You must be signed in to change notification settings - Fork 108
Rambulanceを使って404/500/422ページを整備 #1702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a39bdf6
a4ce6e1
548d72a
7c19b40
a456487
44d493b
677d005
62ad7c3
4b52ff7
7e4737c
9dac37e
c0d871a
092c3dd
8e7f468
0103961
21dbe7b
ee6d1de
4e8612b
2222744
22c107b
285999d
5d202b7
d064907
5a6566d
ca186cb
50bd22e
c50c127
4457879
f86b750
29c6cac
48b231e
8cd758d
52bc61d
619801e
4df3a8a
cc5dad9
561e698
37ed1c4
9b25662
ac24ce1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module ErrorsHelper | ||
def error_title(code) | ||
case code | ||
when 404 then "ページが見つかりませんでした... 🥺💦" | ||
when 422 then "リクエストが処理できませんでした… 😢" | ||
when 500 then "予期しないエラーが発生しました 😵💫" | ||
else "予期せぬエラーが発生しました…😵" | ||
end | ||
end | ||
|
||
def error_desc(code) | ||
case code | ||
when 404 then "ページが削除された可能性があります 🤔💭" | ||
when 422 then "入力内容に誤りがあるか、リクエストが正しく送信されなかった可能性があります。" | ||
when 500 then "申し訳ありません。サーバーで問題が発生しています。" | ||
else "改善されない場合は、お手数ですがエラー報告をお願いいたします🙇♀️" | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1 @@ | ||
<% provide(:title, "ページが見つかりませんでした... 🥺💦") %> | ||
<% provide(:desc, "ページが削除された可能性があります 🤔💭") %> | ||
|
||
<div id="top" class="title text-center"> | ||
<i class="cd cd-logo"></i> | ||
<h1>CoderDojo Japan</h1> | ||
<p>子どものためのプログラミング道場</p> | ||
<br> | ||
</div> | ||
|
||
<section class="introduction text-center list"> | ||
<h2> | ||
ページが見つかりませんでした... 🥺💦 | ||
</h2> | ||
<p> | ||
ページが削除された可能性があります 🤔💭 | ||
<br> | ||
</p> | ||
<p> | ||
フィードバックなどあれば [email protected] にメールしていただくか、 | ||
<br> | ||
もしくは | ||
<a href="https://github.com/coderdojo-japan/coderdojo.jp/issues/">GitHub Issue</a> | ||
に書いていただけると嬉しいです (>人< )✨ | ||
</p> | ||
</section> | ||
|
||
<section id="timeline" class="text-center grayscale-bg" style="padding-bottom: 100px;"> | ||
<br> | ||
<h3 class="text-center"> | ||
<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> | ||
</h3> | ||
<br> | ||
<div class="twitter-timeline-wrapper"> | ||
<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> | ||
</div> | ||
<script async="" await="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> | ||
</section> | ||
|
||
<%= render template: "errors/show", locals: { status_code: 400 } %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1 @@ | ||
<% provide(:title, "403 Forbidden エラー 🚧 🏗") %> | ||
<% provide(:desc, "アクセス権限が無いか削除された可能性があります 🤔💭") %> | ||
|
||
<div id="top" class="title text-center"> | ||
<i class="cd cd-logo"></i> | ||
<h1>CoderDojo Japan</h1> | ||
<p>子どものためのプログラミング道場</p> | ||
<br> | ||
</div> | ||
|
||
<section class="introduction text-center list"> | ||
<h2> | ||
アクセスできないページのため表示できませんでした 🚧 🏗 | ||
</h2> | ||
<p> | ||
ログインし直すことで解決する可能性があります 🤔💭 | ||
<br> | ||
</p> | ||
<p> | ||
フィードバックなどあれば [email protected] にメールしていただくか、 | ||
<br> | ||
もしくは | ||
<a href="https://github.com/coderdojo-japan/coderdojo.jp/issues/">GitHub Issue</a> | ||
に書いていただけると嬉しいです (>人< )✨ | ||
</p> | ||
</section> | ||
|
||
<section id="timeline" class="text-center grayscale-bg" style="padding-bottom: 100px;"> | ||
<br> | ||
<h3 class="text-center"> | ||
<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> | ||
</h3> | ||
<br> | ||
<div class="twitter-timeline-wrapper"> | ||
<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> | ||
</div> | ||
<script async="" await="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> | ||
</section> | ||
<%= render template: "errors/show", locals: { status_code: 403 } %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render template: "errors/show", locals: { status_code: 500 } %> |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1 @@ | ||
<% provide(:title, "ページが見つかりませんでした... 🥺💦") %> | ||
<% provide(:desc, "ページが削除された可能性があります 🤔💭") %> | ||
|
||
<div id="top" class="title text-center"> | ||
<i class="cd cd-logo"></i> | ||
<h1>CoderDojo Japan</h1> | ||
<p>子どものためのプログラミング道場</p> | ||
<br> | ||
</div> | ||
|
||
<section class="introduction text-center list"> | ||
<h2> | ||
ページが見つかりませんでした... 🥺💦 | ||
</h2> | ||
<p> | ||
ページが削除された可能性があります 🤔💭 | ||
<br> | ||
</p> | ||
<p> | ||
フィードバックなどあれば [email protected] にメールしていただくか、 | ||
<br> | ||
もしくは | ||
<a href="https://github.com/coderdojo-japan/coderdojo.jp/issues/">GitHub Issue</a> | ||
に書いていただけると嬉しいです (>人< )✨ | ||
</p> | ||
</section> | ||
|
||
<section id="timeline" class="text-center grayscale-bg" style="padding-bottom: 100px;"> | ||
<br> | ||
<h3 class="text-center"> | ||
<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> | ||
</h3> | ||
<br> | ||
<div class="twitter-timeline-wrapper"> | ||
<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> | ||
</div> | ||
<script async="" await="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> | ||
</section> | ||
<%= render template: "errors/show", locals: { status_code: 404 } %> |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<%# The status_code local variable is passed from the parent template %> | ||
<% title = error_title(status_code) %> | ||
<% desc = error_desc(status_code) %> | ||
|
||
<% provide(:title, title) %> | ||
<% provide(:desc, desc) %> | ||
|
||
<div id="top" class="title text-center"> | ||
<i class="cd cd-logo"></i> | ||
<h1>CoderDojo Japan</h1> | ||
<p>子どものためのプログラミング道場</p> | ||
<br> | ||
</div> | ||
|
||
<section class="introduction text-center list"> | ||
<h2><%= title %></h2> | ||
<p> | ||
<%= desc %> | ||
<br> | ||
</p> | ||
<p> | ||
フィードバックなどあれば [email protected] にメールしていただくか、 | ||
<br> | ||
もしくは | ||
<a href="https://github.com/coderdojo-japan/coderdojo.jp/issues/" target="_blank" rel="noopener"> | ||
GitHub Issue | ||
</a> | ||
に書いていただけると嬉しいです (>人< )✨ | ||
</p> | ||
</section> | ||
|
||
<section id="timeline" class="text-center grayscale-bg" style="padding-bottom: 100px;"> | ||
<br> | ||
<h3 class="text-center"> | ||
<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> | ||
</h3> | ||
<br> | ||
<div class="twitter-timeline-wrapper"> | ||
<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> | ||
</div> | ||
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> | ||
</section> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render template: "errors/show", locals: { status_code: 422 } %> |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,9 @@ | |
config.consider_all_requests_local = true | ||
config.cache_store = :null_store | ||
|
||
# テスト環境でも例外を自前ルーティングへ | ||
config.exceptions_app = self.routes | ||
|
||
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rambulance提供のルートを利用する場合は、この定義も不要そうです👍 |
||
# Render exception templates for rescuable exceptions and raise for other exceptions. | ||
config.action_dispatch.show_exceptions = :rescuable | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,11 @@ | |
# For details on the DSL available within this file, | ||
# see http://guides.rubyonrails.org/routing.html | ||
|
||
# Rambulance を開発/テスト環境でのみマウント | ||
if Rails.env.development? || Rails.env.test? | ||
mount Rambulance::Engine => "/" | ||
end | ||
|
||
Comment on lines
+5
to
+9
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rambulance提供のルートを利用する場合は、この定義も不要そうです👍 |
||
root "home#show" | ||
|
||
# Render legal documents by using Keiyaku CSS | ||
|
@@ -105,4 +110,10 @@ | |
|
||
# Check development sent emails | ||
mount LetterOpenerWeb::Engine, at: "/letter_opener" if Rails.env.development? | ||
|
||
# Rambulance がキャッチする /404, /422, /500 | ||
match "/404", to: Rambulance::Engine, via: :all, defaults: { status_code: 404 } | ||
match "/422", to: Rambulance::Engine, via: :all, defaults: { status_code: 422 } | ||
match "/500", to: Rambulance::Engine, via: :all, defaults: { status_code: 500 } | ||
|
||
Comment on lines
+113
to
+118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rambulance提供のルートを利用するので、この定義も不要そうです👍 |
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,64 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Errors", type: :request do | ||
RSpec.describe 'Errors', type: :request do | ||
include Rambulance::TestHelper | ||
include ErrorsHelper | ||
|
||
describe "Error requests" do | ||
it 'should render a corresponding error page' do | ||
before do | ||
# テスト用のルーティングを直接定義 | ||
Rails.application.routes.draw do | ||
get '/trigger_400', to: ->(env) { raise ActionController::BadRequest } | ||
get '/trigger_422', to: ->(env) { raise ActionController::InvalidAuthenticityToken } | ||
get '/trigger_500', to: ->(env) { raise 'This is a test 500 error' } | ||
end | ||
|
||
# ビューのレンダリングをスタブして、レイアウト起因のエラーを回避 | ||
allow_any_instance_of(Rambulance::ExceptionsApp).to receive(:render).and_wrap_original do |m, *args| | ||
options = args.last.is_a?(Hash) ? args.pop : {} | ||
m.call(*args, **options.merge(layout: false)) | ||
end | ||
end | ||
|
||
after do | ||
# テスト後に追加したルーティングを元に戻す | ||
Rails.application.reload_routes! | ||
end | ||
|
||
describe 'Error requests' do | ||
it 'renders the 404 error page' do | ||
with_exceptions_app do | ||
# どのルーティングにもマッチしないパスをリクエスト | ||
get '/does_not_exist' | ||
end | ||
expect(response.status).to eq(404) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 「子どものためのプログラミング道場」など、必ず表示されるテキストが存在するかも確認しておくと、さらに安心できそうです👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. テストを追加しました🙌 |
||
expect(response.body).to include(error_title(404)) | ||
expect(response.body).to include(error_desc(404)) | ||
end | ||
|
||
assert_equal 404, response.status | ||
it 'renders the 422 error page' do | ||
with_exceptions_app do | ||
get '/trigger_422' | ||
end | ||
expect(response.status).to eq(422) | ||
expect(response.body).to include(error_title(422)) | ||
expect(response.body).to include(error_desc(422)) | ||
end | ||
|
||
it 'renders the 500 error page' do | ||
with_exceptions_app do | ||
get '/trigger_500' | ||
end | ||
expect(response.status).to eq(500) | ||
expect(response.body).to include(error_title(500)) | ||
expect(response.body).to include(error_desc(500)) | ||
end | ||
it 'renders the 400 error page' do | ||
with_exceptions_app do | ||
get '/trigger_400' | ||
end | ||
expect(response.status).to eq(400) | ||
expect(response.body).to include(error_title(400)) | ||
expect(response.body).to include(error_desc(400)) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rambulance提供のルートを利用する場合は、この定義も不要そうです👍