Changeset 7822
- Timestamp:
- 10/10/07 02:34:42 (1 year ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/rescue.rb (modified) (2 diffs)
- trunk/actionpack/test/controller/rescue_test.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r7820 r7822 1 1 *SVN* 2 3 * rescue_from accepts :with => lambda { |exception| ... } or a normal block. #9827 [lifofifo] 2 4 3 5 * 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 72 72 # end 73 73 # end 74 def rescue_from(*klasses )74 def rescue_from(*klasses, &block) 75 75 options = klasses.extract_options! 76 unless options.has_key?(:with) # allow nil77 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.") 78 78 end 79 79 … … 193 193 194 194 def handler_for_rescue(exception) 195 if handler = rescue_handlers[exception.class.name] 195 case handler = rescue_handlers[exception.class.name] 196 when Symbol 196 197 method(handler) 198 when Proc 199 handler.bind(self) 197 200 end 198 201 end trunk/actionpack/test/controller/rescue_test.rb
r7597 r7822 10 10 end 11 11 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 12 24 rescue_from NotAuthorized, :with => :deny_access 13 25 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 14 37 15 38 def raises … … 30 53 end 31 54 55 def not_allowed 56 raise NotAllowed 57 end 58 59 def invalid_request 60 raise InvalidRequest 61 end 62 32 63 def record_invalid 33 64 raise RecordInvalid 34 65 end 35 66 67 def bad_gateway 68 raise BadGateway 69 end 70 71 def resource_unavailable 72 raise ResourceUnavailable 73 end 74 36 75 def missing_template 37 76 end … … 246 285 end 247 286 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 248 307 protected 249 308 def with_all_requests_local(local = true)