why can't I catch this exception?

R

Rob Sanheim

(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
 
J

John Wilger

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.

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 Wilger
http://johnwilger.com

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland
 
R

Rob Sanheim

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 ?

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
 
J

John Wilger

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 Wilger
http://johnwilger.com

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland
 
R

Rob Sanheim

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top