Skip to content

Commit f30c5a7

Browse files
author
hamdibayhan
committed
KBP-140 #time 2h - Configure custom error pages
1 parent 5abb135 commit f30c5a7

16 files changed

+127
-8
lines changed

.rubocop.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,27 @@ Metrics/LineLength:
1616

1717
Metrics/ClassLength:
1818
Exclude:
19+
- 'spec/support/gem_test_helpers.rb'
1920
- 'lib/cybele/generators/app_generator.rb'
2021

22+
Metrics/ModuleLength:
23+
Exclude:
24+
- 'spec/support/gem_test_helpers.rb'
25+
2126
Style/AccessorMethodName:
2227
Exclude:
2328
- 'lib/cybele/generators/app_generator.rb'
2429

2530
Metrics/MethodLength:
2631
CountComments: false
2732
Max: 15
33+
Exclude:
34+
- 'spec/support/gem_test_helpers.rb'
35+
36+
Metrics/AbcSize:
37+
Exclude:
38+
- 'spec/support/gem_test_helpers.rb'
39+
- 'spec/support/mail_test_helpers.rb'
2840

2941
Metrics/BlockLength:
3042
CountComments: false
@@ -35,6 +47,7 @@ Metrics/BlockLength:
3547
- 'spec/**/*.rb'
3648
- 'lib/cybele/app_builder.rb'
3749
- 'lib/cybele/generators/app_generator.rb'
50+
- 'spec/support/cybele_test_helpers.rb'
3851

3952
Style/FrozenStringLiteralComment:
4053
EnforcedStyle: when_needed

lib/cybele.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
require 'cybele/helpers/mailer'
1616
require 'cybele/helpers/paperclip'
1717
require 'cybele/helpers/devise'
18+
require 'cybele/helpers/error_pages'
1819
require 'cybele/app_builder'

lib/cybele/app_builder.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class AppBuilder < Rails::AppBuilder
1515
include Cybele::Helpers::Mailer
1616
include Cybele::Helpers::Paperclip
1717
include Cybele::Helpers::Devise
18+
include Cybele::Helpers::ErrorPages
1819

1920
def readme
2021
template 'README.md.erb',
@@ -75,7 +76,7 @@ def setup_gitignore_files
7576
end
7677

7778
def setup_gitignore_folders
78-
%w(
79+
%w[
7980
app/assets/images
8081
db/migrate
8182
spec/support
@@ -84,7 +85,7 @@ def setup_gitignore_folders
8485
spec/views
8586
spec/controllers
8687
spec/helpers
87-
).each do |dir|
88+
].each do |dir|
8889
empty_directory_with_keep_file dir
8990
end
9091
end

lib/cybele/generators/app_generator.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ def gitignore_files_and_folders
213213
build :setup_gitignore_folders
214214
end
215215

216+
def configure_error_pages
217+
say 'Setup custom exception pages and 404 page'
218+
build :configure_error_pages
219+
end
220+
216221
def goodbye
217222
say 'Congratulations! That\'s all...', :green
218223
end

lib/cybele/helpers/error_pages.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module Cybele
4+
module Helpers
5+
module ErrorPages
6+
def configure_error_pages
7+
inject_into_file 'app/controllers/application_controller.rb',
8+
template_content('error_pages/error_control.erb'),
9+
before: 'self.responder'
10+
11+
inject_into_file 'app/controllers/application_controller.rb',
12+
template_content('error_pages/error_method.erb'),
13+
after: 'protect_from_forgery with: :exception'
14+
15+
inject_into_file 'config/routes.rb',
16+
template_content('error_pages/error_route.erb'),
17+
before: /^end/
18+
19+
create_error_pages_files
20+
end
21+
22+
private
23+
24+
def create_error_pages_files
25+
# Server Error
26+
template 'error_pages/internal_server_error.html.haml',
27+
'app/views/errors/internal_server_error.html.haml',
28+
force: true
29+
30+
# Not Found Error
31+
template 'error_pages/not_found.html.haml',
32+
'app/views/errors/not_found.html.haml',
33+
force: true
34+
end
35+
end
36+
end
37+
end

