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

Ticket #9449 (closed enhancement: fixed)

Opened 10 months ago

Last modified 8 months ago

[PATCH] rescue_from class method for AC::Base

Reported by: norbert Assigned to: core
Priority: normal Milestone: 1.x
Component: ActionPack Version: edge
Severity: minor Keywords: rescue exception rails2
Cc:

Description

This is a patch that introduces a piece of code written by John Wilger as class methods in ActionController. In short, it provides a nicer way to handle exceptions outside of controller actions.

For example,

class ApplicationController < ActionController::Base
   handle_exception User::NotAuthorized, :with => :not_authorized # self defined exception

   protected
     def not_authorized
       ...
     end
end

whereas previously you had to override rescue_action and possibly call super, which is not well documented. This can also be used for, say, ActiveRecord exceptions raised by methods with a bang!.

In my opinion this improvement in exception handling semantics is relatively small enough to be considered for inclusion.

Attachments

action_controller_exception_handler.diff (5.3 kB) - added by norbert on 08/30/07 23:50:39.
action_controller_rescue_from.2.diff (5.7 kB) - added by norbert on 09/10/07 22:47:00.
It's called rescue_from now
action_controller_rescue_from.3.diff (7.3 kB) - added by norbert on 09/13/07 14:34:00.
Using different machinery

Change History

08/30/07 23:50:39 changed by norbert

  • attachment action_controller_exception_handler.diff added.

09/07/07 16:28:48 changed by lifofifo

I'd like to call the method just bare naked "handle". Kinda like "verify".

Also, it would make sense to pass exception object to handler method. As it'd be very useful for exceptions such as ActiveRecord::RecordInvalid where exception.record carries the AR object in scrutiny.

09/07/07 17:09:48 changed by lifofifo

My +1 for the patch if it lets me do something like :

class ApplicationController < ActionController::Base
  handle ActiveRecord::RecordInvalid, :with => :bad_bad_boy
  
  def bad_bad_boy
    render :action => (exception.record.new_record? ? 'new' : 'edit')
  end
end

Thanks :-)

09/10/07 22:06:11 changed by david

I propose:

class ApplicationController < ActionController::Base
   rescue_from User::NotAuthorized, :with => :not_authorized

09/10/07 22:47:00 changed by norbert

  • attachment action_controller_rescue_from.2.diff added.

It's called rescue_from now

09/10/07 22:51:06 changed by norbert

  • keywords changed from exceptions to rescue exception.
  • summary changed from [PATCH] handle_exception class method for AC::Base to [PATCH] rescue_from class method for AC::Base.

david, this patch implements what we discussed.

The method to register handlers is now called rescue_from, and handler methods that take arguments get called with the exception.

09/11/07 10:14:10 changed by manfred

Cool feature, someone at our company uses something very similar in one of our projects (: Applies cleanly and tests run, +1 for inclusion.

09/13/07 14:34:00 changed by norbert

  • attachment action_controller_rescue_from.3.diff added.

Using different machinery

09/13/07 14:38:46 changed by norbert

Okay, after discussing it with bitsweat and manfred I updated the patch to work with rescue_action from within. And the handlers are now stored in a nice, clean class_inheritable_hash instead of manually written accessors. Finally the exception classes are stored by class name, as with the other defined mechanisms in AC::Rescue.

09/23/07 21:42:25 changed by bitsweat

  • keywords changed from rescue exception to rescue exception rails2.

09/23/07 21:56:29 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [7597]) Introduce ActionController::Base.rescue_from to declare exception-handling methods. Cleaner style than the case-heavy rescue_action_in_public. Closes #9449.

09/24/07 19:56:18 changed by bitsweat

(In [7618]) Move rescue_action_with_handler from rescue_action to perform_action so it isn't clobbered by test overrides. References #9449, closes #9645.

11/09/07 16:20:11 changed by fxn

This feature was later enhaced in #10079, added to trunk in [8081].