Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7822

Show
Ignore:
Timestamp:
10/10/07 02:34:42 (1 year ago)
Author:
bitsweat
Message:

rescue_from accepts :with => lambda { |exception| ... } or a normal block. Closes #9827.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r7820 r7822  
    11*SVN* 
     2 
     3* rescue_from accepts :with => lambda { |exception| ... } or a normal block.  #9827 [lifofifo] 
    24 
    35* Add :status to redirect_to allowing users to choose their own response code without manually setting headers. #8297 [codahale, chasgrundy] 
  • trunk/actionpack/lib/action_controller/rescue.rb

    r7719 r7822  
    7272      #     end 
    7373      # end 
    74       def rescue_from(*klasses
     74      def rescue_from(*klasses, &block
    7575        options = klasses.extract_options! 
    76         unless options.has_key?(:with) # allow nil 
    77           raise ArgumentError, "Need a handler. Supply an options hash that has a :with key as the last argument." 
     76        unless options.has_key?(:with) 
     77          block_given? ? options[:with] = block : raise(ArgumentError, "Need a handler. Supply an options hash that has a :with key as the last argument.") 
    7878        end 
    7979 
     
    193193 
    194194      def handler_for_rescue(exception) 
    195         if handler = rescue_handlers[exception.class.name] 
     195        case handler = rescue_handlers[exception.class.name] 
     196        when Symbol 
    196197          method(handler) 
     198        when Proc 
     199          handler.bind(self) 
    197200        end 
    198201      end 
  • trunk/actionpack/test/controller/rescue_test.rb

    r7597 r7822  
    1010  end 
    1111 
     12  class NotAllowed < StandardError 
     13  end 
     14 
     15  class InvalidRequest < StandardError 
     16  end 
     17 
     18  class BadGateway < StandardError 
     19  end 
     20 
     21  class ResourceUnavailable < StandardError 
     22  end 
     23 
    1224  rescue_from NotAuthorized, :with => :deny_access 
    1325  rescue_from RecordInvalid, :with => :show_errors 
     26 
     27  rescue_from NotAllowed, :with => proc { head :forbidden } 
     28  rescue_from InvalidRequest, :with => proc { |exception| render :text => exception.message } 
     29 
     30  rescue_from BadGateway do 
     31    head :status => 502 
     32  end 
     33 
     34  rescue_from ResourceUnavailable do |exception| 
     35    render :text => exception.message 
     36  end 
    1437 
    1538  def raises 
     
    3053  end 
    3154 
     55  def not_allowed 
     56    raise NotAllowed 
     57  end 
     58 
     59  def invalid_request 
     60    raise InvalidRequest 
     61  end 
     62 
    3263  def record_invalid 
    3364    raise RecordInvalid 
    3465  end 
    3566   
     67  def bad_gateway 
     68    raise BadGateway 
     69  end 
     70 
     71  def resource_unavailable 
     72    raise ResourceUnavailable 
     73  end 
     74 
    3675  def missing_template 
    3776  end 
     
    246285  end 
    247286 
     287  def test_proc_rescue_handler 
     288    get :not_allowed 
     289    assert_response :forbidden 
     290  end 
     291 
     292  def test_proc_rescue_handle_with_argument 
     293    get :invalid_request 
     294    assert_equal "RescueController::InvalidRequest", @response.body 
     295  end 
     296 
     297  def test_block_rescue_handler 
     298    get :bad_gateway 
     299    assert_response 502 
     300  end 
     301 
     302  def test_block_rescue_handler_with_argument 
     303    get :resource_unavailable 
     304    assert_equal "RescueController::ResourceUnavailable", @response.body 
     305  end 
     306 
    248307  protected 
    249308    def with_all_requests_local(local = true)