From cc790834695c38a92e62edc3fe435e4102ffdc35 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Tue, 8 Jun 2010 11:59:09 -0400 Subject: [PATCH] Exceptions from views should be rescued based on the original exception. If a handler for original exception is missing then apply ActiveView::TemplateError [#2034 state:resolved] --- actionpack/lib/action_controller.rb | 1 + actionpack/lib/action_controller/base.rb | 6 +++- .../action_controller/metal/rescue_with_helper.rb | 14 ++++++++++ actionpack/test/controller/rescue_test.rb | 27 ++++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 actionpack/lib/action_controller/metal/rescue_with_helper.rb diff --git a/actionpack/lib/action_controller.rb b/actionpack/lib/action_controller.rb index c14393d..5667f93 100644 --- a/actionpack/lib/action_controller.rb +++ b/actionpack/lib/action_controller.rb @@ -28,6 +28,7 @@ module ActionController autoload :Rendering autoload :RequestForgeryProtection autoload :Rescue + autoload :RescueWithHelper autoload :Responder autoload :SessionManagement autoload :Streaming diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 8611d0d..798ad38 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -47,7 +47,9 @@ module ActionController AbstractController::Callbacks, # The same with rescue, append it at the end to wrap as much as possible. - Rescue + Rescue, + + RescueWithHelper ] MODULES.each do |mod| @@ -73,4 +75,4 @@ module ActionController end end -require "action_controller/deprecated/base" \ No newline at end of file +require "action_controller/deprecated/base" diff --git a/actionpack/lib/action_controller/metal/rescue_with_helper.rb b/actionpack/lib/action_controller/metal/rescue_with_helper.rb new file mode 100644 index 0000000..7894dea --- /dev/null +++ b/actionpack/lib/action_controller/metal/rescue_with_helper.rb @@ -0,0 +1,14 @@ +module ActionController #:nodoc: + module RescueWithHelper + + def rescue_with_handler(exception) + if ((exception.class == ActionView::TemplateError) && + (orig_exception = exception.original_exception) && + (orig_handler = handler_for_rescue(orig_exception))) + exception = orig_exception + end + super(exception) + end + + end +end diff --git a/actionpack/test/controller/rescue_test.rb b/actionpack/test/controller/rescue_test.rb index 0f64b77..a24de62 100644 --- a/actionpack/test/controller/rescue_test.rb +++ b/actionpack/test/controller/rescue_test.rb @@ -79,6 +79,14 @@ class RescueController < ActionController::Base render :text => 'no way' end + rescue_from ActionView::TemplateError do + render :text => 'action_view templater error' + end + + rescue_from IOError do + render :text => 'io error' + end + before_filter(:only => :before_filter_raises) { raise 'umm nice' } def before_filter_raises @@ -141,6 +149,14 @@ class RescueController < ActionController::Base def missing_template end + + def io_error_in_view + raise ActionView::TemplateError.new(nil, {}, IOError.new('this is io error')) + end + + def zero_division_error_in_view + raise ActionView::TemplateError.new(nil, {}, ZeroDivisionError.new('this is zero division error')) + end protected def deny_access @@ -228,6 +244,17 @@ class ControllerInheritanceRescueControllerTest < ActionController::TestCase end class RescueControllerTest < ActionController::TestCase + + def test_io_error_in_view + get :io_error_in_view + assert_equal 'io error', @response.body + end + + def test_zero_division_error_in_view + get :zero_division_error_in_view + assert_equal 'action_view templater error', @response.body + end + def test_rescue_handler get :not_authorized assert_response :forbidden -- 1.6.5.2