Skip to content

Commit

Permalink
KBP-140 #time 2h - Configure custom error pages
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdibayhan committed Nov 13, 2017
1 parent 5abb135 commit f30c5a7
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 8 deletions.
13 changes: 13 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,27 @@ Metrics/LineLength:

Metrics/ClassLength:
Exclude:
- 'spec/support/gem_test_helpers.rb'
- 'lib/cybele/generators/app_generator.rb'

Metrics/ModuleLength:
Exclude:
- 'spec/support/gem_test_helpers.rb'

Style/AccessorMethodName:
Exclude:
- 'lib/cybele/generators/app_generator.rb'

Metrics/MethodLength:
CountComments: false
Max: 15
Exclude:
- 'spec/support/gem_test_helpers.rb'

Metrics/AbcSize:
Exclude:
- 'spec/support/gem_test_helpers.rb'
- 'spec/support/mail_test_helpers.rb'

Metrics/BlockLength:
CountComments: false
Expand All @@ -35,6 +47,7 @@ Metrics/BlockLength:
- 'spec/**/*.rb'
- 'lib/cybele/app_builder.rb'
- 'lib/cybele/generators/app_generator.rb'
- 'spec/support/cybele_test_helpers.rb'

Style/FrozenStringLiteralComment:
EnforcedStyle: when_needed
Expand Down
1 change: 1 addition & 0 deletions lib/cybele.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
require 'cybele/helpers/mailer'
require 'cybele/helpers/paperclip'
require 'cybele/helpers/devise'
require 'cybele/helpers/error_pages'
require 'cybele/app_builder'
5 changes: 3 additions & 2 deletions lib/cybele/app_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AppBuilder < Rails::AppBuilder
include Cybele::Helpers::Mailer
include Cybele::Helpers::Paperclip
include Cybele::Helpers::Devise
include Cybele::Helpers::ErrorPages

def readme
template 'README.md.erb',
Expand Down Expand Up @@ -75,7 +76,7 @@ def setup_gitignore_files
end

def setup_gitignore_folders
%w(
%w[
app/assets/images
db/migrate
spec/support
Expand All @@ -84,7 +85,7 @@ def setup_gitignore_folders
spec/views
spec/controllers
spec/helpers
).each do |dir|
].each do |dir|
empty_directory_with_keep_file dir
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/cybele/generators/app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ def gitignore_files_and_folders
build :setup_gitignore_folders
end

def configure_error_pages
say 'Setup custom exception pages and 404 page'
build :configure_error_pages
end

def goodbye
say 'Congratulations! That\'s all...', :green
end
Expand Down
37 changes: 37 additions & 0 deletions lib/cybele/helpers/error_pages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module Cybele
module Helpers
module ErrorPages
def configure_error_pages
inject_into_file 'app/controllers/application_controller.rb',
template_content('error_pages/error_control.erb'),
before: 'self.responder'

inject_into_file 'app/controllers/application_controller.rb',
template_content('error_pages/error_method.erb'),
after: 'protect_from_forgery with: :exception'

inject_into_file 'config/routes.rb',
template_content('error_pages/error_route.erb'),
before: /^end/

create_error_pages_files
end

private

def create_error_pages_files
# Server Error
template 'error_pages/internal_server_error.html.haml',
'app/views/errors/internal_server_error.html.haml',
force: true

# Not Found Error
template 'error_pages/not_found.html.haml',
'app/views/errors/not_found.html.haml',
force: true
end
end
end
end
10 changes: 9 additions & 1 deletion spec/features/new_default_project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
expect(gemfile_file).to match(/^gem 'devise-async'/)

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

sidekiq_schedule_file = content('config/sidekiq_schedule.yml')
expect(sidekiq_schedule_file).to match(/-> Daily at midnight/)
Expand Down Expand Up @@ -207,4 +207,12 @@
it 'uses devise' do
devise_test_helper
end

it 'uses error_pages' do
error_pages_helper
end

it 'uses gitignore' do
gitignore_helper
end
end
8 changes: 8 additions & 0 deletions spec/features/new_not_default_project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,12 @@
it 'uses devise' do
devise_test_helper
end

it 'uses error_pages' do
error_pages_helper
end

it 'uses gitignore' do
gitignore_helper
end
end
19 changes: 19 additions & 0 deletions spec/support/gem_test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,23 @@ def config_test_helper
config_test_file = content('config/environments/test.rb')
expect(config_test_file).to match(/^Rails.application.configure/)
end

def error_pages_helper
application_controller_file = content('app/controllers/application_controller.rb')
expect(application_controller_file).to match('rescue_from Exception')
expect(application_controller_file).to match('rescue_from ActiveRecord::RecordNotFound')
expect(application_controller_file).to match('rescue_from ActionController::RoutingError')
expect(application_controller_file).to match('server_error')
expect(application_controller_file).to match('page_not_found')

route_file = content('config/routes.rb')
expect(route_file).to match('unmatched_route')
end

def gitignore_helper
application_controller_file = content('.gitignore')
expect(application_controller_file).to match('.DS_Store')
expect(application_controller_file).to match('.secret')
expect(application_controller_file).to match('.env')
end
end
4 changes: 1 addition & 3 deletions spec/support/mail_test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ def mail_test_helper(path)
expect(file).to match('user_name:')
expect(file).to match('password:')
expect(file).to match('authentication:')
unless content('config/settings.yml').present?
expect(file).to match('host:')
end
expect(file).to match('host:') unless content('config/settings.yml').present?
end
end
5 changes: 4 additions & 1 deletion templates/config/locales/view.en.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
en:
view:
view:
error:
internal_server_error: "We are sorry. An error occured. Our engineers are working on it. We will solve it as soon as possible."
not_found: "We are sorry. Page was not found. It may have been moved."
5 changes: 4 additions & 1 deletion templates/config/locales/view.tr.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
tr:
view:
view:
error:
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."
not_found: "Üzgünüz. Bu sayfa bulunamadı. Taşınmış olabilir. Buraya gitmek istediğinizden emin olun."
5 changes: 5 additions & 0 deletions templates/error_pages/error_control.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

rescue_from Exception, with: :server_error if Rails.env.production? or Rails.env.staging?
rescue_from ActiveRecord::RecordNotFound, with: :page_not_found if Rails.env.production? or Rails.env.staging?
rescue_from ActionController::RoutingError, with: :page_not_found if Rails.env.production? or Rails.env.staging?

10 changes: 10 additions & 0 deletions templates/error_pages/error_method.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


def server_error(exception)
Rollbar.error "ApplicationController#server_error --exception: #{exception}"
render template: 'errors/internal_server_error', status: 500
end

def page_not_found
render template: 'errors/not_found', status: 404
end
4 changes: 4 additions & 0 deletions templates/error_pages/error_route.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

if Rails.env.production? or Rails.env.staging?
match '*unmatched_route', to: 'application#page_not_found', via: :all
end
2 changes: 2 additions & 0 deletions templates/error_pages/internal_server_error.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.well
%p= t('view.internal_server_error')
2 changes: 2 additions & 0 deletions templates/error_pages/not_found.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.well
%p= t('view.not_found')

0 comments on commit f30c5a7

Please sign in to comment.