X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst.pm;h=0eee708fe9ff96ef772817c4c0266a1451930bda;hp=c07a1c7b23d80db511d5ee07cded5e55d73b02e7;hb=79070bc658ced79555a17ee2b4a84269c4d32de0;hpb=a74d53e34e33a6252905b25f93a8a7359e1fb5d1 diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index c07a1c7..0eee708 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -43,6 +43,10 @@ use Plack::Middleware::IIS7KeepAliveFix; use Plack::Middleware::LighttpdScriptNameFix; use Plack::Middleware::ContentLength; use Plack::Middleware::Head; +use Plack::Middleware::HTTPExceptions; +use Plack::Middleware::FixMissingBodyInRedirect; +use Plack::Middleware::MethodOverride; +use Plack::Middleware::RemoveRedundantBody; use Plack::Util; use Class::Load 'load_class'; @@ -122,7 +126,7 @@ __PACKAGE__->stats_class('Catalyst::Stats'); # Remember to update this in Catalyst::Runtime as well! -our $VERSION = '5.90059_002'; +our $VERSION = '5.90059_004'; sub import { my ( $class, @arguments ) = @_; @@ -1126,6 +1130,7 @@ sub setup { $class->setup_home( delete $flags->{home} ); + $class->setup_log( delete $flags->{log} ); $class->setup_plugins( delete $flags->{plugins} ); # Call plugins setup, this is stupid and evil. @@ -1136,7 +1141,6 @@ sub setup { $class->setup unless $Catalyst::__AM_RESTARTING; } - $class->setup_log( delete $flags->{log} ); $class->setup_middleware(); $class->setup_data_handlers(); $class->setup_dispatcher( delete $flags->{dispatcher} ); @@ -1892,11 +1896,32 @@ sub finalize_cookies { my $c = shift; $c->engine->finalize_cookies( $c, @_ ) } =head2 $c->finalize_error -Finalizes error. +Finalizes error. If there is only one error in L and it is an object that +does C or C we rethrow the error and presume it caught by middleware +up the ladder. Otherwise we return the debugging error page (in debug mode) or we +return the default error page (production mode). =cut -sub finalize_error { my $c = shift; $c->engine->finalize_error( $c, @_ ) } +sub finalize_error { + my $c = shift; + if($#{$c->error} > 0) { + $c->engine->finalize_error( $c, @_ ); + } else { + my ($error) = @{$c->error}; + if( + blessed $error && + ($error->can('as_psgi') || $error->can('code')) + ) { + # In the case where the error 'knows what it wants', becauses its PSGI + # aware, just rethow and let middleware catch it + $error->can('rethrow') ? $error->rethrow : croak $error; + croak $error; + } else { + $c->engine->finalize_error( $c, @_ ) + } + } +} =head2 $c->finalize_headers @@ -1916,36 +1941,11 @@ sub finalize_headers { if ( my $location = $response->redirect ) { $c->log->debug(qq/Redirecting to "$location"/) if $c->debug; $response->header( Location => $location ); - - if ( !$response->has_body ) { - # Add a default body if none is already present - my $encoded_location = encode_entities($location); - $response->body(<<"EOF"); - - - - Moved - - -

This item has moved here.

- - -EOF - $response->content_type('text/html; charset=utf-8'); - } } # Remove incorrectly added body and content related meta data when returning # an information response, or a response the is required to not include a body - if ( $response->status =~ /^(1\d\d|[23]04)$/ ) { - if($response->has_body) { - $c->log->debug('Removing body for informational or no content http responses'); - $response->body(''); - $response->headers->remove_header("Content-Length"); - } - } - $c->finalize_cookies; $c->response->finalize_headers(); @@ -2013,10 +2013,13 @@ sub handle_request { my $c = $class->prepare(@arguments); $c->dispatch; $status = $c->finalize; - } - catch { + } catch { chomp(my $error = $_); $class->log->error(qq/Caught exception in engine "$error"/); + #rethow if this can be handled by middleware + if(blessed $error && ($error->can('as_psgi') || $error->can('code'))) { + $error->can('rethrow') ? $error->rethrow : croak $error; + } }; $COUNT++; @@ -3105,7 +3108,11 @@ sub registered_middlewares { my $class = shift; if(my $middleware = $class->_psgi_middleware) { return ( + Plack::Middleware::HTTPExceptions->new, + Plack::Middleware::RemoveRedundantBody->new, + Plack::Middleware::FixMissingBodyInRedirect->new, Plack::Middleware::ContentLength->new, + Plack::Middleware::MethodOverride->new, Plack::Middleware::Head->new, @$middleware); } else { @@ -3597,6 +3604,23 @@ So the general form is: Where C<@middleware> is one or more of the following, applied in the REVERSE of the order listed (to make it function similarly to L: + +Alternatively, you may also define middleware by calling the L +package method: + + package MyApp::Web; + + use Catalyst; + + __PACKAGE__->setup_middleware( \@middleware_definitions); + __PACKAGE__->setup; + +In the case where you do both (use 'setup_middleware' and configuration) the +package call to setup_middleware will be applied earlier (in other words its +middleware will wrap closer to the application). Keep this in mind since in +some cases the order of middleware is important. + +The two approaches are not exclusive. =over 4 @@ -3911,6 +3935,8 @@ rainboxx: Matthias Dietrich, C dd070: Dhaval Dhanani +Upasana + =head1 COPYRIGHT Copyright (c) 2005, the above named PROJECT FOUNDER and CONTRIBUTORS.