Skip to content

Commit

Permalink
Catch errors with custom error pages yielded w/ application layout
Browse files Browse the repository at this point in the history
  • Loading branch information
sfate committed Dec 28, 2016
1 parent a093d3d commit 6bf0207
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 122 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ApplicationController < ActionController::Base
include Concerns::UserCampaignable
include Concerns::AutomaticDiscount
include Concerns::Moodboards
include Concerns::Errors

# Marketing related concerns
include Marketing::Gtm::Controller::Container
Expand Down
36 changes: 36 additions & 0 deletions app/controllers/concerns/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Concerns
module Errors
extend ActiveSupport::Concern

included do
rescue_from ActiveRecord::RecordNotFound, with: :render_404
rescue_from ActionController::UnknownController, with: :render_404
rescue_from ActionController::UnknownAction, with: :render_404
rescue_from ActiveRecord::RecordInvalid, with: :render_422
rescue_from Exception, with: :render_500
end

protected

def render_404
render_error(code: 404)
end

def render_422
render_error(code: 422)
end

def render_500
render_error(code: 500)
end

def render_error(code: 404)
respond_to do |format|
format.html { render "errors/#{code}", status: code }
format.xml { render nothing: true, status: code }
format.json { render nothing: true, status: code }
end
end

end
end
8 changes: 0 additions & 8 deletions app/controllers/errors_controller.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/controllers/products/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def collection_template
if page.page_is_lookbook? || @collection_options
page.template_path
else
{file: 'public/404', layout: false, status: :not_found}
raise ActiveRecord::RecordNotFound
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/spree/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def failure
end

def passthru
render :file => "#{Rails.root}/public/404", :formats => [:html], :status => 404, :layout => false
raise ActiveRecord::RecordNotFound
end

def auth_hash
Expand Down
2 changes: 0 additions & 2 deletions app/helpers/errors_helper.rb

This file was deleted.

13 changes: 13 additions & 0 deletions app/views/errors/404.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- @title = "The page you were looking for doesn't exist (404)"

= render 'errors/analytics_javascript', locals: { code: 404, message: 'not found'}

.error-page.text-center
h1
| Looking for something?
h2
| <em>Sorry,</em> this page doesn't exist.
p
| Try our #{link_to "homepage", root_path} to see what's new and start customizing.
p.thin-bottom
| Still can't find what you're looking for? #{link_to "E-mail us", 'mailto:team@fameandpartners.com'} at team@fameandpartners.com.
13 changes: 13 additions & 0 deletions app/views/errors/422.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- @title = "The change you wanted was rejected (422)"

= render 'errors/analytics_javascript', locals: { code: 422, message: 'unprocessable entity'}

.error-page.text-center
h1
| The change you wanted was rejected.
h2
| Maybe you tried to change something you didn't have access to.
p.thin-bottom
= link_to 'Go to homepage', root_path
p.thin-bottom
| Still can't find what you're looking for? #{link_to "E-mail us", 'mailto:team@fameandpartners.com'} at team@fameandpartners.com.
17 changes: 17 additions & 0 deletions app/views/errors/500.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
- @title = "We're sorry, but something went wrong (500)"

= render 'errors/analytics_javascript', locals: { code: 500, message: 'server error'}

.error-page.text-center
h1
| So sorry to interrupt your shopping experience.
p
| In the meantime, here is a $20 voucher to make up for your time - just use this code <strong>@checkout: OOPS20</strong></p>
p
| We should be back up in a few minutes but if you would like some further assistance, feel free to email or call us:
p
= link_to 'team@fameandpartners.com', 'mailto:team@fameandpartners.com'
p
| Our site engineers are currently making some upgrades to make it even better.
p.thin-bottom
= link_to 'Go to homepage', root_path
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
.error-page.text-center
h1
| Looking for something?
h2
| <em>Sorry,</em> this page doesn't exist.
p
| Try our #{link_to "homepage", '/'} to see what's new and start customizing.
p.thin-bottom
| Still can't find what you're looking for? #{link_to "E-mail us", 'mailto:team@fameandpartners.com'} at team@fameandpartners.com.
- code = local_assigns[:code]
- message = local_assigns[:message]

javascript:
(function (i, s, o, g, r, a, m) {
Expand All @@ -22,4 +15,4 @@ javascript:
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-41247818-1', 'auto');
ga('send', 'pageview');
ga('send', 'event', '404', 'not found');
ga('send', 'event', "#{code}", "#{message}");
3 changes: 0 additions & 3 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,6 @@ class Application < Rails::Application

config.skip_mail_delivery = false

#Use route for error handling
config.exceptions_app = self.routes

config.generators do |generator|
generator.test_framework :rspec
end
Expand Down
6 changes: 0 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,6 @@

mount Spree::Core::Engine, at: '/'

########
# 404 Error Pages
########

get "/404", :to => "errors#not_found", :via => :all

############################################
# Storefront (Search, Checkout and Payments)
############################################
Expand Down
26 changes: 0 additions & 26 deletions public/422.html

This file was deleted.

64 changes: 0 additions & 64 deletions public/500.html

This file was deleted.

2 changes: 1 addition & 1 deletion spec/controllers/products/collections_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

it 'when querying a inexistent permalink' do
get :show, permalink: 'nothing'
expect(response).to render_template(file: 'public/404', layout: false)
expect(response).to render_template(file: 'errors/404', layout: 'redesign/application')
expect(response).to have_http_status(:not_found)
end
end
Expand Down

0 comments on commit 6bf0207

Please sign in to comment.