Patch from the mailing list to clarify view warning
Tomas Doran [Wed, 29 Sep 2010 01:34:03 +0000 (01:34 +0000)]
Changes
lib/Catalyst.pm
t/aggregate/live_view_warnings.t [new file with mode: 0644]
t/lib/TestAppViewWarnings.pm [new file with mode: 0644]
t/lib/TestAppViewWarnings/Controller/Root.pm [new file with mode: 0644]

diff --git a/Changes b/Changes
index 331d18f..49023f4 100644 (file)
--- 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.
 
index f72d3ce..ed1841c 100644 (file)
@@ -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 (file)
index 0000000..1387c1b
--- /dev/null
@@ -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 (file)
index 0000000..3a9102c
--- /dev/null
@@ -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 (file)
index 0000000..6d252f8
--- /dev/null
@@ -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;