From b2430b66ee143cc38d633131b1470c4d388a3c10 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Sat, 7 Oct 2023 18:02:11 +0200 Subject: [PATCH] Revise controller exception handling Don't catch everything during testing so that the underlying exception can be shown instead of just saying that a 500 happened --- app/controllers/application_controller.rb | 25 ++++++++++------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 74ca95b7..ab94ca83 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 @@ -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)