2

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)?
Utsav Kesharwani
  • 1,595
  • 10
  • 21

1 Answers1

1

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.

Simone Carletti
  • 164,184
  • 42
  • 341
  • 356
  • Thanks. It answers the first question. What about the second one? It there any alternative to handle such URLs? – Utsav Kesharwani Sep 20 '15 at 10:50
  • I can't understand why you fetch the path info in that way rather using `request.env['PATH_INFO']` – Simone Carletti Sep 20 '15 at 11:31
  • `Rails.application.routes.recognize_path` gives me a hash with `controller` and `action` as keys. I need this hash to do some further processing. This works even for custom routes. `request.env['PATH_INFO']` doesn't give me that. – Utsav Kesharwani Sep 20 '15 at 12:06
  • In the controller you can access `controller_name` and `action_name` http://stackoverflow.com/questions/21399663/rails-get-the-controller-and-action-of-the-current-request – Simone Carletti Sep 20 '15 at 12:26
  • I have to fetch the path_info in a `lib` file to get this working: https://github.com/bendiken/rack-throttle/blob/master/lib/rack/throttle/limiter.rb#L6. Can't thus use `path_info` in controller. Please let me know if you need more details. – Utsav Kesharwani Sep 20 '15 at 12:40