Declarative exception-handling in your controllers - Rails 2.0 a feature a day #2

I know I said I was going to try and keep the features in “a feature a day” to those not mentioned in popular places, but this feature is one of the few nicest features in Rails made by a contributor outside of the Rails core so I couldn’t resist mentioning it.

First, an example

class CommentsController < ActionController::Base
  rescue_from Comment::Spammy, :with => :moderate_comment
  rescue_from ActiveRecord::RecordNotFound, :with => :redirect_if_not_found

  protected
    def moderate_comment
      # Handle the exception, like placing the comment in a moderation queue
      # and then rendering a different action.
    end

    def redirect_if_not_found
      # Redirect somewhere...
    end
end

Compare this to what you had to do with Rails 1.2.6 and earlier:

class CommentsController < ActionController::Base

  def rescue_action_in_public(exception)
    case exception
      when Comment::Spammy
        # Handle the exception as above.
      when ActiveRecord::RecordNotFound
        # Redirect somewhere...
      else
        super
    end
  end
end

Much clearer and reads almost like English. I don’t think I even need to explain what the code in the 1st example is trying to do.

Let’s try another example

Here’s another common use-case: dealing bad create/update actions. With rescue_from, you can simply do this:

class ApplicationController < ActionController::Base
  rescue_from ActiveRecord::RecordInvalid do |exception|
    render :action => (exception.record.new_record? ? :new : :edit)
  end
end

No need for actions to be coded like this anymore!

def create
  @comment = comment.create!(params[:comment])
rescue ActiveRecord::RecordInvalid
  render :action => :new
end

This patch was committed in revision 7597 after a short discussion on the syntax and implementation over at the ticket. (More improvements on the implementation over at ticket #9645 for the interested.)

About the contributor, Norbert Crombach

Norbert Crombach (Rails Trac username: norbert, WorkingWithRails profile) is another regular Rails contributor hailing from the Netherlands. The rescue_from is, in my opinion, one of his more significant contributions (and what a great one it is too).

Some final words

While the new declarative rescue_from macro is useful, I’ve found myself liking the merb way of dealing with exceptions, where exception handling is delegated to an Exception controller. You can use layouts, templates, helpers and filters just like in ye plain olde controller. I’m still not so sure if it’s worth making a patch for Rails to emulate this behavior yet though!

Update: Rails 2.0 a feature a day #3 is now available Concatenate your stylesheets and JavaScripts in 3 seconds

8 Comments & TrackBacks ()

Paper doll icon
Martin's Gravatar

Nice post!

One correction. Norbert has left Fingertips and is now doing consulting. ( I found out when I caught up with him at the local Ruby Users group in London last night).

Posted by: Martin on December 11, 2007 7pm

Paper doll icon
links for 2007-12-12 « Bloggitation's Gravatar

[…] Declarative exception-handling in your controllers Rails 2.0 a feature a day #2 (tags: ruby rails programming) […]

Posted by: links for 2007-12-12 « Bloggitation on December 12, 2007 8am

Paper doll icon
Interesting Rails Tidbits #1's Gravatar

[…] Yeow demonstrates the very clean way in which you can handle controller-level exceptions in Rails […]

Posted by: Interesting Rails Tidbits #1 on December 12, 2007 6pm

Paper doll icon
john's Gravatar

I too really like merbs error handling. Merbs gotta lot of potential!!

Posted by: john on December 12, 2007 9pm

Paper doll icon
Infovore » links for 2007-12-18's Gravatar

[…] Declarative exception-handling in your controllers - Rails 2.0 a feature a day #2 - redemption in a … “…this feature is one of the few nicest features in Rails made by a contributor outside of the Rails core so I couldn’t resist mentioning it.” More on #rescue_from, with some nice use cases. (tags: rails rubyonrails ruby exceptionhandling exceptions) […]

Posted by: Infovore » links for 2007-12-18 on December 19, 2007 7am

Paper doll icon
Xavier Noria's Gravatar

There was another patch before 2.0 was out that enhanced rescue_from in a few ways, see ticket #10079. (Disclaimer, I am the author.)

Posted by: Xavier Noria on December 27, 2007 5am

Paper doll icon
Tecker.LOG » ?????????????????Rails 2.0?? #2(??)'s Gravatar

[…] ?????????????????Rails 2.0?? #2(??) ???Declarative exception-handling in your controllers - Rails 2.0 a feature a day #2 […]

Posted by: Tecker.LOG » ?????????????????Rails 2.0?? #2(??) on June 2, 2008 8pm

Paper doll icon
Enfranchised Mind » Functional (Meta)?Programming Stunts for Ruby and Groovy (and a Little Perl)'s Gravatar

[…] Note that Rails 2.0 apparently jumped on this approach, too: here is a nice example. […]

Posted by: Enfranchised Mind » Functional (Meta)?Programming Stunts for Ruby and Groovy (and a Little Perl) on June 25, 2008 2am

You can subscribe to the RSS feed for comments on this post.

Sorry, this entry is no longer accepting comments. If you have something you really want to say, you can write me.