5 B<CGI::Carp> - CGI routines for writing to the HTTPD (or other) error log
11 croak "We're outta here!";
12 confess "It was my fault: $!";
13 carp "It was your fault!";
19 CGI scripts have a nasty habit of leaving warning messages in the error
20 logs that are neither time stamped nor fully identified. Tracking down
21 the script that caused the error is a pain. This fixes that. Replace
30 And the standard warn(), die (), croak(), confess() and carp() calls
31 will automagically be replaced with functions that write out nicely
32 time-stamped messages to the HTTP server error log.
36 [Fri Nov 17 21:40:43 1995] test.pl: I'm confused at test.pl line 3.
37 [Fri Nov 17 21:40:43 1995] test.pl: Got an error message: Permission denied.
38 [Fri Nov 17 21:40:43 1995] test.pl: I'm dying.
40 =head1 REDIRECTING ERROR MESSAGES
42 By default, error messages are sent to STDERR. Most HTTPD servers
43 direct STDERR to the server's error log. Some applications may wish
44 to keep private error logs, distinct from the server's error log, or
45 they may wish to direct error messages to STDOUT so that the browser
48 The C<carpout()> function is provided for this purpose. Since
49 carpout() is not exported by default, you must import it explicitly by
52 use CGI::Carp qw(carpout);
54 The carpout() function requires one argument, which should be a
55 reference to an open filehandle for writing errors. It should be
56 called in a C<BEGIN> block at the top of the CGI application so that
57 compiler errors will be caught. Example:
60 use CGI::Carp qw(carpout);
61 open(LOG, ">>/usr/local/cgi-logs/mycgi-log") or
62 die("Unable to open mycgi-log: $!\n");
66 carpout() does not handle file locking on the log for you at this point.
68 The real STDERR is not closed -- it is moved to SAVEERR. Some
69 servers, when dealing with CGI scripts, close their connection to the
70 browser when the script closes STDOUT and STDERR. SAVEERR is used to
71 prevent this from happening prematurely.
73 You can pass filehandles to carpout() in a variety of ways. The "correct"
74 way according to Tom Christiansen is to pass a reference to a filehandle
79 This looks weird to mere mortals however, so the following syntaxes are
86 carpout(\'main::LOG');
90 FileHandle and other objects work as well.
92 Use of carpout() is not great for performance, so it is recommended
93 for debugging purposes or for moderate-use applications. A future
94 version of this module may delay redirecting STDERR until one of the
95 CGI::Carp methods is called to prevent the performance hit.
97 =head1 MAKING PERL ERRORS APPEAR IN THE BROWSER WINDOW
99 If you want to send fatal (die, confess) errors to the browser, ask to
100 import the special "fatalsToBrowser" subroutine:
102 use CGI::Carp qw(fatalsToBrowser);
103 die "Bad error here";
105 Fatal errors will now be echoed to the browser as well as to the log. CGI::Carp
106 arranges to send a minimal HTTP header to the browser so that even errors that
107 occur in the early compile phase will be seen.
108 Nonfatal errors will still be directed to the log file only (unless redirected
111 =head2 Changing the default message
113 By default, the software error message is followed by a note to
114 contact the Webmaster by e-mail with the time and date of the error.
115 If this message is not to your liking, you can change it using the
116 set_message() routine. This is not imported by default; you should
117 import it on the use() line:
119 use CGI::Carp qw(fatalsToBrowser set_message);
120 set_message("It's not a bug, it's a feature!");
122 You may also pass in a code reference in order to create a custom
123 error message. At run time, your code will be called with the text
124 of the error message that caused the script to die. Example:
126 use CGI::Carp qw(fatalsToBrowser set_message);
130 print "<h1>Oh gosh</h1>";
131 print "Got an error: $msg";
133 set_message(\&handle_errors);
136 In order to correctly intercept compile-time errors, you should call
137 set_message() from within a BEGIN{} block.
141 1.05 carpout() added and minor corrections by Marc Hedlund
142 <hedlund@best.com> on 11/26/95.
144 1.06 fatalsToBrowser() no longer aborts for fatal errors within
147 1.08 set_message() added and carpout() expanded to allow for FileHandle
150 1.09 set_message() now allows users to pass a code REFERENCE for
151 really custom error messages. croak and carp are now
152 exported by default. Thanks to Gunther Birznieks for the
155 1.10 Patch from Chris Dean (ctdean@cogit.com) to allow
156 module to run correctly under mod_perl.
160 Lincoln D. Stein <lstein@genome.wi.mit.edu>. Feel free to redistribute
161 this under the Perl Artistic License.
166 Carp, CGI::Base, CGI::BasePlus, CGI::Request, CGI::MiniSvr, CGI::Form,
176 @EXPORT = qw(confess croak carp);
177 @EXPORT_OK = qw(carpout fatalsToBrowser wrap set_message);
179 $main::SIG{__WARN__}=\&CGI::Carp::warn;
180 $main::SIG{__DIE__}=\&CGI::Carp::die;
181 $CGI::Carp::VERSION = '1.10';
182 $CGI::Carp::CUSTOM_MSG = undef;
184 # fancy import routine detects and handles 'errorWrap' specially.
188 grep($routines{$_}++,@_,@EXPORT);
189 $WRAP++ if $routines{'fatalsToBrowser'} || $routines{'wrap'};
190 my($oldlevel) = $Exporter::ExportLevel;
191 $Exporter::ExportLevel = 1;
192 Exporter::import($pkg,keys %routines);
193 $Exporter::ExportLevel = $oldlevel;
196 # These are the originals
197 sub realwarn { warn(@_); }
198 sub realdie { die(@_); }
202 my($pack,$file,$line,$sub) = caller($level);
203 my($id) = $file=~m|([^/]+)$|;
204 return ($file,$line,$id);
208 my $time = scalar(localtime);
210 my ($id,$pack,$file);
213 ($pack,$file) = caller($frame++);
215 ($id) = $id=~m|([^/]+)$|;
216 return "[$time] $id: ";
221 my($file,$line,$id) = id(1);
222 $message .= " at $file line $line.\n" unless $message=~/\n$/;
224 $message=~s/^/$stamp/gm;
228 # The mod_perl package Apache::Registry loads CGI programs by calling
229 # eval. These evals don't count when looking at the stack backtrace.
231 my $message = Carp::longmess();
232 my $mod_perl = ($ENV{'GATEWAY_INTERFACE'}
233 && $ENV{'GATEWAY_INTERFACE'} =~ /^CGI-Perl\//);
234 $message =~ s,eval[^\n]+Apache/Registry\.pm.*,,s if $mod_perl;
240 my $time = scalar(localtime);
241 my($file,$line,$id) = id(1);
242 $message .= " at $file line $line.\n" unless $message=~/\n$/;
243 &fatalsToBrowser($message) if $WRAP && _longmess() !~ /eval [{\']/m;
245 $message=~s/^/$stamp/gm;
250 $CGI::Carp::CUSTOM_MSG = shift;
251 return $CGI::Carp::CUSTOM_MSG;
254 # Avoid generating "subroutine redefined" warnings with the following
259 sub confess { CGI::Carp::die Carp::longmess \@_; }
260 sub croak { CGI::Carp::die Carp::shortmess \@_; }
261 sub carp { CGI::Carp::warn Carp::shortmess \@_; }
266 # We have to be ready to accept a filehandle as a reference
270 my($no) = fileno(to_filehandle($in));
271 die "Invalid filehandle $in\n" unless defined $no;
273 open(SAVEERR, ">&STDERR");
274 open(STDERR, ">&$no") or
275 ( print SAVEERR "Unable to redirect STDERR: $!\n" and exit(1) );
279 sub fatalsToBrowser {
285 my($wm) = $ENV{SERVER_ADMIN} ?
286 qq[the webmaster (<a href="mailto:$ENV{SERVER_ADMIN}">$ENV{SERVER_ADMIN}</a>)] :
287 "this site's webmaster";
288 my ($outer_message) = <<END;
289 For help, please send mail to $wm, giving this error message
290 and the time and date of the error.
293 print STDOUT "Content-type: text/html\n\n";
296 if (ref($CUSTOM_MSG) eq 'CODE') {
297 &$CUSTOM_MSG($msg); # nicer to perl 5.003 users
300 $outer_message = $CUSTOM_MSG;
305 <H1>Software error:</H1>
313 # Cut and paste from CGI.pm so that we don't have the overhead of
314 # always loading the entire CGI module.
317 return undef unless $thingy;
318 return $thingy if UNIVERSAL::isa($thingy,'GLOB');
319 return $thingy if UNIVERSAL::isa($thingy,'FileHandle');
322 while (my $package = caller($caller++)) {
323 my($tmp) = $thingy=~/[\':]/ ? $thingy : "$package\:\:$thingy";
324 return $tmp if defined(fileno($tmp));