spec/features/new_default_project_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
expect(gemfile_file).to match(/^gem 'devise-async'/)
3232

3333
sidekiq_file = content('config/sidekiq.yml')
34-
expect(sidekiq_file).to match(/^:concurrency: 25/)
34+
expect(sidekiq_file).to match('[high_priority, 2]')
3535

3636
sidekiq_schedule_file = content('config/sidekiq_schedule.yml')
3737
expect(sidekiq_schedule_file).to match(/-> Daily at midnight/)
@@ -207,4 +207,12 @@
207207
it 'uses devise' do
208208
devise_test_helper
209209
end
210+
211+
it 'uses error_pages' do
212+
error_pages_helper
213+
end
214+
215+
it 'uses gitignore' do
216+
gitignore_helper
217+
end
210218
end

spec/features/new_not_default_project_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,12 @@
187187
it 'uses devise' do
188188
devise_test_helper
189189
end
190+
191+
it 'uses error_pages' do
192+
error_pages_helper
193+
end
194+
195+
it 'uses gitignore' do
196+
gitignore_helper
197+
end
190198
end

spec/support/gem_test_helpers.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,23 @@ def config_test_helper
145145
config_test_file = content('config/environments/test.rb')
146146
expect(config_test_file).to match(/^Rails.application.configure/)
147147
end
148+
149+
def error_pages_helper
150+
application_controller_file = content('app/controllers/application_controller.rb')
151+
expect(application_controller_file).to match('rescue_from Exception')
152+
expect(application_controller_file).to match('rescue_from ActiveRecord::RecordNotFound')
153+
expect(application_controller_file).to match('rescue_from ActionController::RoutingError')
154+
expect(application_controller_file).to match('server_error')
155+
expect(application_controller_file).to match('page_not_found')
156+
157+
route_file = content('config/routes.rb')
158+
expect(route_file).to match('unmatched_route')
159+
end
160+
161+
def gitignore_helper
162+
application_controller_file = content('.gitignore')
163+
expect(application_controller_file).to match('.DS_Store')
164+
expect(application_controller_file).to match('.secret')
165+
expect(application_controller_file).to match('.env')
166+
end
148167
end

spec/support/mail_test_helpers.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ def mail_test_helper(path)
1010
expect(file).to match('user_name:')
1111
expect(file).to match('password:')
1212
expect(file).to match('authentication:')
13-
unless content('config/settings.yml').present?
14-
expect(file).to match('host:')
15-
end
13+
expect(file).to match('host:') unless content('config/settings.yml').present?
1614
end
1715
end

templates/config/locales/view.en.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
en:
2-
view:
2+
view:
3+
error:
4+
internal_server_error: "We are sorry. An error occured. Our engineers are working on it. We will solve it as soon as possible."
5+
not_found: "We are sorry. Page was not found. It may have been moved."

templates/config/locales/view.tr.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
tr:
2-
view:
2+
view:
3+
error:
4+
internal_server_error: "Üzgünüz. Şu anda bir hata var. Mühendislerimiz bu hata üzerinde çalışma yapıyorlar. En kısa sürede çözeceğiz."
5+
not_found: "Üzgünüz. Bu sayfa bulunamadı. Taşınmış olabilir. Buraya gitmek istediğinizden emin olun."
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
rescue_from Exception, with: :server_error if Rails.env.production? or Rails.env.staging?
3+
rescue_from ActiveRecord::RecordNotFound, with: :page_not_found if Rails.env.production? or Rails.env.staging?
4+
rescue_from ActionController::RoutingError, with: :page_not_found if Rails.env.production? or Rails.env.staging?
5+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
3+
def server_error(exception)
4+
Rollbar.error "ApplicationController#server_error --exception: #{exception}"
5+
render template: 'errors/internal_server_error', status: 500
6+
end
7+
8+
def page_not_found
9+
render template: 'errors/not_found', status: 404
10+
end

templates/error_pages/error_route.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
if Rails.env.production? or Rails.env.staging?
3+
match '*unmatched_route', to: 'application#page_not_found', via: :all
4+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.well
2+
%p= t('view.internal_server_error')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.well
2+
%p= t('view.not_found')

0 commit comments

Comments
 (0)