Utsav Kesharwani
Utsav Kesharwani

Reputation: 1745

Rescuing LoadError in Rails application

In my Rails 3 app, I am fetching path_info by:

Rails.application.routes.recognize_path(url, { :method => request.request_method }) rescue {}

If a crawler hits a URL like "http://localhost:3000/admin_", the above code raises following error:

LoadError: Expected /Users/user/myRailsApp/app/controllers/admin_controller.rb to define Admin_Controller
from /Users/user/.rvm/gems/ree-1.8.7-2012.02/gems/activesupport-3.0.20/lib/active_support/dependencies.rb:492:in `load_missing_constant'

I have two questions:

  1. Why is rescue not working? If I change it to rescue LoadError => e, exception is handled gracefully.
  2. Is there any other alternative rather than rescuing such exception(s)?

Upvotes: 2

Views: 569

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176562

If you omit the exception type, by default rescue will rescue only StandardError exceptions and subclasses.

LoadError doesn't inherit from StandardError:

LoadError.ancestors
 => [LoadError, ScriptError, Exception, Object, Kernel, BasicObject]

Therefore, the one-line rescue pattern doesn't work with a LoadError.

Upvotes: 1

Related Questions