Catching Exceptions in ActionController


#1

I’d like to be able to catch ActionController::MissingTemplate
exceptions from within ActionControlle, but, MissingTemplate isn’t
defined within my controllers!!!

How is that possible - after all, all controller subclass
ActionController, so how are exceptions it defines not there!?

More importantly, how can I do this?


#2

Can anyone help with this? It sounds like it would be a common task.

List R. wrote:

I’d like to be able to catch ActionController::MissingTemplate
exceptions from within ActionControlle, but, MissingTemplate isn’t
defined within my controllers!!!

How is that possible - after all, all controller subclass
ActionController, so how are exceptions it defines not there!?

More importantly, how can I do this?


#3

Hi,

I’m afraid that I don’t know all this well enough to accurately
explain what (I think) is going on.

Anyhow, it sounds like you’re doing:

rescue MissingTemplate

when you should be doing

rescue ActionController::MissingTemplate

more below:

On 11-Dec-05, at 6:28 PM, List R. wrote:

Can anyone help with this? It sounds like it would be a common task.

List R. wrote:

I’d like to be able to catch ActionController::MissingTemplate
exceptions from within ActionControlle, but, MissingTemplate isn’t
defined within my controllers!!!

Right. MissingTemplate is a class that’s defined in the
ActionController module.

How is that possible - after all, all controller subclass
ActionController, so how are exceptions it defines not there!?

No, all controllers subclass ActionController::Base (via
ApplicationController). I.e. the class called “Base” in the
ActionController module.

Your code may be subclassing ActionController::Base but it has its
own module namespace (probably the root namespace) so any time your
code refers to a constant such as MissingTemplate it has to tell ruby
the full name - ActionController::MissingTemplate.

Bear in mind, all this has nothing to do with whether that error will
actually propagate up to a point where your code can rescue it…

And if I’m misunderstanding the true nature of your problem then
perhaps you should re-post your question with example code so that
people can get a better idea of what’s causing you grief.

Regards,
Trevor

Trevor S.
http://somethinglearned.com


#4

You can’t use a regular ‘rescue’ keyword to rescue MissingTemplate
exception.

Use rescue_action instead, for example:

def rescue_action(exception)
if ::ActionController::MissingTemplate === exception
render :text => ‘rescued’
else
super
end
end

Kent.


#5

On 12/11/05, Kent S. removed_email_address@domain.invalid wrote:

end

Kent.

I’d suggest using rescue_action_in_public instead so you still get the
stack trace while developing or accessing from the local machine.


rick
http://techno-weenie.net


#6

Good point. Thanks.

Kent.


#7

One correction. If you use ActionController::MissingTemplate directly
from
within your controller action, you will get NameError (uninitialized
constant
Base). You have to explicitly set the global scope when you refer to
framework classes like so ::ActionController::MissingTemplate.

Kent.


#8

Kent S. wrote:

One correction. If you use ActionController::MissingTemplate directly
from
within your controller action, you will get NameError (uninitialized
constant
Base). You have to explicitly set the global scope when you refer to
framework classes like so ::ActionController::MissingTemplate.

Kent.

Fascinating. I found the same thing with the rescue_action_in_public in
the Rails Book - you need to do case ::ActionController::RoutingError.

Could you explain a) why this is so and b) the usage of :: without being
preceded by a module?