Skip to content

Commit

Permalink
Ensure error templates are always rendered as HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulDoyle-EA committed Mar 6, 2024
1 parent e216a9b commit db5193e
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 90 deletions.
124 changes: 62 additions & 62 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,63 +30,63 @@ GEM
specs:
aasm (5.5.0)
concurrent-ruby (~> 1.0)
actioncable (7.1.3)
actionpack (= 7.1.3)
activesupport (= 7.1.3)
actioncable (7.1.3.2)
actionpack (= 7.1.3.2)
activesupport (= 7.1.3.2)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
zeitwerk (~> 2.6)
actionmailbox (7.1.3)
actionpack (= 7.1.3)
activejob (= 7.1.3)
activerecord (= 7.1.3)
activestorage (= 7.1.3)
activesupport (= 7.1.3)
actionmailbox (7.1.3.2)
actionpack (= 7.1.3.2)
activejob (= 7.1.3.2)
activerecord (= 7.1.3.2)
activestorage (= 7.1.3.2)
activesupport (= 7.1.3.2)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.1.3)
actionpack (= 7.1.3)
actionview (= 7.1.3)
activejob (= 7.1.3)
activesupport (= 7.1.3)
actionmailer (7.1.3.2)
actionpack (= 7.1.3.2)
actionview (= 7.1.3.2)
activejob (= 7.1.3.2)
activesupport (= 7.1.3.2)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.2)
actionpack (7.1.3)
actionview (= 7.1.3)
activesupport (= 7.1.3)
actionpack (7.1.3.2)
actionview (= 7.1.3.2)
activesupport (= 7.1.3.2)
nokogiri (>= 1.8.5)
racc
rack (>= 2.2.4)
rack-session (>= 1.0.1)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
actiontext (7.1.3)
actionpack (= 7.1.3)
activerecord (= 7.1.3)
activestorage (= 7.1.3)
activesupport (= 7.1.3)
actiontext (7.1.3.2)
actionpack (= 7.1.3.2)
activerecord (= 7.1.3.2)
activestorage (= 7.1.3.2)
activesupport (= 7.1.3.2)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.1.3)
activesupport (= 7.1.3)
actionview (7.1.3.2)
activesupport (= 7.1.3.2)
builder (~> 3.1)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
activejob (7.1.3)
activesupport (= 7.1.3)
activejob (7.1.3.2)
activesupport (= 7.1.3.2)
globalid (>= 0.3.6)
activemodel (7.1.3)
activesupport (= 7.1.3)
activerecord (7.1.3)
activemodel (= 7.1.3)
activesupport (= 7.1.3)
activemodel (7.1.3.2)
activesupport (= 7.1.3.2)
activerecord (7.1.3.2)
activemodel (= 7.1.3.2)
activesupport (= 7.1.3.2)
timeout (>= 0.4.0)
activerecord-session_store (2.1.0)
actionpack (>= 6.1)
Expand All @@ -95,13 +95,13 @@ GEM
multi_json (~> 1.11, >= 1.11.2)
rack (>= 2.0.8, < 4)
railties (>= 6.1)
activestorage (7.1.3)
actionpack (= 7.1.3)
activejob (= 7.1.3)
activerecord (= 7.1.3)
activesupport (= 7.1.3)
activestorage (7.1.3.2)
actionpack (= 7.1.3.2)
activejob (= 7.1.3.2)
activerecord (= 7.1.3.2)
activesupport (= 7.1.3.2)
marcel (~> 1.0)
activesupport (7.1.3)
activesupport (7.1.3.2)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
Expand Down Expand Up @@ -204,8 +204,7 @@ GEM
dotenv-rails (2.8.1)
dotenv (= 2.8.1)
railties (>= 3.2)
drb (2.2.0)
ruby2_keywords
drb (2.2.1)
erubi (1.12.0)
execjs (2.8.1)
factory_bot (6.4.5)
Expand Down Expand Up @@ -249,11 +248,12 @@ GEM
http-accept (1.7.0)
http-cookie (1.0.5)
domain_name (~> 0.5)
i18n (1.14.1)
i18n (1.14.3)
concurrent-ruby (~> 1.0)
racc (~> 1.7)
io-console (0.7.2)
io-event (1.2.2)
irb (1.11.1)
irb (1.11.2)
rdoc
reline (>= 0.4.2)
jquery-rails (4.6.0)
Expand All @@ -271,7 +271,7 @@ GEM
net-imap
net-pop
net-smtp
marcel (1.0.2)
marcel (1.0.4)
matrix (0.4.2)
mime-types (3.5.2)
mime-types-data (~> 3.2015)
Expand Down Expand Up @@ -324,28 +324,28 @@ GEM
stringio
public_suffix (5.0.4)
racc (1.7.3)
rack (2.2.8)
rack (2.2.8.1)
rack-session (1.0.2)
rack (< 3)
rack-test (2.1.0)
rack (>= 1.3)
rackup (1.0.0)
rack (< 3)
webrick
rails (7.1.3)
actioncable (= 7.1.3)
actionmailbox (= 7.1.3)
actionmailer (= 7.1.3)
actionpack (= 7.1.3)
actiontext (= 7.1.3)
actionview (= 7.1.3)
activejob (= 7.1.3)
activemodel (= 7.1.3)
activerecord (= 7.1.3)
activestorage (= 7.1.3)
activesupport (= 7.1.3)
rails (7.1.3.2)
actioncable (= 7.1.3.2)
actionmailbox (= 7.1.3.2)
actionmailer (= 7.1.3.2)
actionpack (= 7.1.3.2)
actiontext (= 7.1.3.2)
actionview (= 7.1.3.2)
activejob (= 7.1.3.2)
activemodel (= 7.1.3.2)
activerecord (= 7.1.3.2)
activestorage (= 7.1.3.2)
activesupport (= 7.1.3.2)
bundler (>= 1.15.0)
railties (= 7.1.3)
railties (= 7.1.3.2)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
Expand All @@ -357,9 +357,9 @@ GEM
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (7.1.3)
actionpack (= 7.1.3)
activesupport (= 7.1.3)
railties (7.1.3.2)
actionpack (= 7.1.3.2)
activesupport (= 7.1.3.2)
irb
rackup (>= 1.0.0)
rake (>= 12.2)
Expand All @@ -374,7 +374,7 @@ GEM
rdoc (6.6.2)
psych (>= 4.0.0)
regexp_parser (2.9.0)
reline (0.4.2)
reline (0.4.3)
io-console (~> 0.5)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
Expand Down Expand Up @@ -469,7 +469,7 @@ GEM
stringio (3.1.0)
sucker_punch (3.2.0)
concurrent-ruby (~> 1.0)
thor (1.3.0)
thor (1.3.1)
tilt (2.2.0)
timeout (0.4.1)
timers (4.3.5)
Expand All @@ -492,7 +492,7 @@ GEM
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
zeitwerk (2.6.12)
zeitwerk (2.6.13)

