Better error message if $c->forward fails to find action or component
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Dispatcher.pm
index af4f7ce..ca6c0ee 100644 (file)
@@ -104,7 +104,7 @@ sub dispatch {
     }
 }
 
-=item $c->forward($command [args]))
+=item $c->forward( $command [, \@arguments ] )
 
 Forward processing to a private action or a method from a class.
 If you define a class without method it will default to process().
@@ -130,15 +130,7 @@ sub forward {
 
     my $caller    = caller(0);
     my $namespace = '/';
-    my $args;
-
-    if ( ref( $_[0] ) eq 'ARRAY' ) {
-        $args=$c->req->args();
-        $c->req->args( shift );
-    } elsif ( ref( $_[1] ) eq 'ARRAY' ) {
-        $args=$c->req->args();
-        $c->req->args( $_[1] );
-    }
+    my $arguments = ( ref( $_[-1] ) eq 'ARRAY' ) ? pop(@_) : $c->req->args;
 
     if ( $command =~ /^\// ) {
         $command =~ /^\/(.*)\/(\w+)$/;
@@ -156,36 +148,24 @@ sub forward {
     my $results = $c->get_action( $command, $namespace );
 
     unless ( @{$results} ) {
-        my $class = $command || '';
-        my $path = $class . '.pm';
-        $path =~ s/::/\//g;
-
-        unless ( $INC{$path} ) {
-            my $error =
-              qq/Couldn't forward to "$class". Invalid or not loaded./;
-            $c->error($error);
-            $c->log->debug($error) if $c->debug;
-            return 0;
-        }
-
-        unless ( UNIVERSAL::isa( $class, 'Catalyst::Base' ) ) {
-            my $error =
-              qq/Can't forward to "$class". Class is not a Catalyst component./;
+        
+        unless ( $c->components->{$command} ) {
+            my $error = qq/Couldn't forward to command "$command". Invalid action or component./;
             $c->error($error);
             $c->log->debug($error) if $c->debug;
             return 0;
         }
-
+        
+        my $class  = $command;
         my $method = shift || 'process';
 
-        if ( my $code = $class->can($method) ) {
+        if ( my $code = $c->components->{$class}->can($method) ) {
             $c->actions->{reverse}->{"$code"} = "$class->$method";
             $results = [ [ [ $class, $code ] ] ];
         }
 
         else {
-            my $error =
-              qq/Couldn't forward to "$class". Does not implement "$method"/;
+            my $error = qq/Couldn't forward to "$class". Does not implement "$method"/;
             $c->error($error);
             $c->log->debug($error)
               if $c->debug;
@@ -193,15 +173,15 @@ sub forward {
         }
 
     }
+    
+    local $c->request->{arguments} = [ @{ $arguments } ];
 
     for my $result ( @{$results} ) {
         $c->execute( @{ $result->[0] } );
-        last if scalar @{ $c->error };
+        return if scalar @{ $c->error };
         last unless $c->state;
     }
-    $c->req->args( $args ) if $args;
 
-    return if scalar @{ $c->error };
     return $c->state;
 }