Catalyst/View/Errors/JSON.pm

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