From: Tomas Doran Date: Wed, 29 Sep 2010 01:34:03 +0000 (+0000) Subject: Patch from the mailing list to clarify view warning X-Git-Tag: 5.80029~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=4d723d120281581f8b040252e1b6324e2509b65b;hp=b7574be10423241f54ba7244cc9e84708460e997 Patch from the mailing list to clarify view warning --- diff --git a/Changes b/Changes index 331d18f..49023f4 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ # This file documents the revision history for Perl extension Catalyst. + - Add a warning when $c->view is called and cannot locate a default_view + or current_view. This clarifies the logging when ::RenderView gets + confused. + - Deal with Moose >= 1.15 warning if you add a method called 'meta' to a class which already has one by using _add_meta_method. diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index f72d3ce..ed1841c 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -750,7 +750,12 @@ sub view { unless ( ref($name) ) { # Direct component hash lookup to avoid costly regexps my $comps = $c->components; my $check = $appclass."::View::".$name; - return $c->_filter_component( $comps->{$check}, @args ) if exists $comps->{$check}; + if( exists $comps->{$check} ) { + return $c->_filter_component( $comps->{$check}, @args ); + } + else { + $c->log->warn( "Attempted to use view '$check', but does not exist" ); + } } my @result = $c->_comp_search_prefixes( $name, qw/View V/ ); return map { $c->_filter_component( $_, @args ) } @result if ref $name; diff --git a/t/aggregate/live_view_warnings.t b/t/aggregate/live_view_warnings.t new file mode 100644 index 0000000..1387c1b --- /dev/null +++ b/t/aggregate/live_view_warnings.t @@ -0,0 +1,23 @@ +#!perl + +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../lib"; + +use Test::More; +use Catalyst::Test 'TestAppViewWarnings'; + +if ( $ENV{CATALYST_SERVER} ) { + plan skip_all => 'Using remote server'; +} + +{ + ok( my $response = request('http://localhost/'), 'Request' ); + like($TestAppViewWarnings::log_messages[0], qr/Attempted to use view/s, 'View failure warning received'); + +} + +done_testing; + diff --git a/t/lib/TestAppViewWarnings.pm b/t/lib/TestAppViewWarnings.pm new file mode 100644 index 0000000..3a9102c --- /dev/null +++ b/t/lib/TestAppViewWarnings.pm @@ -0,0 +1,22 @@ +use strict; +use warnings; + +package TestAppViewWarnings; + +use Catalyst; + +our @log_messages; + +__PACKAGE__->config( name => 'TestAppWarnings', root => '/some/dir', default_view => "DoesNotExist" ); + +__PACKAGE__->log(TestAppViewWarnings::Log->new); + +__PACKAGE__->setup; + +package TestAppViewWarnings::Log; + +use base qw/Catalyst::Log/; +sub warn { push(@TestAppViewWarnings::log_messages, @_[1..$#_]); } + +1; + diff --git a/t/lib/TestAppViewWarnings/Controller/Root.pm b/t/lib/TestAppViewWarnings/Controller/Root.pm new file mode 100644 index 0000000..6d252f8 --- /dev/null +++ b/t/lib/TestAppViewWarnings/Controller/Root.pm @@ -0,0 +1,17 @@ +package TestAppViewWarnings::Controller::Root; +use strict; +use warnings; +use base 'Catalyst::Controller'; + +__PACKAGE__->config->{namespace} = ''; + +# Return log messages from previous request +sub index :Path Args() {} + +sub end : Action { + my ($self, $c) = @_; + $c->view; # Cause view lookup and ergo warning we are testing. + $c->res->body('foo'); +} + +1;