rescue_from issue with AWS - uninitialized constant

The new rescue_from in edge rails seems to conflict with ActionWebService, which I need for various other purposes. When I define, for example (with AWS gem installed):

<b>rescue_from ActionController::RoutingError, :with => :page_not_found</b>

I get the exception:

<b>uninitialized constant ActionWebService::Dispatcher::ActionController::Base</b>

OK, so AWS interfers with the inheritance chain I guess, but if I define:

<b>rescue_from Object::ActionController::RoutingError, :with => :page_not_found</b>

I get no errors, but then the rescue_from method isn't invoked either.

Anyone got any advice? I can get around this with rescue_action_in_public, but thats just not as nice.

Cheers, --Kip

The new rescue_from in edge rails

rescue_from comes with Rails 2.

seems to conflict with ActionWebService, which I need for various other purposes. When I define, for example (with AWS gem installed):

<b>rescue_from ActionController::RoutingError, :with => :page_not_found</b>

I get the exception:

<b>uninitialized constant ActionWebService::Dispatcher::ActionController::Base</b>

OK, so AWS interfers with the inheritance chain I guess, but if I define:

The problem seems to be that ActionController::RoutingError is an unknown constant at the time that file is being interpreted. If that's correct that's unrelated to rescue_from, it just happens that Ruby sees a constant, tries to resolve it because it is an argument of a method it is calling (rescue_from), and fails.

The first thing I'd try is to pass the exception class name instead of the exception class itself:

     rescue_from "ActionController::RoutingError", :with => :page_not_found

If the exception gets raised it will be certainly already loaded and rescue_from will be able to find the match.

-- fxn

> <b>uninitialized constant > ActionWebService::Dispatcher::ActionController::Base</b>

> OK, so AWS interfers with the inheritance chain I guess, but if I > define:

The problem seems to be that ActionController::RoutingError is an
unknown constant at the time that file is being interpreted. If that's
correct that's unrelated to rescue_from, it just happens that Ruby
sees a constant, tries to resolve it because it is an argument of a
method it is calling (rescue_from), and fails.

The first thing I'd try is to pass the exception class name instead of
the exception class itself:

 rescue\_from &quot;ActionController::RoutingError&quot;, :with  

=> :page_not_found

I've seen ::ActionController::RoutingError work too I think.

Fred

Good.

For Kip: AWS defines a constant ActionController somewhere within its namespace. If putting a leading "::" works it would happen that rescue_from is invoked somehow within that namespace[*] and "ActionContoller" is found relative to something instead of as the top-level constant. The one in AWS does not have a child RoutingError constant, and since constant name resolution has no backtracking so to speak it fails at that point.

-- fxn

[*] Somehow means the way constant name resolution works.

Very helpful guys, thank you. The String argument to rescue_from works fine. Of course I made a stupid error for my example: ActionController::RoutingError is never going to be rescued in this way (since it relies on a normal controller execution).

Therefore there is still a reason to use rescue_action(_in_public) in order to handle that error (unless I missed something entirely!)

Cheers, --Kip

Why it follows?

rescue_from is written in a way that does not assume the exceptions you declare will ever exist. That's why it understands a string with an exception class name as well as an excection class.

In its implementation if a string is unknown as constant by the time some exception is being processed it just ignores it and continues. That's by design.

-- fxn

Very helpful guys, thank you. The String argument to rescue_from works fine. Of course I made a stupid error for my example: ActionController::RoutingError is never going to be rescued in this way (since it relies on a normal controller execution).

Therefore there is still a reason to use rescue_action(_in_public) in order to handle that error (unless I missed something entirely!)

Why it follows?

rescue_from is written in a way that does not assume the exceptions you declare will ever exist. That's why it understands a string with an exception class name as well as an excection class.

I think Kip's point is that ActionController::RoutingError is thrown
when a controller could not be found that matched the url, so having
something in a controller to rescue_from it is futile.

Fred

Oh yes.