Skip to content
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

Add error pages for unhandled exceptions #269

Merged
merged 2 commits into from
Sep 24, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions app/controllers/waste_carriers_engine/errors_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module WasteCarriersEngine
class ErrorsController < ApplicationController
def show
render(
template: file_for(template),
locals: { message: exception.try(:message) }
)
end

protected

def error_code
@error_code ||= params[:id]
end

def template_exists(name)
File.exist?(template_path(name))
end

def template_path(name)
File.expand_path(
"app/views/#{file_for(name)}.html.erb",
WasteCarriersEngine::Engine.root
)
end

def template
@template ||= template_exists(error_code) ? error_code : "generic"
end

def file_for(name)
"waste_carriers_engine/errors/error_#{name}"
end

def exception
env["action_dispatch.exception"]
end
end
end
6 changes: 6 additions & 0 deletions app/views/waste_carriers_engine/errors/error_401.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1 class="heading-large">
<%= t(".heading") %>
</h1>

<p><%= t(".sorry") %></p>
<p><%= t(".clarification") %></p>
6 changes: 6 additions & 0 deletions app/views/waste_carriers_engine/errors/error_403.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1 class="heading-large">
<%= t(".heading") %>
</h1>

<p><%= t(".sorry") %></p>
<p><%= t(".clarification") %></p>
6 changes: 6 additions & 0 deletions app/views/waste_carriers_engine/errors/error_404.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1 class="heading-large">
<%= t(".heading") %>
</h1>

<p><%= t(".sorry") %></p>
<p><%= t(".clarification") %></p>
6 changes: 6 additions & 0 deletions app/views/waste_carriers_engine/errors/error_422.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1 class="heading-large">
<%= t(".heading") %>
</h1>

<p><%= t(".sorry") %></p>
<p><%= t(".clarification") %></p>
6 changes: 6 additions & 0 deletions app/views/waste_carriers_engine/errors/error_generic.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1 class="heading-large">
<%= t(".heading") %>
</h1>

<p><%= t(".sorry") %></p>
<p><%= t(".try_again") %></p>
4 changes: 2 additions & 2 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
config.serve_static_files = true
config.static_cache_control = 'public, max-age=3600'

# Show full error reports and disable caching.
config.consider_all_requests_local = true
# Show error pages and disable caching.
config.consider_all_requests_local = false
config.action_controller.perform_caching = false

# Raise exceptions instead of rendering exception templates.
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/exception_handling.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
unless Rails.application.config.consider_all_requests_local
Rails.application.config.exceptions_app = WasteCarriersEngine::Engine.routes
end
28 changes: 28 additions & 0 deletions config/locales/errors/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
en:
waste_carriers_engine:
errors:
error_generic:
heading: Something went wrong
sorry: Sorry, there was a technical problem. It has been reported to our support team.
clarification: If it happens again, call the Environment Agency helpline 03708 506 506.
try_again: Please try again in a few minutes.
error_401:
heading: Unauthorised
sorry: Sorry, you don't have permission to access this page.
clarification: Please check the web address you entered is correct.
error_403:
# As per https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4
# We do not wish to make this information available to the client, therefore using messaging for 404-not-found instead.
heading: Page not found
sorry: Sorry, we can't find that page.
clarification: Please check the web address you entered is correct.
link: Start again
error_404:
heading: Page not found
sorry: Sorry, we can't find that page.
clarification: Please check the web address you entered was correct.
link: Start again
error_422:
heading: There was a problem
sorry: Sorry, the system wasn't able to complete that step.
clarification: You could go back and try it again.
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@
on: :collection
end

# See http://patrickperey.com/railscast-053-handling-exceptions/
get "(errors)/:id", to: "errors#show", as: "error"

# Static pages with HighVoltage - don't include "/pages/" in the path
resources :pages, only: [:show], controller: "pages", path: ""
end
4 changes: 2 additions & 2 deletions spec/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
config.serve_static_files = true
config.static_cache_control = 'public, max-age=3600'

# Show full error reports and disable caching.
config.consider_all_requests_local = true
# Show error pages and disable caching.
config.consider_all_requests_local = false
config.action_controller.perform_caching = false

# Raise exceptions instead of rendering exception templates.
Expand Down
17 changes: 17 additions & 0 deletions spec/requests/waste_carriers_engine/errors_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "rails_helper"

module WasteCarriersEngine
RSpec.describe "Errors", type: :request do
describe "#show" do
it "renders matching error template when one exists" do
get error_path("401")
expect(response).to render_template(:error_401)
end

it "renders generic error template when no matching error template exists" do
get error_path("unknown")
expect(response).to render_template(:error_generic)
end
end
end
end