Skip to content

Commit

Permalink
Implemented custom dynamic error pages. Fixes #221.
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminSchaaf committed Apr 25, 2017
1 parent 1898e24 commit d1cec7e
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 200 deletions.
9 changes: 9 additions & 0 deletions app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ErrorsController < ApplicationController
def not_found
render(status: 404)
end

def internal_server_error
render(status: 500)
end
end
7 changes: 7 additions & 0 deletions app/views/errors/internal_server_error.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.panel.panel-danger
.panel-heading
%h1
%span.glyphicon.glyphicon-warning-sign
<strong>500</strong> Internal Server Error!
.panel-body
%p This is a bug, please report it to your administrator!
8 changes: 8 additions & 0 deletions app/views/errors/not_found.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.panel.panel-danger
.panel-heading
%h1
%span.glyphicon.glyphicon-warning-sign
<strong>404</strong> Page Not Found!
.panel-body
%p The page you are looking for does not exist.
%p If you think this is a bug please report it to your administrator!
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class Application < Rails::Application
# Make view helpers, view specific
config.action_controller.include_all_helpers = false

# Use dynamic error pages
config.exceptions_app = self.routes

# News config file
config.news = config_for(:news)
end
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Rails.application.routes.draw do
match '/404', to: 'errors#not_found', via: :all
match '/500', to: 'errors#internal_server_error', via: :all

namespace :api do
namespace :v1 do
resources :leagues, shallow: true, only: [:index, :show] do
Expand Down
67 changes: 0 additions & 67 deletions public/404.html

This file was deleted.

67 changes: 0 additions & 67 deletions public/422.html

This file was deleted.

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

This file was deleted.

19 changes: 19 additions & 0 deletions spec/controllers/errors_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'rails_helper'

describe ErrorsController do
describe 'GET #not_found' do
it 'returns http not_foudn' do
get :not_found

expect(response).to have_http_status(:not_found)
end
end

describe 'GET #internal_server_error' do
it 'returns http internal_server_error' do
get :internal_server_error

expect(response).to have_http_status(:internal_server_error)
end
end
end
24 changes: 24 additions & 0 deletions spec/features/errors_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rails_helper'

feature 'User accesses invalid page' do
# Make it like we're accessing in production
around :each do |example|
Rails.application.config.consider_all_requests_local = false
Rails.application.config.action_dispatch.show_exceptions = true
example.run
Rails.application.config.consider_all_requests_local = true
Rails.application.config.action_dispatch.show_exceptions = false
end

scenario 'authenticated user' do
visit team_path(-1)

expect(page).to have_content '404'
end

scenario 'unauthenticated user' do
visit team_path(-1)

expect(page).to have_content '404'
end
end
9 changes: 9 additions & 0 deletions spec/views/errors/internal_server_error.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rails_helper'

describe 'errors/internal_server_error.html.haml' do
it 'renders' do
render

expect(rendered).to include('500')
end
end
9 changes: 9 additions & 0 deletions spec/views/errors/not_found.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rails_helper'

describe 'errors/not_found.html.haml' do
it 'renders' do
render

expect(rendered).to include('404')
end
end

0 comments on commit d1cec7e

Please sign in to comment.