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!






Hey, nice! Especially the :status => :not_found, I thought you had to do :status => 404
Sweet, I waited for this post since you said you’d blog in #rubyonrails.
Sweet stuff, can’t wait for the next release! :)
Sweet!
puck!! :)
Wow, this is awesome. I’m definitely going to start using this right away, thanks!
This is what I’ve been doing: http://henrik.nyh.se/2008/07/rails-404
Bah, not linkified. Click my name in this comment for the blog post.
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
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
what a funny article!
what relation is beteen invalid method and mom ….
Hello, when i’m render template i’m see 500 Server Error?
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_foundend
can anyone say why i get wired characters
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!