Permalink
Browse files

Beef up AD::Rescue to replace global exception handling lost in Appli…

…cationController
  • Loading branch information...
josh committed Sep 15, 2009
1 parent 90d7ae2 commit 52aeb8d2e72223f9b40b0193c151c252a3f4fb09
Showing with 29 additions and 26 deletions.
  1. +16 −4 actionpack/lib/action_dispatch/middleware/rescue.rb
  2. +13 −22 actionpack/test/controller/rescue_test.rb
@@ -1,14 +1,26 @@
module ActionDispatch
class Rescue
- def initialize(app, rescuer)
- @app, @rescuer = app, rescuer
+ def initialize(app, rescuers = {}, &block)
+ @app, @rescuers = app, {}
+ rescuers.each { |exception, rescuer| rescue_from(exception, rescuer) }
+ instance_eval(&block) if block_given?
end
def call(env)
@app.call(env)
rescue Exception => exception
- env['action_dispatch.rescue.exception'] = exception
- @rescuer.call(env)
+ if rescuer = @rescuers[exception.class.name]
+ env['action_dispatch.rescue.exception'] = exception
+ rescuer.call(env)
+ else
+ raise exception
+ end
end
+
+ protected
+ def rescue_from(exception, rescuer)
+ exception = exception.class.name if exception.is_a?(Exception)
+ @rescuers[exception.to_s] = rescuer
+ end
end
end
@@ -227,12 +227,6 @@ def test_exception_in_parent_controller
end
end
-class ApplicationController < ActionController::Base
- rescue_from ActionController::RoutingError do
- render :text => 'no way'
- end
-end
-
class RescueControllerTest < ActionController::TestCase
def test_rescue_handler
get :not_authorized
@@ -331,24 +325,21 @@ def show_errors(exception)
end
end
- # test 'rescue routing exceptions' do
- # assert_equal 1, ApplicationController.rescue_handlers.length
- #
- # begin
- # with_test_routing do
- # get '/no_way'
- # assert_equal 'no way', response.body
- # end
- # ensure
- # ActionController::Base.rescue_handlers.clear
- # end
- # end
+ test 'rescue routing exceptions' do
+ app = ActionDispatch::Rescue.new(ActionController::Routing::Routes) do
+ rescue_from ActionController::RoutingError, lambda { |env| [200, {"Content-Type" => "text/html"}, "Gotcha!"] }
+ end
+ @integration_session = open_session(app)
+
+ get '/b00m'
+ assert_equal "Gotcha!", response.body
+ end
test 'unrescued exception' do
- with_test_routing do
- get '/b00m'
- assert_match(/Action Controller: Exception caught/, response.body)
- end
+ app = ActionDispatch::Rescue.new(ActionController::Routing::Routes)
+ @integration_session = open_session(app)
+
+ assert_raise(ActionController::RoutingError) { get '/b00m' }
end
private

0 comments on commit 52aeb8d

Please sign in to comment.