PLATFORMS
ruby
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/flood_risk_engine/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ class ApplicationController < ActionController::Base

layout ->(_) { FloodRiskEngine.config.layout }

rescue_from StandardError do |e|
Airbrake.notify e
Rails.logger.error "Unhandled exception: #{e}\n#{e.backtrace}"
redirect_to error_path("500")
end

protected

# http://jacopretorius.net/2014/01/force-page-to-reload-on-browser-back-in-rails.html
Expand Down
20 changes: 18 additions & 2 deletions app/controllers/flood_risk_engine/errors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@
module FloodRiskEngine
class ErrorsController < ApplicationController

before_action :set_html_response_format

def show
render(
template: file_for(template),
locals: { message: exception.try(:message), enrollment: }
locals: { message: exception.try(:message), enrollment: },
status: response_status
)
end

protected

# Changes the request format to HTML to always display the error pages
def set_html_response_format
request.format = :html
end

def response_status
template.to_i.positive? ? template : :internal_server_error
end

def error_code
@error_code ||= params[:id]
end
Expand All @@ -30,7 +42,11 @@ def template_path(name)
end

def template
@template ||= template_exists(error_code) ? error_code : "generic"
@template ||= if template_exists(error_code)
error_code
else
(error_code.to_i.positive? ? "generic" : "404")
end
end

def file_for(name)
Expand Down
4 changes: 2 additions & 2 deletions config/locales/flood_risk_engine/errors.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ en:
heading: Page not found
if_you_typed: If you typed the web address, check it is correct.
if_you_pasted: If you pasted the web address, check you copied the entire address.
call_our_helpline: If the web address is correct or you selected a link or button, call our helpline on 03708 506 506,
call_our_helpline: If the web address is correct or you selected a link or button, call our helpline on 03708 506 506.
error_404:
heading: Page not found
if_you_typed: If you typed the web address, check it is correct.
if_you_pasted: If you pasted the web address, check you copied the entire address.
call_our_helpline: If the web address is correct or you selected a link or button, call our helpline on 03708 506 506,
call_our_helpline: If the web address is correct or you selected a link or button, call our helpline on 03708 506 506.
error_422:
heading: There was a problem
sorry: Sorry, the system wasn't able to complete that step.
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@

mount DefraRubyEmail::Engine => "/email"

# See http://patrickperey.com/railscast-053-handling-exceptions/
# See http://railscasts.com/episodes/53-handling-exceptions-revised
get "(errors)/:id", to: "errors#show", as: "error"
end
# rubocop:enable Metrics/BlockLength
23 changes: 0 additions & 23 deletions spec/controllers/flood_risk_engine/errors_controller_spec.rb

This file was deleted.

Loading

0 comments on commit db5193e

Please sign in to comment.