3

次のコードを使用して、ブラウザへの致命的なエラーの出力をテストしています。

use CGI;
use CGI::Carp qw(fatalsToBrowser);

die "test";

ブラウザにエラーが表示されることを期待していますが、エラーはなく、通常の 500 応答が返されます。リモート リクエスト用のカスタム エラー ページがオンになっていることを忘れていましたScript failed to send data.

また:

> perl -w index.pl
Status: 500
Content-type: text/html

<h1>Software error:</h1>
<pre>test at index.pl line 4.</pre>
<p>
For help, please send mail to this site's webmaster, giving this error message and the time and date of the error.

</p>
[Mon Feb  8 18:29:52 2010] index.pl: test at index.pl line 4.
4

1 に答える 1

4

何よりも前に、数行の新しい行を印刷してみてください。「CGI標準」が言うように、これはHTTPヘッダーの終わりをサーバーに通知します。または、次のようなものを使用できる場合があります: ( Carp の man ページからコピーしたもの):

use CGI::Carp qw(set_die_handler);
BEGIN {
   sub handle_errors {
      my $msg = shift;
      print "content-type: text/html\n\n";
      print "<h1>Oh gosh</h1>";
      print "<p>Got an error: $msg</p>";
  }
  set_die_handler(\&handle_errors);
}

ここでうまくいかない場合は、さらにいくつかのトリックがあります。

#!perl
BEGIN { $| = 1; print "\n\n" }
open STDERR, ">&STDOUT";

..そして、この Perl Journal の記事でさらにいくつかのトリックを紹介します。

于 2010-02-07T22:48:33.517 に答える