v1.21
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Serialize / View.pm
1 package Catalyst::Action::Serialize::View;
2
3 use Moose;
4 use namespace::autoclean;
5
6 extends 'Catalyst::Action';
7
8 sub execute {
9     my $self = shift;
10     my ( $controller, $c, $view ) = @_;
11
12     # Views don't care / are not going to render an entity for 3XX
13     # responses.
14     return 1 if $c->response->status =~ /^(?:204|3\d\d)$/;
15
16     my $stash_key = (
17             $controller->{'serialize'} ?
18                 $controller->{'serialize'}->{'stash_key'} :
19                 $controller->{'stash_key'} 
20         ) || 'rest';
21
22     if ( !$c->view($view) ) {
23         $c->log->error("Could not load $view, refusing to serialize");
24         return;
25     }
26
27     if ($c->view($view)->process($c, $stash_key)) {
28       return 1;
29     } else {
30       # This is stupid. Please improve it.
31       my $error = join("\n", @{ $c->error }) || "Error in $view";
32       $error .= "\n";
33       $c->clear_errors;
34       die $error;
35     }
36 }
37
38 __PACKAGE__->meta->make_immutable;
39
40 1;