Catalyst/Plugin/Errors.pm

filter: Statementstmt 28.4% 29 / 102 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 33.3% 6 / 18 Podpod 42.8% 3 / 7 totaltotal 20.8% 38 / 182 CC 24 SCAR 39.6
CC
24
Cov
19%
dispatch_error
156.0
setup
20.0
setup_components
20.0
CRAP 52.3
↑ Summary CatalystX/Utils/DoesHttpException.pm »
1 package Catalyst::Plugin::Errors;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MRO::Compat;
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 1 ▶use Catalyst::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our %DEFAULT_ERROR_VIEWS = (
11 'text/html' => 'Errors::HTML',
12 'text/plain' => 'Errors::Text',
13 'application/json' => 'Errors::JSON',
14 );
15  
16 1 my %views = %DEFAULT_ERROR_VIEWS;
17 1 my @accepted = ();
18 1 my $default_media_type = 'text/plain';
19 1 my $default_language = 'en_US';
20  
21 0 ▶my $profile = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
22 0 my $c = shift;
23 0 ▶ $c->stats->profile(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
24 if $c->debug;
25 1 };
26  
27 0 ▶my $available_languages = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 0 my ($c) = @_;
29 0 return my @lang_tags = CatalystX::Utils::ErrorMessages::available_languages;
30 1 };
31  
32 0 ▶my $get_language = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
33 0 my ($c) = @_;
34 0 ▶ if(my $lang = $c->request->header('Accept-Language')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lang = $c->request->header("Accept-Language")) 0 0 0
35 0 ▶ return CatalystX::Utils::ContentNegotiation::content_negotiator->choose_language([$c->$available_languages], $lang) || $default_language;
Condition0%0/3
CatalystX::Utils::ContentNegotiation::content_negotiator()->choose_language([$c->$available_languages], $lang) || $default_language
l!l&&r!l&&!r
000
MC/DC0%0/2
CatalystX::Utils::ContentNegotiation::content_negotiator()->choose_language([$c->$available_languages], $lang) || $default_language
CatalystX::Utils::ContentNegotiation::content_negotiator()->choose_language([$c->$available_languages], $lang) $default_language
Truth table0%0/3
A || B
A= CatalystX::Utils::ContentNegotiation::content_negotiator()->choose_language([$c->$available_languages], $lang)B= $default_language
ABresult
1X1
011
000
36 }
37 0 return $default_language;
38 1 };
39  
40 0 ▶my $get_message_info = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
41 0 my ($c, $lang, $code) = @_;
42 0 return my $message_info_hash = CatalystX::Utils::ErrorMessages::get_message_info($lang, $code);
43 1 };
44  
45 0 ▶my $finalize_message_info = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
46 0 my ($c, $code, $lang, %args) = @_;
47 0 my $message_info = $c->$get_message_info($lang, $code);
48 0 ▶ $message_info = $c->$get_message_info('en_US', $code) unless $message_info; # Fallback to US English
Branch: 1 branch
BranchTrueFalseTotal
unless $message_info 0 0 0
49 return (
50 0 %$message_info,
51 lang => $lang,
52 %args,
53 );
54 1 };
55  
56 0 ▶sub generate_error_template_name {
Subroutine generate_error_template_name: not called CC=1 SCAR=6.9
Pod undocumented
57 0 my ($c, $code, %args) = @_;
58 0 return 'http_error';
59 }
60  
61 0 ▶sub finalize_error_args {
Subroutine finalize_error_args: not called CC=1 SCAR=6.9
Pod documented
62 0 my ($c, $code, %args) = @_;
63 0 my $lang = $c->$get_language;
64 0 my %message_info = $c->$finalize_message_info($code, $lang, %args);
65 return (
66 0 status_code => $code,
67 0 uri => "@{[ $c->req->uri ]}",
68 %message_info );
69 }
70  
71 0 ▶sub looks_like_http_error_obj {
Subroutine looks_like_http_error_obj: not called CC=3 SCAR=24.8
Pod undocumented
72 0 my ($self, $obj) = @_;
73 0 ▶ return Scalar::Util::blessed($obj) && $obj->can('as_http_response') ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($obj) && $obj->can('as_http_response') ? : 0 0 0
Condition0%0/3
builtin::blessed($obj) and $obj->can("as_http_response")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($obj) and $obj->can("as_http_response")
builtin::blessed($obj) $obj->can("as_http_response")
Truth table0%0/3
A and B
A= builtin::blessed($obj)B= $obj->can("as_http_response")
ABresult
0X0
100
111
74 }
75  
76 0 ▶sub setup {
Subroutine setup: not called CC=4 SCAR=30.0
Pod undocumented
77 0 my $app = shift;
78 0 my $ret = $app->maybe::next::method(@_);
79 0 my $config = $app->config->{'Plugin::Errors'};
80  
81 0 ▶ %views = (%views, %{$config->{views}}) if $config->{views};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"views"} 0 0 0
82 0 ▶ $default_media_type = $config->{default_media_type} if exists $config->{default_media_type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{"default_media_type"} 0 0 0
83 0 ▶ $default_language = $config->{default_language} if exists $config->{default_language};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{"default_language"} 0 0 0
84  
85 0 @accepted = keys %views;
86  
87 0 return $ret;
88 }
89  
90 0 ▶sub setup_components {
Subroutine setup_components: not called CC=4 SCAR=30.0
Pod undocumented
91 0 my ($app, @args) = @_;
92 0 my $ret = $app->maybe::next::method(@_);
93  
94 0 my $namespace = "${app}::View";
95 0 my %views_we_have = map { Catalyst::Utils::class2classsuffix($_) => 1 }
96 0 grep { m/$namespace/ }
97 0 keys %{ $app->components };
98  
99 0 foreach my $view_needed (values %views) {
100 0 ▶ next if $views_we_have{"View::${view_needed}"};
Branch: 1 branch
BranchTrueFalseTotal
if $views_we_have{"View::$view_needed"} 0 0 0
101 0 ▶ $app->log->debug("Injecting Catalyst::View::${view_needed}") if $app->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $app->debug 0 0 0
102 0 Catalyst::Utils::ensure_class_loaded("Catalyst::View::${view_needed}");
103 0 Catalyst::Utils::inject_component(
104 into => $app,
105 component => "Catalyst::View::${view_needed}",
106 as => $view_needed );
107 }
108  
109 0 return $ret;
110 }
111  
112 0 ▶sub dispatch_error {
Subroutine dispatch_error: not called CC=12 SCAR=50.5
Pod documented
113 0 my ($c, $code, @args) = @_;
114 0 my (@additional_headers, %data) = ();
115  
116 0 ▶ @additional_headers = @{shift(@args)} if (ref($args[0])||'') eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] || "") eq "ARRAY" 0 0 0
Condition0%0/2
ref $args[0] || ''
l!l
00
MC/DC0%0/1
ref $args[0] || ''
ref $args[0]
Truth table0%0/2
A
A= ref $args[0]
Aresult
11
00
117 0 ▶ while(@additional_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@additional_headers) 0 0 0
118 0 $c->response->headers->header(shift(@additional_headers), shift(@additional_headers));
119 }
120  
121 0 ▶ %data = %{shift(@args)} if (ref($args[0])||'') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] || "") eq "HASH" 0 0 0
Condition0%0/2
ref $args[0] || ''
l!l
00
MC/DC0%0/1
ref $args[0] || ''
ref $args[0]
Truth table0%0/2
A
A= ref $args[0]
Aresult
11
00
122 0 %data = $c->finalize_error_args($code, %data);
123  
124 0 ▶ my $chosen_media_type = CatalystX::Utils::ContentNegotiation::content_negotiator
Condition0%0/3
CatalystX::Utils::ContentNegotiation::content_negotiator()->choose_media_type(\@accepted, $c->request->header('Accept')) || $default_media_type
l!l&&r!l&&!r
000
MC/DC0%0/2
CatalystX::Utils::ContentNegotiation::content_negotiator()->choose_media_type(\@accepted, $c->request->header('Accept')) || $default_media_type
CatalystX::Utils::ContentNegotiation::content_negotiator()->choose_media_type(\@accepted, $c->request->header('Accept')) $default_media_type
Truth table0%0/3
A || B
A= CatalystX::Utils::ContentNegotiation::content_negotiator()->choose_media_type(\@accepted, $c->request->header('Accept'))B= $default_media_type
ABresult
1X1
011
000
125 ->choose_media_type(\@accepted, $c->request->header('Accept'))
126 || $default_media_type;
127  
128 0 ▶ $c->log->debug("Error dispatched to mediatype '$chosen_media_type' using view '$views{$chosen_media_type}'") if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
129 0 ▶ $c->log->error($data{error}) if exists($data{error});
Branch: 1 branch
BranchTrueFalseTotal
if exists $data{"error"} 0 0 0
130  
131 0 my $chosen_view = $views{$chosen_media_type};
132 0 my $view_class = ref($c) . "::View::${chosen_view}";
133 0 ▶ my $view_obj = $view_class->can('ACCEPT_CONTEXT') ?
Branch: 1 branch
BranchTrueFalseTotal
$view_class->can('ACCEPT_CONTEXT') ? : 0 0 0
134 $c->view($chosen_view, %data) :
135 $c->view($chosen_view);
136  
137 0 ▶ if(my $sub = $view_obj->can("http_${code}")) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $sub = $view_obj->can("http_$code")) { } 0 0 0
elsif ($view_obj->can('http_default')) { } 0 0 0
138 0 $c->$profile(begin => "=> View::${chosen_view}");
139 0 $view_obj->$sub($c, %data);
140 0 $c->$profile(end => "=> View::${chosen_view}");
141 } elsif($view_obj->can('http_default')) {
142 0 $c->$profile(begin => "=> View::${chosen_view}");
143 0 $view_obj->http_default($c, $code, %data);
144 0 $c->$profile(end => "=> View::${chosen_view}");
145 } else {
146 0 my $template = $c->generate_error_template_name($code, %data);
147 0 $c->stash(template=>$template, %data);
148 0 $c->forward($view_obj);
149 }
150 }
151  
152 0 ▶sub detach_error {
Subroutine detach_error: not called CC=1 SCAR=6.9
Pod documented
153 0 my $c = shift;
154 0 $c->dispatch_error(@_);
155 0 $c->detach;
156 }
157  
158 1 __PACKAGE__->meta->make_immutable;
159  
160 =head1 NAME
161  
162 Catalyst::Plugin::Errors - Standard error responses with content negotiation
163  
164 =head1 SYNOPSIS
165  
166 Use in your application class
167  
168 package Example;
169  
170 use Catalyst;
171  
172 __PACKAGE__->setup_plugins([qw/Errors/]);
173 __PACKAGE__->setup();
174 __PACKAGE__->meta->make_immutable();
175  
176 And then you can use it in a controller (or anyplace where you have C<$c> context).
177  
178 package Example::Controller::Root;
179  
180 use Moose;
181 use MooseX::MethodAttributes;
182  
183 extends 'Catalyst::Controller';
184  
185 sub root :Chained(/) PathPart('') CaptureArgs(0) {}
186  
187 sub not_found :Chained(root) PathPart('') Args {
188 my ($self, $c, @args) = @_;
189 $c->detach_error(404);
190 }
191  
192 __PACKAGE__->config(namespace=>'');
193 __PACKAGE__->meta->make_immutable;
194  
195 =head1 DESCRIPTION
196  
197 This is a plugin which installs (if needed) View classes to handle HTTP errors (4xx
198 and 5xx codes) in a regular and content negotiated way. See <CatalystX::Errors>
199 for a high level overview. Documentation here is more API level and the examples
200 are sparse.
201  
202 =head1 METHODS
203  
204 This plugin adds the following methods to your C<$c> context.
205  
206 =head2 dispatch_error ($code, ?\@additional_headers, ?\%template_args)
207  
208 Examples:
209  
210 $c->detach_error(404);
211 $c->detach_error(404, +{error=>'invalid uri request'});
212 $c->detach_error(401, ['WWW-Authenticate" => 'Basic realm=myapp, charset="UTF-8"'], +{error=>'unauthorized access attempt'});
213  
214 Dispatches to an error view based on content negotiation and the provided code. You can also pass
215 an arrayref of extra HTTP headers (such as www-authenticate for 401 errors) and also optionally
216 a hashref of fields that will be sent to the view.
217  
218 When dispatching to a C<$view> we use the following rules in order:
219  
220 First if the View has a method C<http_${code}> (where C<$code> is the HTTP status code you are
221 using for the error) we call that method with args C<$c, %template_args> and expect that method to setup
222 a valid error response.
223  
224 Second, call the method C<http_default> with args C<$c, $code, %template_args> if that exists.
225  
226 If neither method exists we call C<$c->forward($view)> and C<%template_args> are added to the stash, along
227 with a stash var 'template' which is set to 'http_error'. This should work with most standard L<Catalyst>
228 views that look at the stash field 'template' to find a template name. If you prefer a different template
229 name you can override the method 'generate_error_template_name' to make it whatever you wish.
230  
231 B<NOTE> Using C<dispatch_error> (or C<detach_error>) doesn't add anything to the Catalyst error log
232 as we consider this control flow more than anything else. If you want to log a special line you can
233 add an C<error> field to C<%template_args> and that we go to the error log.
234  
235 =head2 detach_error
236  
237 Calls L</dispatch_error> with the provided arguments and then does a C<$c->detach> which
238 effectively ends processing for the action.
239  
240 =head1 CONFIGURATION & CUSTOMIZATION
241  
242 This plugin can be customized with the following configuration options or via
243 overriding or adapting the following methods
244  
245 =head2 finalize_error_args
246  
247 This method provides the actual arguments given to the error view (args which are for
248 example used in the template for messaging to the end user). You can override this
249 to provide your own version. See the source for how this should work
250  
251 =head2 Configuration keys
252  
253 This plugin defines the following configuration by default, which you can override.
254  
255 package Example;
256  
257 use Catalyst;
258  
259 __PACKAGE__->setup_plugins([qw/Errors/]);
260 __PACKAGE__->config(
261 # This is the configuration which is default. You don't have to actually type
262 # this out. I'm just putting it here to show you what its doing under the hood.
263 'Plugin::Errors' => +{
264 default_media_type => 'text/plain',
265 default_language => 'en_US',
266 views => +{
267 'text/html' => 'Errors::HTML',
268 'text/plain' => 'Errors::Text',
269 'application/json' => 'Errors::JSON',
270 },
271 },
272 );
273  
274 __PACKAGE__->setup();
275 __PACKAGE__->meta->make_immutable();
276  
277 By default we map the media types C<text/html>, C<text/plain> and C<application/json> to
278 cooresponding views. This views are injected automatically if you don't provide subclasses
279 or your own view locally. The following views are injected as needed:
280  
281 L<Catalyst::View::Error::HTML>, L<Catalyst::View::Error::Text>, and L<L<Catalyst::View::Error::JSON>.
282  
283 You can check the docs for each of the default views for customization options but you can always
284 make a local subclass inside you application's view directory and tweak as desired (or you can just
285 use your own view or one of the common ones on CPAN).
286  
287 You can also add additional media types mappings.
288  
289 =head1 SEE ALSO
290
291 L<CatalystX::Errors>.
292  
293 =head1 AUTHOR
294
295 L<CatalystX::Errors>.
296
297 =head1 COPYRIGHT & LICENSE
298
299 L<CatalystX::Errors>.
300  
301 =cut
↑ Summary CatalystX/Utils/DoesHttpException.pm »