Skip to content

Commit

Permalink
Allow Dispatcher exceptions to be handled in application.rb using res…
Browse files Browse the repository at this point in the history
…cue_from
  • Loading branch information
lifo committed Jul 16, 2008
1 parent c64d749 commit 90c930f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
28 changes: 16 additions & 12 deletions actionpack/lib/action_controller/rescue.rb
Expand Up @@ -112,19 +112,23 @@ def rescue_from(*klasses, &block)
protected protected
# Exception handler called when the performance of an action raises an exception. # Exception handler called when the performance of an action raises an exception.
def rescue_action(exception) def rescue_action(exception)
log_error(exception) if logger if handler_for_rescue(exception)
erase_results if performed? rescue_action_with_handler(exception)
else
log_error(exception) if logger
erase_results if performed?


# Let the exception alter the response if it wants. # Let the exception alter the response if it wants.
# For example, MethodNotAllowed sets the Allow header. # For example, MethodNotAllowed sets the Allow header.
if exception.respond_to?(:handle_response!) if exception.respond_to?(:handle_response!)
exception.handle_response!(response) exception.handle_response!(response)
end end


if consider_all_requests_local || local_request? if consider_all_requests_local || local_request?
rescue_action_locally(exception) rescue_action_locally(exception)
else else
rescue_action_in_public(exception) rescue_action_in_public(exception)
end
end end
end end


Expand Down Expand Up @@ -200,7 +204,7 @@ def rescue_action_with_handler(exception)
def perform_action_with_rescue #:nodoc: def perform_action_with_rescue #:nodoc:
perform_action_without_rescue perform_action_without_rescue
rescue Exception => exception rescue Exception => exception
rescue_action_with_handler(exception) || rescue_action(exception) rescue_action(exception)
end end


def rescues_path(template_name) def rescues_path(template_name)
Expand Down
9 changes: 9 additions & 0 deletions actionpack/test/controller/rescue_test.rb
Expand Up @@ -62,6 +62,11 @@ class ResourceUnavailableToRescueAsString < StandardError
render :text => exception.message render :text => exception.message
end end


# This is a Dispatcher exception and should be in ApplicationController.
rescue_from ActionController::RoutingError do
render :text => 'no way'
end

def raises def raises
render :text => 'already rendered' render :text => 'already rendered'
raise "don't panic!" raise "don't panic!"
Expand Down Expand Up @@ -378,6 +383,10 @@ def test_block_rescue_handler_with_argument_as_string
assert_equal "RescueController::ResourceUnavailableToRescueAsString", @response.body assert_equal "RescueController::ResourceUnavailableToRescueAsString", @response.body
end end


def test_rescue_dispatcher_exceptions
RescueController.process_with_exception(@request, @response, ActionController::RoutingError.new("Route not found"))
assert_equal "no way", @response.body
end


protected protected
def with_all_requests_local(local = true) def with_all_requests_local(local = true)
Expand Down

1 comment on commit 90c930f

@jweissman
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sick.

Please sign in to comment.