Skip to content

Display custom 404 page: invalid route / record not found - Bounty #428 #249

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

Merged
merged 1 commit into from
Nov 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class ApplicationController < ActionController::Base
after_action :record_visit
after_action :record_location

rescue_from ActiveRecord::RecordNotFound, with: :render_404 unless Rails.env.production?
rescue_from ActionController::RoutingError, with: :render_404 unless Rails.env.production?
rescue_from ActiveRecord::RecordNotFound, with: :render_404

protected

Expand Down Expand Up @@ -183,7 +182,7 @@ def not_on_achievements?
end

def render_404
render template: 'error/not_found', status: :not_found
render template: 'errors/not_found', status: :not_found
end

def render_500
Expand Down
21 changes: 21 additions & 0 deletions app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class ErrorsController < ApplicationController
def not_found
render status: :not_found
end

def unacceptable
respond_to do |format|
format.html { render 'public/422', status: :unprocessable_entity }
format.xml { head :unprocessable_entity }
format.json { head :unprocessable_entity }
end
end

def internal_error
respond_to do |format|
format.html { render 'public/500', status: :internal_server_error }
format.xml { head :internal_server_error }
format.json { head :internal_server_error }
end
end
end
1 change: 0 additions & 1 deletion app/views/error/not_found.json

This file was deleted.

1 change: 0 additions & 1 deletion app/views/error/not_found.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
If you don't believe you should be seeing this error,
#{ link_to "please contact us", contact_us_path }!

= render partial: "error/helpful_links"

= render partial: "errors/helpful_links"
1 change: 1 addition & 0 deletions app/views/errors/not_found.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions app/views/errors/not_found.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<error>404</error>
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Application < Rails::Application

config.rakismet.key = ENV['AKISMET_KEY']
config.rakismet.url = ENV['AKISMET_URL']

config.exceptions_app = self.routes
end
end

Expand Down
536 changes: 270 additions & 266 deletions config/routes.rb

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions spec/features/errors/internal_error_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "rails_helper"

feature "Custom 500 Page" do
before(:all) do
Rails.application.config.action_dispatch.show_exceptions = true
Rails.application.config.consider_all_requests_local = false
end

after(:all) do
Rails.application.config.action_dispatch.show_exceptions = false
Rails.application.config.consider_all_requests_local = true
end

scenario 'User is presented 500 page when an exception is raised' do
allow(User).to receive(:find_by_username!).and_raise(StandardError)

visit '/user_causes_500_error'

expect(page).
to have_content('Coderwall had an issue but hold on to your localhosts')
end
end
25 changes: 25 additions & 0 deletions spec/features/errors/not_found_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "rails_helper"

feature "Custom 404 Page" do
before(:all) do
Rails.application.config.action_dispatch.show_exceptions = true
Rails.application.config.consider_all_requests_local = false
end

after(:all) do
Rails.application.config.action_dispatch.show_exceptions = false
Rails.application.config.consider_all_requests_local = true
end

scenario 'user is presented 404 page when they visit invalid path' do
visit '/fake/path/doesnt/match/route'

expect(page).to have_content('Uh oh, something went wrong!')
end

scenario 'user is presented 404 page when then visit a bad user path' do
visit '/not_a_real_username'

expect(page).to have_content('Uh oh, something went wrong!')
end
end