Rescue from dispatching 13

Posted by pratik
on Sunday, July 20

This question gets asked quite a few times, if there’s an easy way to handle exceptions raised while dispatching a request (routing exceptions, invalid method etc.) gracefully and show a customized error page. After this commit, there is.

rescue_from to the rescue

Now you can handle those exceptions inside your application.rb like :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class ApplicationController < ActionController::Base
  rescue_from ActionController::RoutingError, :with => :route_not_found
  rescue_from ActionController::MethodNotAllowed, :with => :invalid_method
  
  private
  
  def route_not_found
    render :text => 'What the fuck are you looking for ?', :status => :not_found
  end
  
  def invalid_method
    message = "Now, did your mom tell you to #{request.request_method.to_s.upcase} that ?"
    render :text => message, :status => :method_not_allowed
  end
end

Just make sure you don’t raise another exception from your rescue_from action!

Comments

Leave a response

  1. leethalJuly 20, 2008 @ 03:12 PM

    Hey, nice! Especially the :status => :not_found, I thought you had to do :status => 404

  2. Fredrik WJuly 20, 2008 @ 03:47 PM

    Sweet, I waited for this post since you said you’d blog in #rubyonrails.

    Sweet stuff, can’t wait for the next release! :)

  3. Anil WadghuleJuly 21, 2008 @ 07:43 PM

    Sweet!

  4. hiphapisJuly 24, 2008 @ 02:46 AM

    puck!! :)

  5. Kyle SlatterySeptember 05, 2008 @ 04:17 AM

    Wow, this is awesome. I’m definitely going to start using this right away, thanks!

  6. Henrik NOctober 24, 2008 @ 04:41 PM

    This is what I’ve been doing: http://henrik.nyh.se/2008/07/rails-404

  7. Henrik NOctober 24, 2008 @ 04:43 PM

    Bah, not linkified. Click my name in this comment for the blog post.

  8. Bill DavenportOctober 25, 2008 @ 04:42 PM

    Rails will translate the http response. There is an entire list of translated status codes here: http://www.codyfauser.com/2008/7/4/rails-http-status-code-to-symbol-mapping

    This list is grouped together nicely.

    leethal – July 20, 2008 @ 03:12 PM

    Hey, nice! Especially the :status => :not_found, I thought you had to do :status => 404

  9. Ben DoddNovember 25, 2008 @ 02:41 AM

    It’s nice to be able to acess the Exception object to get the exception message to display as well. I found out how to do this by reading the rails api (http://api.rubyonrails.org/).

    In your example you can just change the function and add a parameter, eg:

    def route_not_found(exception) render :text => exception.message, :status => :not_found end

  10. aotianlongDecember 03, 2008 @ 06:42 PM

    what a funny article!

    what relation is beteen invalid method and mom ….

  11. Ivan ShumkovDecember 16, 2008 @ 12:28 PM

    Hello, when i’m render template i’m see 500 Server Error?

  12. RamyaMarch 05, 2009 @ 10:18 PM

    hi

    Thats cool…but when i try to access the object and print it print [#,#,#,#] i dont know why…

    i have the following code in application.rb

    rescue_from Exception , :with => :runtime_error

    def runtime_error(exception) render :text => exception.message.to_s,:layout=> ‘bug.rhtml’, :status => not_found

    end

    can anyone say why i get wired characters

  13. PhilipMay 10, 2009 @ 01:02 AM

    Hi,

    Does this:

    class ApplicationController < ActionController::Base
    rescue_from ActionController::RoutingError, :with => :route_not_found

    really work? I am just testing out this feature on Rails v2.1.1 and it has taken me quite some time to find out it doesn’t seem to work as advertised here. After looking a bit more, I found an old post with comments that indicate the same problem: (see comments at the bottom) http://ryandaigle.com/articles/2007/9/24/what-s-new-in-edge-rails-better-exception-handling#comment-form

    What’s the update here? Thanks!

Comment