Why can't I catch this exception?

(this is in the context of Rails, buts its primarily a Ruby question
dealing with exception handling)

This is driving me nuts - I’m tracking down a bug in ActionPack that
causes rails to do a 500 error with no info, or when testing causes an
immediate exit with no information or stack trace.

I’ve tracked it down to ActionView::TemplateError, where numerous
things can happen that cause an exception to be thrown from w/o the
TemplateError. For instance, the following controller will cause the
error to happen:

class FooController < ApplicationController
def bad_file_with_full_path_false
render :file => “unknown”, :use_full_path => false
end
end

And I cannot get the exception to be caught in a test case, no matter
what I do. For example, w/i a standard FooControllerTest:

def test_bad_file_render_with_full_path_false
assert_raises ActionController::MissingTemplate do
get :bad_file_with_full_path_false
end
rescue Exception => e
flunk(“failed”)
end

This just exits - no flunk msg, assert_raise doesn’t catch, nothing.
I also commented out an “ensure” in ActionController::Base#process, to
make sure that wouldn’t halt the exception. It still exits.

I tracked it down into TemplateError via a lot of tedious logging, and
it seems the line here throws an error (though I cant figure out why,
it works in irb):

http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action_view/template_error.rb#L79

and this line can also throw an exception, which makes sense because
@file_name refers to a non-existent file:

http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action_view/template_error.rb#L29

And of course none of that answers why I can’t rescue the exception
back in my calling code, no matter what I do. Any ideas?

  • thanks,
  • Rob

On 8/5/06, Rob S. [email protected] wrote:

This just exits - no flunk msg, assert_raise doesn’t catch, nothing.

If it is raising the exception, you won’t reach that rescue –
#assert_raises rescues the exception internally. Perhaps you meant to
use #assert_nothing_raised ?


Regards,
John W.
http://johnwilger.com

On 8/5/06, John W. [email protected] wrote:

end

This just exits - no flunk msg, assert_raise doesn’t catch, nothing.

If it is raising the exception, you won’t reach that rescue –
#assert_raises rescues the exception internally. Perhaps you meant to
use #assert_nothing_raised ?


Regards,
John W.

John,
I realize assert_raises should rescue it, but then it should either
pass or fail based on the class of the exception. My main issue is
that the program just exits, with out success or failure for that test
class. So its basically as if it was calling exit (which its not).

Doing this will also show the same behavior:

def test_bad_file_render_with_full_path_false
get :bad_file_with_full_path_false
end

So my question is, what is happening inside TemplateError that it
causes the whole process to do a hard exit?

  • Rob

On 8/5/06, Rob S. [email protected] wrote:

I realize assert_raises should rescue it, but then it should either
pass or fail based on the class of the exception. My main issue is
that the program just exits, with out success or failure for that test
class. So its basically as if it was calling exit (which its not).

Ah, OK. I misunderstood and simply thought that you had a test that
was passing when you expected it to fail.

I tried out your example on my OSX system (ruby 1.8.4, compiled form
src). I had the same result both with the latest release of Rails and
after freezing to edge Rails. Not much help, I know – but it’s not
just you, FWIW.


Regards,
John W.
http://johnwilger.com

On 8/5/06, John W. [email protected] wrote:

src). I had the same result both with the latest release of Rails and
after freezing to edge Rails. Not much help, I know – but it’s not
just you, FWIW.

Thanks for confirming that I’m not going crazy =). I understand that
it should error out for a bad file (which I’m going to submit a patch
for), but I don’t understand what kind of error would cause it to just
crash like that w/o a “rescuable” exception.

  • rob

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs