views:

253

answers:

3

I have an action that searches records based on a url parameter. The url for the action looks something like this:

http://domain.com/records/filter/<filtercode>

If the user enters an incorrect filtercode, I would like to app to

  • raise an error so that Hoptoad will report to error to me.
  • render a 404 instead of a 500 in production env.

I understand that certain Rails errors such as ActiveRecord::NotFound and ActionController:RoutingError will render 404 in production env. So what I like to do is raise this error when a user enters an invalid filtercode in the url.

Now, my question: what's the ideal error to raise in this type of situation? Is there a list of Rails errors/exceptions in the net?

+2  A: 

If it searches records and doesn't find any the logical way would be to use the ActiveRecord::NotFound wouldn't it?

railsninja
+3  A: 

You should be catching any sort of error thrown by Rails as none of them are made to be user-facing... When an exception bubbles up to the HTTP response, it will always manifest as HTTP 500.

What is perfectly normal is to catch the exception and handle it by setting the HTTP status to 404 and output your own error perhaps saying "we searched for this, but didn't find anything". Or, if security is a concern, just say "not found" in a really fancy way.

Omega
A: 

I found an article on how to notify Hoptoad while rescuing exceptions. Might be useful to anyone reading this:

How to handle exceptions in controllers, rake tasks and regular classes using Hoptoad

gsmendoza