From: Frank Lichtenheld Date: Tue, 28 Nov 2006 02:25:17 +0000 (+0000) Subject: Improve error handling: X-Git-Url: https://git.deb.at/w?a=commitdiff_plain;h=c40b1514a9289273df1ae785fd938b8fa0f31183;p=deb%2Fpackages.git Improve error handling: - 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) --- diff --git a/cgi-bin/dispatcher.pl b/cgi-bin/dispatcher.pl index b72e443..d67f86d 100755 --- a/cgi-bin/dispatcher.pl +++ b/cgi-bin/dispatcher.pl @@ -58,6 +58,11 @@ if ($ARGV[0] && ($ARGV[0] eq 'php')) { } 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; @@ -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 } : {} ) ); +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'; @@ -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(@_) }; -print $input->header(-charset => $charset, -type => get_mime($opts{format}) ); - -#use Data::Dumper; -#print '
'.Dumper(\%ENV, \%html_header, \%page_content, get_all_messages()).'
'; - 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 '
'.Dumper(\%ENV, \%html_header, \%page_content, get_all_messages()).'
'; + 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 { -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 diff --git a/lib/Packages/CGI.pm b/lib/Packages/CGI.pm index 34d7a5f..555c9e5 100644 --- a/lib/Packages/CGI.pm +++ b/lib/Packages/CGI.pm @@ -20,6 +20,7 @@ my %mime_types = ( 'txt.gz' => 'text/plain', html => 'text/html', rss => 'application/rss+xml', + rfc822 => 'text/plain', ); sub get_mime { @@ -27,13 +28,16 @@ sub get_mime { } our (@fatal_errors, @errors, @debug, @msgs, @hints, @notes); +our $http_code; sub reset { @fatal_errors = @errors = @debug = @msgs = @hints = @notes = (); + $http_code = 200; } sub fatal_error { push @fatal_errors, $_[0]; + $http_code = $_[1] if $_[1]; } sub error { push @errors, $_[0]; diff --git a/lib/Packages/Template.pm b/lib/Packages/Template.pm index 9912cd7..7d85f46 100644 --- a/lib/Packages/Template.pm +++ b/lib/Packages/Template.pm @@ -71,7 +71,7 @@ sub error_page { # 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;