Skip to content

Commit

Permalink
Create custom error page for Pundit errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Nat York committed Apr 4, 2017
1 parent 304a1ed commit 4073dc8
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ErrorsController < ApplicationController
def forbidden
render status: 403
end
end
7 changes: 7 additions & 0 deletions app/views/errors/forbidden.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div class='main'>
<h1>Woops!</h1>
<p>You do not have access to this page.</p>
<% if !current_person %>
<p> This is most likely being caused because you are not <%= link_to 'signed in', '/auth/google_oauth2' %>.</p>
<% end %>
</div>
6 changes: 6 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@ class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

config.action_dispatch
.rescue_responses["Pundit::NotAuthorizedError"] = :forbidden

require Rails.root.join('lib/custom_public_exceptions')
config.exceptions_app = CustomPublicExceptions.new(Rails.public_path)
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
get 'auth/failure', to: redirect('/')
get 'signout', to: 'sessions#destroy', as: 'signout'
get 'authentication/google', to: 'authentication#google'
match '/403', to: 'errors#forbidden', via: :all

resources :sessions, only: [:destroy]
resources :people
Expand Down
10 changes: 10 additions & 0 deletions lib/custom_public_exceptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CustomPublicExceptions < ActionDispatch::PublicExceptions
def call(env)
status = env["PATH_INFO"][1..-1]
if status == '403'
Rails.application.routes.call(env)
else
super
end
end
end
9 changes: 9 additions & 0 deletions spec/controllers/errors_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rails_helper'

RSpec.describe ErrorsController, type: :controller do
describe 'GET #forbidden' do
subject { get :forbidden }
it { is_expected.to render_template :forbidden }
it { is_expected.to have_http_status(403) }
end
end
19 changes: 19 additions & 0 deletions spec/lib/custom_public_exceptions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'rails_helper'

RSpec.describe CustomPublicExceptions do
describe '#call' do
subject { described_class.new(Rails.public_path).call(env) }

context 'the status is 403' do
let(:env) { Rack::MockRequest.env_for('/403') }
it { is_expected.to be_instance_of(Array) }
it { is_expected.to start_with 403 }
end

context 'the status is not 403' do
let(:env) { Rack::MockRequest.env_for('/500') }
it { is_expected.to be_instance_of(Array) }
it { is_expected.to start_with 500 }
end
end
end

0 comments on commit 4073dc8

Please sign in to comment.