Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the ideal error to raise when the url entered by the user is incorrect?

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?

like image 552
gsmendoza Avatar asked Jul 16 '09 03:07

gsmendoza


People also ask

When should I raise my TypeError?

TypeError is raised whenever an operation is performed on an incorrect/unsupported object type. For example, using the + (addition) operator on a string and an integer value will raise TypeError.

How do you raise input error in Python?

When an error occurs in your program, you may either print a message and use sys. exit(1) to abort the program, or you may raise an exception. The latter task is easy. You just write raise E(message) , where E can be a known exception type in Python and message is a string explaining what is wrong.


1 Answers

You're looking for

ActionController::RoutingError

I like to do this in my application_controller:

  def not_found
    raise ActionController::RoutingError.new('Not Found')
  end

Then in other controllers, say pages#show for example, I can do:

def show
  unless @page = Page.find_by_permalink(params[:permalink])
    not_found
  end
end
like image 120
tybro0103 Avatar answered Oct 14 '22 13:10

tybro0103