Handle "no type" in messages better - attempt to reply saying so.
[catagits/Catalyst-Engine-STOMP.git] / lib / Catalyst / Controller / MessageDriven.pm
index 9e8d0d3..1969bfd 100644 (file)
@@ -60,27 +60,33 @@ sub end : Private {
 
        # Engine will send our reply based on the value of this header.
        $c->response->headers->header( 'X-Reply-Address' => $c->stash->{request}->{reply_to} );
+       
+       # The wire response
+       my $output;
+       
+       # Load a serializer
+       my $serializer = $self->config->{serializer};
+       my $s = Data::Serializer->new( serializer => $serializer );
 
        # Custom error handler - steal errors from catalyst and dump them into
        # the stash, to get them serialized out as the reply.
        if (scalar @{$c->error}) {
                my $error = join "\n", @{$c->error};
                $c->stash->{response} = { status => 'ERROR', error => $error };
-               $c->error(0); # clear errors, so our response isn't clobbered
+               $output = $s->serialize( $c->stash->{response} );
+               $c->clear_errors;
+               $c->response->status(400);
        }
 
        # Serialize the response
-       my $output;
-       my $serializer = $self->config->{serializer};
-       my $s = Data::Serializer->new( serializer => $serializer );
        eval {
                $output = $s->raw_serialize( $c->stash->{response} );
        };
        if ($@) {
                my $error = "exception in serialize: $@";
-               $c->error($error);
                $c->stash->{response} = { status => 'ERROR', error => $error };
-               $output = Dump( $c->stash->{response} );
+               $output = $s->serialize( $c->stash->{response} );
+               $c->response->status(400);
        }
 
        $c->response->output( $output );
@@ -92,7 +98,12 @@ sub default : Private {
        # Forward the request to the appropriate action, based on the
        # message type.
        my $action = $c->stash->{request}->{type};
-       $c->forward($action, [$c->stash->{request}]);
+       if (defined $action) {
+               $c->forward($action, [$c->stash->{request}]);
+       }
+       else {
+               $c->error('no message type specified');
+       }
 }
 
 __PACKAGE__->meta->make_immutable;