documentation patch (edited by jnap)
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
index 8184a98..b085a6c 100644 (file)
@@ -81,6 +81,8 @@ sub _build_request_constructor_args {
 
 sub composed_request_class {
   my $class = shift;
+  return $class->_composed_request_class if $class->_composed_request_class;
+
   my @traits = (@{$class->request_class_traits||[]}, @{$class->config->{request_class_traits}||[]});
 
   # For each trait listed, figure out what the namespace is.  First we try the $trait
@@ -92,8 +94,14 @@ sub composed_request_class {
     Class::Load::load_first_existing_class($_, $class.'::'.$trait_ns.'::'. $_, 'Catalyst::'.$trait_ns.'::'.$_)
   } @traits;
 
-  return $class->_composed_request_class ||
-    $class->_composed_request_class(Moose::Util::with_traits($class->request_class, @normalized_traits));
+  if ($class->debug && scalar(@normalized_traits)) {
+      my $column_width = Catalyst::Utils::term_width() - 6;
+      my $t = Text::SimpleTable->new($column_width);
+      $t->row($_) for @normalized_traits;
+      $class->log->debug( "Composed Request Class Traits:\n" . $t->draw . "\n" );
+  }
+
+  return $class->_composed_request_class(Moose::Util::with_traits($class->request_class, @normalized_traits));
 }
 
 has response => (
@@ -115,6 +123,8 @@ sub _build_response_constructor_args {
 
 sub composed_response_class {
   my $class = shift;
+  return $class->_composed_response_class if $class->_composed_response_class;
+  
   my @traits = (@{$class->response_class_traits||[]}, @{$class->config->{response_class_traits}||[]});
 
   my $trait_ns = 'TraitFor::Response';
@@ -122,8 +132,14 @@ sub composed_response_class {
     Class::Load::load_first_existing_class($_, $class.'::'.$trait_ns.'::'. $_, 'Catalyst::'.$trait_ns.'::'.$_)
   } @traits;
 
-  return $class->_composed_response_class ||
-    $class->_composed_response_class(Moose::Util::with_traits($class->response_class, @normalized_traits));
+  if ($class->debug && scalar(@normalized_traits)) {
+      my $column_width = Catalyst::Utils::term_width() - 6;
+      my $t = Text::SimpleTable->new($column_width);
+      $t->row($_) for @normalized_traits;
+      $class->log->debug( "Composed Response Class Traits:\n" . $t->draw . "\n" );
+  }
+
+  return $class->_composed_response_class(Moose::Util::with_traits($class->response_class, @normalized_traits));
 }
 
 has namespace => (is => 'rw');
@@ -166,6 +182,8 @@ __PACKAGE__->stats_class('Catalyst::Stats');
 
 sub composed_stats_class {
   my $class = shift;
+  return $class->_composed_stats_class if $class->_composed_stats_class;
+
   my @traits = (@{$class->stats_class_traits||[]}, @{$class->config->{stats_class_traits}||[]});
 
   my $trait_ns = 'TraitFor::Stats';
@@ -173,14 +191,20 @@ sub composed_stats_class {
     Class::Load::load_first_existing_class($_, $class.'::'.$trait_ns.'::'. $_, 'Catalyst::'.$trait_ns.'::'.$_)
   } @traits;
 
-  return $class->_composed_stats_class ||
-    $class->_composed_stats_class(Moose::Util::with_traits($class->stats_class, @normalized_traits));
+  if ($class->debug && scalar(@normalized_traits)) {
+      my $column_width = Catalyst::Utils::term_width() - 6;
+      my $t = Text::SimpleTable->new($column_width);
+      $t->row($_) for @normalized_traits;
+      $class->log->debug( "Composed Stats Class Traits:\n" . $t->draw . "\n" );
+  }
+
+  return $class->_composed_stats_class(Moose::Util::with_traits($class->stats_class, @normalized_traits));
 }
 
 __PACKAGE__->_encode_check(Encode::FB_CROAK | Encode::LEAVE_SRC);
 
 # Remember to update this in Catalyst::Runtime as well!
-our $VERSION = '5.90104';
+our $VERSION = '5.90106';
 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
 
 sub import {
@@ -1473,6 +1497,11 @@ EOF
         $class->log->warn("This setting is deprecated and planned to be removed in Catalyst 5.81.");
     }
 
+    # call these so we pre setup the composed classes
+    $class->composed_request_class;
+    $class->composed_response_class;
+    $class->composed_stats_class;
+
     $class->setup_finalize;
 
     # Flush the log for good measure (in case something turned off 'autoflush' early)
@@ -2437,9 +2466,6 @@ sub prepare {
     # 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');
         # Note we call finalize and then die here, which escapes
         # finalize being called in the enclosing block..
         # It in fact couldn't be called, as we don't return $c..
@@ -2447,8 +2473,20 @@ sub prepare {
         # breaking compat for people doing crazy things (we should set
         # the 400 and just return the ctx here IMO, letting finalize get called
         # above...
-        $c->finalize;
-        die $_;
+        if ( $c->_handle_http_exception($_) ) {
+            foreach my $err (@{$c->error}) {
+                $c->log->error($err);
+            }
+            $c->clear_errors;
+            $c->log->_flush if $c->log->can('_flush');
+            $_->can('rethrow') ? $_->rethrow : croak $_;
+        } else {
+            $c->response->status(400);
+            $c->response->content_type('text/plain');
+            $c->response->body('Bad Request');
+            $c->finalize;
+            die $_;
+        }
     };
 
     $c->log_request;
@@ -3535,14 +3573,31 @@ sub setup_encoding {
 
 Hook to let you customize how encoding errors are handled.  By default
 we just throw an exception.  Receives a hashref of debug information.
-Example:
+Example of call:
 
     $c->handle_unicode_encoding_exception({
         param_value => $value,
         error_msg => $_,
-            encoding_step => 'params',
+        encoding_step => 'params',
         });
 
+You can override this for custom handling of unicode errors.  If you want a
+custom response here, one approach is to throw an HTTP style exception:
+
+    sub handle_unicode_encoding_exception {
+      my ($c, $params) = @_;
+      HTTP::Exception::BAD_REQUEST->throw(status_message=>$params->{error_msg});
+    }
+
+Alternatively you can 'catch' the error, stash it and write handling code later
+in your application:
+
+    sub handle_unicode_encoding_exception {
+      my ($c, $params) = @_;
+      $c->stash(BAD_UNICODE_DATA=>$params);
+      return 1;
+    }
+
 =cut
 
 sub handle_unicode_encoding_exception {