[Catalyst-commits] r12595 - in Catalyst-Runtime/5.80/branches/psgi: . lib

rafl at dev.catalyst.perl.org rafl at dev.catalyst.perl.org
Sun Jan 10 06:31:19 GMT 2010


Author: rafl
Date: 2010-01-10 06:31:16 +0000 (Sun, 10 Jan 2010)
New Revision: 12595

Modified:
   Catalyst-Runtime/5.80/branches/psgi/Makefile.PL
   Catalyst-Runtime/5.80/branches/psgi/lib/Catalyst.pm
Log:
Improve handling errors during prepare.

Modified: Catalyst-Runtime/5.80/branches/psgi/Makefile.PL
===================================================================
--- Catalyst-Runtime/5.80/branches/psgi/Makefile.PL	2010-01-10 06:31:03 UTC (rev 12594)
+++ Catalyst-Runtime/5.80/branches/psgi/Makefile.PL	2010-01-10 06:31:16 UTC (rev 12595)
@@ -44,6 +44,7 @@
 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

Modified: Catalyst-Runtime/5.80/branches/psgi/lib/Catalyst.pm
===================================================================
--- Catalyst-Runtime/5.80/branches/psgi/lib/Catalyst.pm	2010-01-10 06:31:03 UTC (rev 12594)
+++ Catalyst-Runtime/5.80/branches/psgi/lib/Catalyst.pm	2010-01-10 06:31:16 UTC (rev 12595)
@@ -30,6 +30,7 @@
 use attributes;
 use utf8;
 use Carp qw/croak carp shortmess/;
+use Try::Tiny;
 
 BEGIN { require 5.008004; }
 
@@ -1847,7 +1848,7 @@
 
     # Always expect worst case!
     my $status = -1;
-    eval {
+    try {
         if ($class->debug) {
             my $secs = time - $START || 1;
             my $av = sprintf '%.3f', $COUNT / $secs;
@@ -1858,13 +1859,12 @@
         my $c = $class->prepare(@arguments);
         $c->dispatch;
         $status = $c->finalize;
+    }
+    catch {
+        chomp(my $error = $_);
+        $class->log->error(qq/Caught exception in engine "$error"/);
     };
 
-    if ( my $error = $@ ) {
-        chomp $error;
-        $class->log->error(qq/Caught exception in engine "$error"/);
-    }
-
     $COUNT++;
 
     if(my $coderef = $class->log->can('_flush')){
@@ -1900,27 +1900,38 @@
         $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;
+    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;
+            # 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;
+            # 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;




More information about the Catalyst-commits mailing list