Rescue Failing in CGI Script

I have a Ruby 1.8.7 CGI script. Practically speaking, the whole thing
is included within a begin-rescue block. The script is structured such
that if all goes well the content of the desired web page is passed as a
block to cgi.out causing it to be sent to standard out with an
appropriate header. That all works as expected.

There is also a rescue-end block. This block passes (as another block)
the content of an error page to cgi.out. The theory is that when an
exception is raised control will be transferred to the rescue-end block
and and the error page will be displayed. I have done this before and
it has worked as expected for some (but not all) errors. For example,
an error that typically gets caught and sent to the rescue block is a
divide-by-zero error. Other errors result in the typical Apache error
page.

For some reason, in my current lashup (as described above) no errors get
caught. If I through in a divide-by-zero (e.g. x=1/0) I get an Apache
error and not the error page that is in my rescue block.

Being a cgi script this is difficult to debug and I’m afraid that
posting code would be a nightmare. So, what I’m seeking is suggestions
on how I might debug this. Any thoughts?

Thanks for any input.

  ... doug

Make sure that the actual work is not done in some threads.

Make sure you wrapped the entire thing in the begin-rescue block.

Make sure you use “rescue Exception” (plain “rescue” only catches
StandardError).

Have you tried somehow running the code as a standalone and seeing in
the exceptions are catched?

– Matma R.

Thanks for the input. I tried to follow your suggestions; but, it’s
still misbehaving. So, I would like to make sure that I fully
understand your suggestions.

Make sure you wrapped the entire thing in the begin-rescue block.

I’m not exactly sure what you mean by “entire thing”. What I am saying
is that begin-rescue-end blocks exist. When I introduce the error
(either by using raise or by using my pet of divide-by-zero) I introduce
it WITHIN the begin-rescue block. That should be sufficient, right?

Make sure you use “rescue Exception” (plain “rescue” only catches
StandardError).

Understood. I do use the Exception argument in this case. In some of
my earlier cases I used rescue with no arguments which may explain why
some exceptions were caught and some were not (in those earlier cases).

Make sure that the actual work is not done in some threads.

I’ve never written a multi-threaded script. My limited understanding
would be that if there were multiple threads I would have had to have
created them with Thread.new. There are no calls to Thread.new in this
entire script.

Have you tried somehow running the code as a standalone and seeing in
the exceptions are catched?

No, I haven’t. I definitely agree that that could be very revealing.
To get that job done I would have to somehow produce equivalent
stand-alone code that would reproduce the problem. I’m thinking that
that might be a very tall order. I’m concerned that I won’t be able to
develop equivalent stand-alone code that reproduces the problem. If I
can’t get this fixed some other way, I may have to give that a try. I
certainly hope that it doesn’t come to that.

Thanks again for the input. If you have any further thoughts, I’d love
to hear them.

     ... doug

Hi,

Have you checked if there’s a problem within the rescue clause? If
another exception happens there, it won’t be rescued and will lead to
the standard error page.

You could try to wrap the outer “begin-end” block in another block and
do something simple in its rescue clause (output a simple string, write
text to a file or whatever).

Have you checked if there’s a problem within the rescue clause? If
another exception happens there, it won’t be rescued and will lead to
the standard error page.

You could try to wrap the outer “begin-end” block in another block and
do something simple in its rescue clause (output a simple string, write
text to a file or whatever).

Now that is absolutely an awesome suggestion. It is exactly the type of
thing that I am looking for. Unfortunately, it appears that that is not
the problem. I have replaced the content of the rescue block with the
following line of code:

puts(“Content-type: text/html\n\nRescue
Test

Error

”)

The rescue block is still not being called when a divide-by-0 error is
introduced. I am, however, leaving the above substitution in place
until this issue is finally resolved just in case there is also a
problem with the actual content of my rescue block in ADDITION to
whatever other problem there is.

Thanks so much. It was indeed an awesome suggestion.

    ... doug

Well, if you still don’t get the custom error, then the exception is
rescued before it reaches the outer rescue block.

I see only two ways to find it out: Either you post the whole script so
we can actually look at it, or you debug it yourself by using a debugger
or by commenting code out.

or by commenting code out.

Using the suggested technique, I was finally able to isolate and solve
the problem. As I am sure you suspected, it was a stupid error on my
part. I had the begin-rescue-end blocks outside the method instead of
inside as it should have been. I don’t want to talk about it. I’m
embarrassed. As I say, it was a stupid mistake.

Thanks for the help.

... doug

On Tue, Jul 10, 2012 at 03:49:30AM +0900, Doug J. wrote:

or by commenting code out.

Using the suggested technique, I was finally able to isolate and solve
the problem. As I am sure you suspected, it was a stupid error on my
part. I had the begin-rescue-end blocks outside the method instead of
inside as it should have been. I don’t want to talk about it. I’m
embarrassed. As I say, it was a stupid mistake.

It happens to the best of us – and I know this, because I’m acquainted
with some “best”, and it has happened to them. Me too, but I’m not one
of those “best”.