You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Rich Duzenbury <th...@theduz.com> on 2002/08/06 18:42:08 UTC

CGI::Carp not working

Hi,

I'm very new to mod_perl, and I've run into this problem on my very first 
script.  CGI::Carp will not return the error message to the browser.

The following perl script:

#!/usr/bin/perl -w
use CGI qw(:all);
use CGI::Carp qw/fatalsToBrowser/;

print header, start_html, p("CGI::Carp Version: $CGI::Carp::VERSION");
print map { "$_ = $ENV{$_}<br>\n" } sort (keys (%ENV));
print end_html;

if (defined(param('die'))) {
   die "A horrible death";
}

produces:

CGI::Carp Version: 1.23
DOCUMENT_ROOT = /www/theduz.com/www
GATEWAY_INTERFACE = CGI-Perl/1.1
HTTP_ACCEPT = application/vnd.ms-excel, application/msword, 
application/vnd.ms-powerpoint, image/gif, image/x-xbitmap, image/jpeg, 
image/pjpeg, */*
HTTP_ACCEPT_ENCODING = gzip, deflate
HTTP_ACCEPT_LANGUAGE = en-us
HTTP_CONNECTION = Keep-Alive
HTTP_HOST = theduz.com
HTTP_USER_AGENT = Mozilla/4.0 (compatible; MSIE 6.0; Windows 98)
MOD_PERL = mod_perl/1.27
PATH = /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin
QUERY_STRING = die=1
REMOTE_ADDR = 216.248.85.102
REMOTE_PORT = 3531
REQUEST_METHOD = GET
REQUEST_URI = /cgi-bin/die.mpl?die=1
SCRIPT_FILENAME = /www/theduz.com/www/cgi-bin/die.mpl
SCRIPT_NAME = /cgi-bin/die.mpl
SERVER_ADDR = 216.248.85.110
SERVER_ADMIN = webmaster@theduz.com
SERVER_NAME = theduz.com
SERVER_PORT = 80
SERVER_PROTOCOL = HTTP/1.1
SERVER_SIGNATURE =
Apache/1.3.26 Server at theduz.com Port 80

SERVER_SOFTWARE = Apache/1.3.26 (Unix) mod_perl/1.27 mod_ssl/2.8.10 
OpenSSL/0.9.6d
OK
The server encountered an internal error or misconfiguration and was unable 
to complete your request.
Please contact the server administrator, webmaster@theduz.com and inform 
them of the time the error occurred, and anything you might have done that 
may have caused the error.
More information about this error may be available in the server error log.

Apache/1.3.26 Server at theduz.com Port 80



What is with the 'OK' message? under plain CGI, the error response is:

Software error:
A horrible death at /www/theduz.com/www/cgi-bin/die.pl line 10.
For help, please send mail to the webmaster (webmaster@theduz.com), giving 
this error message and the time and date of the error.

How can I convince mod_perl to return the error to the browser (and the 
log)?  Is there a way to debug the mod_perl CGI::Carp interaction?

Thank you.


Regards,
Rich


Re: CGI::Carp not working

Posted by Rich Duzenbury <rd...@theduz.com>.
Ahhh, this lead to an answer, thank you very much.  A further search of the 
archive regarding the ineval routine found this:

 > Changing a line in CGI::Carp
 > < sub ineval { $^S || _longmess() =~ /eval [\{\']/m }
 > --- >
 > sub ineval { _longmess() =~ /eval [\{\']/m }

It doesn't seem to have anything to do with whether any content has been 
sent or not, but rather due to a bug in CGI::Carp.

When I have more time, I'll also investigate the other methods you 
suggested, and I thank you for the pointers.

Regards,
Rich

At 02:46 PM 8/6/02, you wrote:

>well, ok... but the result is exactly what I said before - you're sending 
>content over the wire with print() (which starts an OK response) then 
>dying in the middle of sending that content.  this is a no-no in mod_perl.


Re: CGI::Carp not working

Posted by Geoffrey Young <ge...@modperlcookbook.org>.

Rich Duzenbury wrote:

> Sorry, perhaps I was not clear enough.
> 
> I'm not getting a 'double set of headers'.


well, ok... but the result is exactly what I said before - you're 
sending content over the wire with print() (which starts an OK 
response) then dying in the middle of sending that content.  this is a 
no-no in mod_perl.

[snip]
> 
> The mod_perl version does _NOT_ print the proper error message per the 
> fatalsToBrowser directive to CGI::Carp.


that depends upon your definition of proper :)  remember 
Apache::Registry is not mod_cgi - it sacrificies certain things (bad 
coding practices and somewhat different behaviors) for other things 
(speed).


 
> So, how can I debug this?


I found a number of archive postings that deal with this issue - it 
seems I was mistaken and CGI::Carp hasn't behaved with mod_perl in a 
while.  sorry.  see (for example):

http://marc.theaimsgroup.com/?t=95605822900002&r=1&w=2

as for debugging scripts _without_ CGI::Carp, I personally use tail -f 
and a bunch of print statements :)  but there are a few perl tricks 
you can use, some of which can be found (or are linked to) here

http://perl.apache.org/docs/1.0/guide/snippets.html#Redirecting_Errors_to_the_Client_Instead_of_error_log

I've also used something similar to this

http://www.modperlcookbook.org/code/ch04/Cookbook/ErrorsToBrowser.pm

which can be set up like this:

ErrorDocument 500 /show-errors
<Location /show-errors>
   SetHandler perl-script
   PerlHandler Cookbook::ErrorsToBrowser
   PerlSetVar ErrorLines 25
</Location>

HTH

--Geoff






Re: CGI::Carp not working

Posted by Rich Duzenbury <rd...@theduz.com>.
Sorry, perhaps I was not clear enough.

I'm not getting a 'double set of headers'.

Here is a sample.

#!/usr/bin/perl -w
use strict;
use CGI qw(:all);
use CGI::Carp qw(fatalsToBrowser);
print header, start_html('We are dying');
die 'A horrible death';
print end_html; # we won't see this one...

I've installed the above code in two locations, once so it will run as a 
cgi, once so it will run as a mod_perl script:
http://theduz.com/cgi-bin/dead.pl (under cgi)
http://theduz.com/cgi-bin/dead.mpl (under mod_perl)

One piece of code, run once under mod_perl, run once under plain CGI do not 
produce the same results.

The mod_perl version does _NOT_ print the proper error message per the 
fatalsToBrowser directive to CGI::Carp.

This code under CGI produces (This is the desired behavior):
Software error:
A horrible death at /www/theduz.com/www/cgi-bin/die.mpl line 6.
-----------------------------------------------------------------

Under mod_perl, simply:
OK
The server encountered an internal error or misconfiguration and was unable 
to complete your request.
Please contact the server administrator, webmaster@theduz.com and inform 
them of the time the error occurred, and anything you might have done that 
may have caused the error.
More information about this error may be available in the server error log.
------------------------------------------------

When I execute 'dead.mpl', this appears in the server error log:
[Tue Aug  6 13:38:52 2002] [error] A horrible death at 
/www/theduz.com/www/cgi-bin/dead.mpl line 6.

So, how can I debug this?

Thank you.

At 01:01 PM 8/6/02, Geoffrey Young wrote:

>you sent information to the client before you called die().
>
>regular CGI forks a new process, sees what happens to it and, if 
>everything is ok, prints the results to the client.  with mod_perl you're 
>directly calling the Apache API that sends content to the browser with 
>every print(), so those initial print() statements go directly to the 
>client and are not filtered on the way out.
>
>>Software error:
>>A horrible death at /www/theduz.com/www/cgi-bin/die.pl line 10.
>>For help, please send mail to the webmaster (webmaster@theduz.com), 
>>giving this error message and the time and date of the error.
>>How can I convince mod_perl to return the error to the browser (and the 
>>log)?  Is there a way to debug the mod_perl CGI::Carp interaction?
>
>
>CGI::Carp will still work, but you have to change your logic somewhat - do 
>all of your error checking, data validation, and whatnot prior to printing 
>to the client.  until you send data over the wire you can die() and things 
>will behave as you expect.  once you write to the client, all bets are off 
>and you'll see the double set of headers like you did.
>
>capturing errors prior to sending data to the client is discussed in the 
>Eagle book (www.modperl.com) somewhere close to the beginning, IIRC.
>


Re: CGI::Carp not working

Posted by Geoffrey Young <ge...@modperlcookbook.org>.

Rich Duzenbury wrote:

> Hi,
> 
> I'm very new to mod_perl, and I've run into this problem on my very 
> first script.  CGI::Carp will not return the error message to the browser.
> 
> The following perl script:
> 
> #!/usr/bin/perl -w
> use CGI qw(:all);
> use CGI::Carp qw/fatalsToBrowser/;
> 
> print header, start_html, p("CGI::Carp Version: $CGI::Carp::VERSION");
> print map { "$_ = $ENV{$_}<br>\n" } sort (keys (%ENV));
> print end_html;
> 
> if (defined(param('die'))) {
>   die "A horrible death";
> }
> 


[snip]



> What is with the 'OK' message? under plain CGI, the error response is:


you sent information to the client before you called die().

regular CGI forks a new process, sees what happens to it and, if 
everything is ok, prints the results to the client.  with mod_perl 
you're directly calling the Apache API that sends content to the 
browser with every print(), so those initial print() statements go 
directly to the client and are not filtered on the way out.

> 
> Software error:
> A horrible death at /www/theduz.com/www/cgi-bin/die.pl line 10.
> For help, please send mail to the webmaster (webmaster@theduz.com), 
> giving this error message and the time and date of the error.
> 
> How can I convince mod_perl to return the error to the browser (and the 
> log)?  Is there a way to debug the mod_perl CGI::Carp interaction?


CGI::Carp will still work, but you have to change your logic somewhat 
- do all of your error checking, data validation, and whatnot prior to 
printing to the client.  until you send data over the wire you can 
die() and things will behave as you expect.  once you write to the 
client, all bets are off and you'll see the double set of headers like 
you did.

capturing errors prior to sending data to the client is discussed in 
the Eagle book (www.modperl.com) somewhere close to the beginning, IIRC.


HTH


--Geoff