5

I am working on a RoR website and would like to handle server errors (400, 404, 500, etc.) individually. Also, since the website is dynamic I would like to handle the errors within the rails environment rather than at the server level. An example of what I would like to do could be to present the user with optional material or a search bar when she bumps into a page or template that will not load or simply does not exist.

So, I did a bit of reading and I think that using the rescue_from exception handler is the way to go in my case. (Would be more than happy to hear if any of you have a different opinion).

I have a simple working prototype (see code below) up and running, however, I get an error when I include the following exception handler to the code:

rescue_from ActionController::MissingTemplate,          :with => :not_found #404

Now, I can't see that I have a spelling error and I have seen this line in code posted on the web. However, when I include it I get the following routing error:

Routing Error No route matches "/errorhandle" with {:method=>:get}

I am working on rails 2.3.5, perhaps that has got something to do with it?

class ApplicationController < ActionController::Base

    helper :all # include all helpers, all the time

    protect_from_forgery #See ActionController::RequestForgeryProtection for details

    #ActiveRecord exceptions
    rescue_from ActiveRecord::RecordNotFound, :with => :not_found #400   

    #ActiveResource exceptions  
    rescue_from ActiveResource::ResourceNotFound, :with => :not_found #404

    #ActionView exceptions
    rescue_from ActionView::TemplateError, :with => :not_found #500

    #ActionController exceptions
    rescue_from ActionController::RoutingError, :with => :not_found #404   

    rescue_from ActionController::UnknownController, :with => :not_found #404 

    rescue_from ActionController::MethodNotAllowed, :with => :not_found #405   

    rescue_from ActionController::InvalidAuthenticityToken, :with => :not_found #405

    rescue_from ActionController::UnknownAction, :with => :not_found #501

    # This particular exception causes all the rest to fail.... why?
    # rescue_from ActionController::MissingTemplate, :with => :not_found #404
    
    protected
    def not_found
        render :text => "Error", :status => 404
    end

    # Scrub sensitive parameters from your log
    # filter_parameter_logging :password 
end
5
  • 1
    Can you show us the relevant routes from routes.rb, if any? Commented Jan 14, 2010 at 14:25
  • Sure Trevoke, thanks for the quick reply. I haven't added anything to routes.rb so I guess this is pretty much the std. file: ActionController::Routing::Routes.draw do |map| map.resources :errorhandlers # ... # lots of lines commented out # ... map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' end Commented Jan 14, 2010 at 15:08
  • uh, not sure how to make the comment look nice and tidy uark. Hope it's readable anyways... Commented Jan 14, 2010 at 15:10
  • 1
    Edit your question to make the routes look nice and tidy Commented Jan 14, 2010 at 16:01
  • Veger: Can't seem to edit it now but I will work out how to do it for the next post. Cheers. Commented Jan 14, 2010 at 16:52

1 Answer 1

2

Take a quick look at these: http://www.ruby-forum.com/topic/47898

http://henrik.nyh.se/2008/09/404-invalid-rails-format

In particular, a post in the first link:

You can't use a regular 'rescue' keyword to rescue MissingTemplate exception.

Use rescue_action instead, for example:

def rescue_action(exception)
  if ::ActionController::MissingTemplate === exception
     render :text => 'rescued'
  else
     super
  end
end

Kent.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much Kent for your reply and the links, they were all very helpful. I will read up on the :: and general exception handling in RoR to better understand your reply. ...and then I might throw a few more questions out there if you don't mind? For now - thanks to all of you, it's great being part of such an active forum! /Maja
I'm not Kent - Kent is the one who answered that question in the first link :-) You're welcome. By all means, ask more questions, that's what this website is for! (just read the doc first, of course).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.