mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Allow Dispatcher exceptions to be handled in application.rb using rescue_from
This commit is contained in:
@@ -112,19 +112,23 @@ module ActionController #:nodoc:
|
||||
protected
|
||||
# Exception handler called when the performance of an action raises an exception.
|
||||
def rescue_action(exception)
|
||||
log_error(exception) if logger
|
||||
erase_results if performed?
|
||||
|
||||
# Let the exception alter the response if it wants.
|
||||
# For example, MethodNotAllowed sets the Allow header.
|
||||
if exception.respond_to?(:handle_response!)
|
||||
exception.handle_response!(response)
|
||||
end
|
||||
|
||||
if consider_all_requests_local || local_request?
|
||||
rescue_action_locally(exception)
|
||||
if handler_for_rescue(exception)
|
||||
rescue_action_with_handler(exception)
|
||||
else
|
||||
rescue_action_in_public(exception)
|
||||
log_error(exception) if logger
|
||||
erase_results if performed?
|
||||
|
||||
# Let the exception alter the response if it wants.
|
||||
# For example, MethodNotAllowed sets the Allow header.
|
||||
if exception.respond_to?(:handle_response!)
|
||||
exception.handle_response!(response)
|
||||
end
|
||||
|
||||
if consider_all_requests_local || local_request?
|
||||
rescue_action_locally(exception)
|
||||
else
|
||||
rescue_action_in_public(exception)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -200,7 +204,7 @@ module ActionController #:nodoc:
|
||||
def perform_action_with_rescue #:nodoc:
|
||||
perform_action_without_rescue
|
||||
rescue Exception => exception
|
||||
rescue_action_with_handler(exception) || rescue_action(exception)
|
||||
rescue_action(exception)
|
||||
end
|
||||
|
||||
def rescues_path(template_name)
|
||||
|
||||
@@ -62,6 +62,11 @@ class RescueController < ActionController::Base
|
||||
render :text => exception.message
|
||||
end
|
||||
|
||||
# This is a Dispatcher exception and should be in ApplicationController.
|
||||
rescue_from ActionController::RoutingError do
|
||||
render :text => 'no way'
|
||||
end
|
||||
|
||||
def raises
|
||||
render :text => 'already rendered'
|
||||
raise "don't panic!"
|
||||
@@ -378,6 +383,10 @@ class RescueTest < Test::Unit::TestCase
|
||||
assert_equal "RescueController::ResourceUnavailableToRescueAsString", @response.body
|
||||
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
|
||||
def with_all_requests_local(local = true)
|
||||
|
||||
Reference in New Issue
Block a user