Skip to content

Commit

Permalink
Revise controller exception handling
Browse files Browse the repository at this point in the history
Don't catch everything during testing so that the underlying exception
can be shown instead of just saying that a 500 happened
  • Loading branch information
Earlopain committed Oct 7, 2023
1 parent 39ce66d commit b2430b6
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ class ApplicationController < ActionController::Base
before_action :normalize_params
around_action :with_time_zone

rescue_from Exception, with: :rescue_exception
EXCEPTION_TYPES = {
ActionController::BadRequest => 400,
ActionController::ParameterMissing => 400,
ActionController::InvalidAuthenticityToken => 403,
ActionController::UnpermittedParameters => 403,
ActiveRecord::RecordNotFound => 404,
ActionController::UnknownFormat => 406,
PG::ConnectionBad => 503,
}.freeze
exception_classes = Rails.env.test? ? EXCEPTION_TYPES.keys : Exception
rescue_from(*exception_classes, with: :rescue_exception)

def set_start_time
@start_time = Time.current.to_f
Expand All @@ -25,19 +35,6 @@ def with_time_zone(&)
Time.use_zone(Config.time_zone, &)
end

EXCEPTION_TYPES = {
ActionController::BadRequest => 400,
ActionController::ParameterMissing => 400,
ActionController::InvalidAuthenticityToken => 403,
ActionController::UnpermittedParameters => 403,
ActiveRecord::RecordNotFound => 404,
ActionController::UnknownFormat => 406,
ActionView::MissingTemplate => 500,
ActionView::Template::Error => 500,
ActiveRecord::QueryCanceled => 500,
PG::ConnectionBad => 503,
}.freeze

def rescue_exception(exception)
@exception = exception
@exception = @exception.cause if @exception.is_a?(ActionView::Template::Error)
Expand Down

0 comments on commit b2430b6

Please sign in to comment.