diff --git a/app/controllers/waste_carriers_engine/errors_controller.rb b/app/controllers/waste_carriers_engine/errors_controller.rb new file mode 100644 index 000000000..54cd220b2 --- /dev/null +++ b/app/controllers/waste_carriers_engine/errors_controller.rb @@ -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 diff --git a/app/views/waste_carriers_engine/errors/error_401.html.erb b/app/views/waste_carriers_engine/errors/error_401.html.erb new file mode 100644 index 000000000..5d1f376df --- /dev/null +++ b/app/views/waste_carriers_engine/errors/error_401.html.erb @@ -0,0 +1,6 @@ +

+ <%= t(".heading") %> +

+ +

<%= t(".sorry") %>

+

<%= t(".clarification") %>

diff --git a/app/views/waste_carriers_engine/errors/error_403.html.erb b/app/views/waste_carriers_engine/errors/error_403.html.erb new file mode 100644 index 000000000..5d1f376df --- /dev/null +++ b/app/views/waste_carriers_engine/errors/error_403.html.erb @@ -0,0 +1,6 @@ +

+ <%= t(".heading") %> +

+ +

<%= t(".sorry") %>

+

<%= t(".clarification") %>

diff --git a/app/views/waste_carriers_engine/errors/error_404.html.erb b/app/views/waste_carriers_engine/errors/error_404.html.erb new file mode 100644 index 000000000..5d1f376df --- /dev/null +++ b/app/views/waste_carriers_engine/errors/error_404.html.erb @@ -0,0 +1,6 @@ +

+ <%= t(".heading") %> +

+ +

<%= t(".sorry") %>

+

<%= t(".clarification") %>

diff --git a/app/views/waste_carriers_engine/errors/error_422.html.erb b/app/views/waste_carriers_engine/errors/error_422.html.erb new file mode 100644 index 000000000..5d1f376df --- /dev/null +++ b/app/views/waste_carriers_engine/errors/error_422.html.erb @@ -0,0 +1,6 @@ +

+ <%= t(".heading") %> +

+ +

<%= t(".sorry") %>

+

<%= t(".clarification") %>

diff --git a/app/views/waste_carriers_engine/errors/error_generic.html.erb b/app/views/waste_carriers_engine/errors/error_generic.html.erb new file mode 100644 index 000000000..3e7f7a830 --- /dev/null +++ b/app/views/waste_carriers_engine/errors/error_generic.html.erb @@ -0,0 +1,6 @@ +

+ <%= t(".heading") %> +

+ +

<%= t(".sorry") %>

+

<%= t(".try_again") %>

diff --git a/config/environments/test.rb b/config/environments/test.rb index cc0ebf1fb..1a6f25f97 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -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. diff --git a/config/initializers/exception_handling.rb b/config/initializers/exception_handling.rb new file mode 100644 index 000000000..60b7588f4 --- /dev/null +++ b/config/initializers/exception_handling.rb @@ -0,0 +1,3 @@ +unless Rails.application.config.consider_all_requests_local + Rails.application.config.exceptions_app = WasteCarriersEngine::Engine.routes +end diff --git a/config/locales/errors/en.yml b/config/locales/errors/en.yml new file mode 100644 index 000000000..42e0df517 --- /dev/null +++ b/config/locales/errors/en.yml @@ -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. diff --git a/config/routes.rb b/config/routes.rb index d8c1ac931..7b9ec0507 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb index 1c19f08b2..ac3f2184b 100644 --- a/spec/dummy/config/environments/test.rb +++ b/spec/dummy/config/environments/test.rb @@ -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. diff --git a/spec/requests/waste_carriers_engine/errors_spec.rb b/spec/requests/waste_carriers_engine/errors_spec.rb new file mode 100644 index 000000000..04be1badd --- /dev/null +++ b/spec/requests/waste_carriers_engine/errors_spec.rb @@ -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