]> git.deb.at Git - deb/packages.git/commitdiff
Improve error handling:
authorFrank Lichtenheld <frank@lichtenheld.de>
Tue, 28 Nov 2006 02:25:17 +0000 (02:25 +0000)
committerFrank Lichtenheld <frank@lichtenheld.de>
Tue, 28 Nov 2006 02:25:17 +0000 (02:25 +0000)
 - Make it possible to control the returned HTTP code
 - Use always the html error template, we have no others anyway
 - Try to avoid mixing the apache generated and our own error
   messages (this needs more work)

cgi-bin/dispatcher.pl
lib/Packages/CGI.pm
lib/Packages/Template.pm

index b72e443615e5b0e8e2ad1cfe61f3048122f75ed2..d67f86d5687808c8df9199bf6cb84012eb9dacc3 100755 (executable)
@@ -58,6 +58,11 @@ if ($ARGV[0] && ($ARGV[0] eq 'php')) {
 } else {
        $input = new CGI;
 }
 } else {
        $input = new CGI;
 }
+my $cgi_error = $input->cgi_error;
+if ($cgi_error) {
+    fatal_error( "Error parsing the request", $cgi_error );
+}
+
 
 my $pet0 = new Benchmark;
 my $tet0 = new Benchmark;
 
 my $pet0 = new Benchmark;
 my $tet0 = new Benchmark;
@@ -253,6 +258,11 @@ debug( "Parameter evaluation took ".timestr($petd) ) if DEBUG;
 
 my $template = new Packages::Template( $TEMPLATEDIR, $opts{format}, { lang => $opts{lang}, charset => $charset, debug => ( DEBUG ? $opts{debug} : 0 ) }, ( $CACHEDIR ? { COMPILE_DIR => $CACHEDIR } : {} ) );
 
 
 my $template = new Packages::Template( $TEMPLATEDIR, $opts{format}, { lang => $opts{lang}, charset => $charset, debug => ( DEBUG ? $opts{debug} : 0 ) }, ( $CACHEDIR ? { COMPILE_DIR => $CACHEDIR } : {} ) );
 
+unless (-e "$TEMPLATEDIR/$opts{format}/${what_to_do}.tmpl") {
+    fatal_error( "requested format not available for this document",
+                "405 requested format not available");
+}
+
 my (%html_header, %page_content);
 unless (@Packages::CGI::fatal_errors) {
     no strict 'refs';
 my (%html_header, %page_content);
 unless (@Packages::CGI::fatal_errors) {
     no strict 'refs';
@@ -271,21 +281,23 @@ $page_content{uri_escape} = sub { return URI::Escape::uri_escape(@_) };
 $page_content{quotemeta} = sub { return quotemeta($_[0]) };
 $page_content{string2id} = sub { return &Packages::CGI::string2id(@_) };
 
 $page_content{quotemeta} = sub { return quotemeta($_[0]) };
 $page_content{string2id} = sub { return &Packages::CGI::string2id(@_) };
 
-print $input->header(-charset => $charset, -type => get_mime($opts{format}) );
-
-#use Data::Dumper;
-#print '<pre>'.Dumper(\%ENV, \%html_header, \%page_content, get_all_messages()).'</pre>';
-
 unless (@Packages::CGI::fatal_errors) {
 unless (@Packages::CGI::fatal_errors) {
-print $template->page( $what_to_do, { %page_content, %{ get_all_messages() } } );
+    print $input->header(-charset => $charset, -type => get_mime($opts{format}) );
+    #use Data::Dumper;
+    #print '<pre>'.Dumper(\%ENV, \%html_header, \%page_content, get_all_messages()).'</pre>';
+    print $template->page( $what_to_do, { %page_content, %{ get_all_messages() } } );
+    my $tet1 = new Benchmark;
+    my $tetd = timediff($tet1, $tet0);
+    print $template->trailer( undef, undef, undef, $tetd );
+} elsif ($Packages::CGI::http_code) {
+    print $input->header( -charset => $charset, -status => $Packages::CGI::http_code );
 } else {
 } else {
-print $template->error_page( get_all_messages() );
+    # We currently have only an error page in html
+    # so no format support here
+    print $input->header( -charset => $charset );
+    print $template->error_page( get_all_messages() );
+    print $template->trailer();;
 }
 
 }
 
-my $tet1 = new Benchmark;
-my $tetd = timediff($tet1, $tet0);
-
-my $trailer = $template->trailer( undef, undef, undef, $tetd );
-print $trailer;
 
 # vim: ts=8 sw=4
 
 # vim: ts=8 sw=4
index 34d7a5f341636414d0175e089561fd241c9c5823..555c9e534944f80ecc02bfc1efa360f0fdabc3f6 100644 (file)
@@ -20,6 +20,7 @@ my %mime_types = (
                  'txt.gz' => 'text/plain',
                  html => 'text/html',
                  rss => 'application/rss+xml',
                  'txt.gz' => 'text/plain',
                  html => 'text/html',
                  rss => 'application/rss+xml',
+                 rfc822 => 'text/plain',
                  );
 
 sub get_mime {
                  );
 
 sub get_mime {
@@ -27,13 +28,16 @@ sub get_mime {
 }
 
 our (@fatal_errors, @errors, @debug, @msgs, @hints, @notes);
 }
 
 our (@fatal_errors, @errors, @debug, @msgs, @hints, @notes);
+our $http_code;
 
 sub reset {
     @fatal_errors = @errors = @debug = @msgs = @hints = @notes = ();
 
 sub reset {
     @fatal_errors = @errors = @debug = @msgs = @hints = @notes = ();
+    $http_code = 200;
 }
 
 sub fatal_error {
     push @fatal_errors, $_[0];
 }
 
 sub fatal_error {
     push @fatal_errors, $_[0];
+    $http_code = $_[1] if $_[1];
 }
 sub error {
     push @errors, $_[0];
 }
 sub error {
     push @errors, $_[0];
index 9912cd704f2acbb023402e35e7dd7cdd07fc6e7a..7d85f46ba1cd53b1c3315bfc32525df0d89775c6 100644 (file)
@@ -71,7 +71,7 @@ sub error_page {
 #    warn Dumper($page_content);
 
     my $txt;
 #    warn Dumper($page_content);
 
     my $txt;
-    $self->process("$self->{format}/error.tmpl", $page_content, \$txt)
+    $self->process("html/error.tmpl", $page_content, \$txt)
        or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
 
     return $txt;
        or die sprintf( "template error: %s", $self->error ); # too late for reporting on-line
 
     return $txt;