From: Florian Ragwitz Date: Sun, 10 Jan 2010 06:31:16 +0000 (+0000) Subject: Improve handling errors during prepare. X-Git-Tag: 5.89000~56 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=3640641e13deb51a427e7c0ecf13ade30bfa6db7;hp=0bfda603359d0f9f36a5b13856ab025fc8bd8abe Improve handling errors during prepare. --- diff --git a/Makefile.PL b/Makefile.PL index 96c59b4..1f9fb0b 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -44,6 +44,7 @@ requires 'Text::SimpleTable' => '0.03'; requires 'Time::HiRes'; requires 'Tree::Simple' => '1.15'; requires 'Tree::Simple::Visitor::FindByPath'; +requires 'Try::Tiny'; requires 'URI' => '1.35'; requires 'Task::Weaken'; requires 'Text::Balanced'; # core in 5.8.x but mentioned for completeness diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 329a76f..9c8d588 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -30,6 +30,7 @@ use List::MoreUtils qw/uniq/; use attributes; use utf8; use Carp qw/croak carp shortmess/; +use Try::Tiny; BEGIN { require 5.008004; } @@ -1847,7 +1848,7 @@ sub handle_request { # Always expect worst case! my $status = -1; - eval { + try { if ($class->debug) { my $secs = time - $START || 1; my $av = sprintf '%.3f', $COUNT / $secs; @@ -1858,12 +1859,11 @@ sub handle_request { my $c = $class->prepare(@arguments); $c->dispatch; $status = $c->finalize; - }; - - if ( my $error = $@ ) { - chomp $error; - $class->log->error(qq/Caught exception in engine "$error"/); } + catch { + chomp(my $error = $_); + $class->log->error(qq/Caught exception in engine "$error"/); + }; $COUNT++; @@ -1900,27 +1900,38 @@ sub prepare { $c->res->headers->header( 'X-Catalyst' => $Catalyst::VERSION ); } - # Allow engine to direct the prepare flow (for POE) - if ( my $prepare = $c->engine->can('prepare') ) { - $c->engine->$prepare( $c, @arguments ); - } - else { - $c->prepare_request(@arguments); - $c->prepare_connection; - $c->prepare_query_parameters; - $c->prepare_headers; - $c->prepare_cookies; - $c->prepare_path; - - # Prepare the body for reading, either by prepare_body - # or the user, if they are using $c->read - $c->prepare_read; - - # Parse the body unless the user wants it on-demand - unless ( ref($c)->config->{parse_on_demand} ) { - $c->prepare_body; + try { + # Allow engine to direct the prepare flow (for POE) + if ( my $prepare = $c->engine->can('prepare') ) { + $c->engine->$prepare( $c, @arguments ); + } + else { + $c->prepare_request(@arguments); + $c->prepare_connection; + $c->prepare_query_parameters; + $c->prepare_headers; + $c->prepare_cookies; + $c->prepare_path; + + # Prepare the body for reading, either by prepare_body + # or the user, if they are using $c->read + $c->prepare_read; + + # Parse the body unless the user wants it on-demand + unless ( ref($c)->config->{parse_on_demand} ) { + $c->prepare_body; + } } } + # VERY ugly and probably shouldn't rely on ->finalize actually working + catch { + # failed prepare is always due to an invalid request, right? + $c->response->status(400); + $c->response->content_type('text/plain'); + $c->response->body('Bad Request'); + $c->finalize; + die $_; + }; my $method = $c->req->method || ''; my $path = $c->req->path;