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

Handle uncaught exceptions #1291

Merged
merged 3 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions app/controllers/waste_carriers_engine/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

module WasteCarriersEngine
class ApplicationController < ActionController::Base
include WasteCarriersEngine::CanAddDebugLogging

# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
Expand All @@ -10,5 +12,13 @@ class ApplicationController < ActionController::Base
layout "application"

default_form_builder GOVUKDesignSystemFormBuilder::FormBuilder

rescue_from StandardError do |e|
Airbrake.notify e
Rails.logger.error "Unhandled exception: #{e}"
log_transient_registration_details("Uncaught system error", @transient_registration)
redirect_to "/bo/pages/system_error"
end

end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,37 @@ module WasteCarriersEngine
module CanAddDebugLogging
extend ActiveSupport::Concern

# rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
def log_transient_registration_details(description, transient_registration)
return unless FeatureToggle.active?(:additional_debug_logging)

error = if transient_registration.nil?
StandardError.new("#{description}: transient_registration is nil")
else
StandardError.new(
"#{description}: " \
"type: #{transient_registration.class}, " \
"reg_identifier #{transient_registration.reg_identifier}, " \
"from_magic_link: #{from_magic_link(transient_registration)}, " \
"workflow_state: #{transient_registration.workflow_state}, " \
"workflow_history: #{transient_registration.workflow_history}, " \
"tier: #{transient_registration.tier}, " \
"account_email: #{transient_registration.account_email}, " \
"expires_on: #{transient_registration.expires_on}, " \
"renew_token: #{renew_token(transient_registration)}, " \
"metaData.route: #{transient_registration.metaData.route}, " \
"created_at: #{transient_registration.created_at}, " \
"orders: #{transient_registration.finance_details&.orders}, " \
"payments: #{transient_registration.finance_details&.payments}"
)
end
Airbrake.notify(error, reg_identifier: transient_registration.reg_identifier) if defined?(Airbrake)
Rails.logger.warn error
details = if transient_registration.nil?
{ transient_registration: nil }
else
{ type: transient_registration.class.to_s,
reg_identifier: transient_registration.reg_identifier,
from_magic_link: from_magic_link(transient_registration),
workflow_state: transient_registration.workflow_state,
workflow_history: transient_registration.workflow_history.to_s,
tier: transient_registration.tier,
account_email: transient_registration.account_email,
expires_on: transient_registration.expires_on,
renew_token: renew_token(transient_registration),
"metaData.route": transient_registration.metaData.route,
created_at: transient_registration.created_at,
orders: transient_registration.finance_details&.orders.to_s,
payments: transient_registration.finance_details&.payments.to_s }
end
Airbrake.notify(StandardError.new(description), details) if defined?(Airbrake)
Rails.logger.warn "#{description}: #{details}"

# Handle any exceptions which arise while logging
rescue StandardError => e
Airbrake.notify(e, reg_identifier: transient_registration.reg_identifier) if defined?(Airbrake)
Rails.logger.warn "Error writing transient registration details to the log: #{e}"
Airbrake.notify(e, reg_identifier: transient_registration&.reg_identifier) if defined?(Airbrake)
Rails.logger.warn "Error writing debugging information for transient registration " \
"#{transient_registration&.reg_identifier} to the log: #{e}"
end
# rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize

private

Expand Down
8 changes: 8 additions & 0 deletions app/views/pages/system_error.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<% content_for :title, I18n.t(".system_error_title") %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-m"><%= I18n.t(".system_error_heading") %></h1>
<p class="govuk-body"><%= I18n.t(".system_error_text") %></p>
</div>
</div>
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ en:
other: "%{count} addresses found"

# Custom error pages
system_error_title: System error
system_error_heading: A system error has occurred
system_error_text: We are unable to complete this request at this time. Please try again later.

invalid_reg_identifier_title: Cannot find page
invalid_reg_identifier_heading: We cannot find that page
invalid_reg_identifier_text: If you have recently completed a registration then check your email.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,16 @@ module WasteCarriersEngine
expect(Airbrake).to have_received(:notify).at_least(:once)
end
end

context "when the additional logging raises an exception" do
before { allow(transient_registration).to receive(:metaData).and_raise(StandardError) }

it "catches the exception and notifies Airbrake anyway" do
described_class.new.log_transient_registration_details("foo", transient_registration)

expect(Airbrake).to have_received(:notify)
end
end
end
end
end
Expand Down