Exception Notifier Plugin and Bad Routes

Found an annoying thing the other day. Mark and I use the Exception Notifier plugin by Jamis Buck on Pledgie to notify us of exceptions in the wild. It’s a great plugin.

Only problem is, as the site grew in popularity, we would get errors of bad routes, like the Google Accelerator trying to fetch URLs that don’t exist. This usually fails with a 500 application error, so we’d get a lot of exceptions from “bots” like this for nothing. If a URL that does not exist is accessed, one would typically think that “404 not found” is a better choice of response. I think so too. Here’s what you do to make it return 404 instead of 500.

Put this in your application.rb (ApplicationController):

1
2
3
4
5
6
7
8
9
  protected
  def rescue_action_in_public(exception)
    case exception
    when ::ActionController::RoutingError
      render_404
    else
      super
    end
  end

That’s it, now bad routes will return 404 errors.

You can optionally change the case statement to a simple if/else, but as a case statement you have the option to put in other exceptions and perform additional processing if needed.

Comments