File Coverage

blib/lib/Catalyst/View/Errors/JSON.pm
Criterion Covered Total %
statement 12 22 54.5
branch n/a
condition n/a
subroutine 4 7 57.1
pod 1 3 33.3
total 17 32 53.1


line stmt bran cond sub pod time code
1              
2             use Moose;
3 1     1   1388 use JSON::MaybeXS;
  1         2  
  1         6  
4 1     1   5536 use CatalystX::Utils::ContentNegotiation;
  1         1  
  1         65  
5 1     1   416 use CatalystX::Utils::ErrorMessages;
  1         2  
  1         23  
6 1     1   368  
  1         3  
  1         296  
7             extends 'Catalyst::View';
8              
9             has extra_encoder_args => (is=>'ro', required=>1, default => sub { +{} });
10              
11             has encoder => (
12             is => 'ro',
13             init_arg => undef,
14             required => 1,
15             lazy => 1,
16             default => sub { JSON::MaybeXS->new(utf8=>1, %{shift->extra_encoder_args}) },
17             );
18              
19             my ($self, $c, $code, %args) = @_;
20             my $message_info = $self->finalize_message_info($c, $code, %args);
21 0     0 0   my $json = $self->render_json($c, $message_info);
22 0          
23 0           $c->response->body($json);
24             $c->response->content_type('application/json');
25 0           $c->response->status($code);
26 0           }
27 0            
28             my ($self, $c, $code, %args) = @_;
29             return +{
30             info => {
31 0     0 1   lang => delete($args{status_code}),
32             uri => delete($args{uri}),
33             },
34             error => [
35             {
36             status_code => $code,
37             title => delete($args{title}),
38             message => delete($args{message}),
39             },
40             ],
41 0           };
42             }
43              
44              
45             my ($self, $c, $message_info) = @_;
46             return $self->encoder->encode($message_info);
47             }
48              
49 0     0 0   __PACKAGE__->meta->make_immutable;
50 0            
51             =head1 NAME
52              
53             Catalyst::View::Errors::JSON - Standard HTTP Errors Responses in JSON
54              
55             =head1 SYNOPSIS
56              
57             package MyApp::View::JSON;
58              
59             use Moose;
60             extends 'Catalyst::View::Errors::JSON';
61              
62             __PACKAGE__->meta->make_immutable;
63              
64             =head1 DESCRIPTION
65              
66             Used to generate a JSON error response in standard way.
67              
68             =head1 METHODS
69              
70             This view exposes the follow methods for public use or for a programmer to override
71             to change function.
72              
73             =head2 finalize_message_info
74              
75             Finalizes the hash of data that is sent to the template handler to make the body of the error
76             response. You can override if you want to change or add to this data.
77              
78             By default you get an error message that looks like this:
79              
80             {
81             "info" : {
82             "lang" : "en_US",
83             "uri" : "http://localhost:5000/"
84             },
85             "errors" : [
86             {
87             "title" : "Resource not found",
88             "messge" : "The requested resource could not be found but may be available again in the future.",
89             "status_code" : 404
90             }
91             ]
92             }
93              
94             If you're passing extra template args you'll need to override this method to handle things as you
95             wish.
96              
97             =head1 CONFIGURATION
98              
99             This View exposes the following configuration options
100              
101             =head2 extra_encoder_args
102              
103             Extra args used to initialize the L<JSON::MaybeXS> object.
104              
105             =head2 default_language
106              
107             When doing content negotiation if there's no language preferred by the client
108             use this language. Default is C<en_US>.
109              
110             =head1 SEE ALSO
111            
112             L<CatalystX::Errors>.
113              
114             =head1 AUTHOR
115            
116             L<CatalystX::Errors>.
117            
118             =head1 COPYRIGHT & LICENSE
119            
120             L<CatalystX::Errors>.
121              
122             =cut