Forum: Ruby on Rails Catching Exceptions in ActionController

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2005-12-11 03:13
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?
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2005-12-12 03:28
Can anyone help with this?  It sounds like it would be a common task.

List Recv 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?
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2005-12-12 04:15
(Received via mailing list)
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 Recv wrote:

> Can anyone help with this?  It sounds like it would be a common task.
>
> List Recv 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 Squires
http://somethinglearned.com
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 Kent Sibilev (Guest)
on 2005-12-12 04:42
(Received via mailing list)
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.
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2005-12-12 05:18
(Received via mailing list)
On 12/11/05, Kent Sibilev <ksruby@gmail.com> 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
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 Kent Sibilev (Guest)
on 2005-12-12 05:30
(Received via mailing list)
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.
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 Kent Sibilev (Guest)
on 2005-12-12 06:36
(Received via mailing list)
Good point. Thanks.

Kent.
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2005-12-18 09:49
Kent Sibilev 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?
This topic is locked and can not be replied to.