Throwable/Factory.pm

filter: Statementstmt 100% 52 / 52 Branchbran 66.6% 8 / 12 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 15 / 15 Podpod 66.6% 6 / 9 totaltotal 90.4% 85 / 94 SCAR 23.1
CC
10
Cov
93%
class_for
4.0
throw
3.1
roles_for_ident
3.1
CRAP 10.0
« Throwable/Role/Status/ImATeapot.pm ↑ Summary Throwable.pm »
1 package HTTP::Throwable::Factory;
2 5 our $AUTHORITY = 'cpan:STEVAN';
3 5 $HTTP::Throwable::Factory::VERSION = '0.027';
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use HTTP::Throwable::Variant;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use Sub::Exporter::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 use Sub::Exporter -setup => {
11 exports => [
12 http_throw => Sub::Exporter::Util::curry_method('throw'),
13 http_exception => Sub::Exporter::Util::curry_method('new_exception'),
14 ],
15 5 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Module::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 sub throw {
20 57 ▶ my $factory = shift;
Subroutine throw: called CC=3 SCAR=11.4
Pod undocumented
21 57 ▶ my $ident = (! ref $_[0]) ? shift(@_) : undef;
Branch: 1 branch
BranchTrueFalseTotal
!ref($_[0]) ? : 57 0 57
22 57 ▶ my $arg = shift || {};
Condition50%1/2
shift() || {}
l!l
570
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
23  
24 57 $factory->class_for($ident, $arg)->throw($arg);
25 }
26  
27 sub new_exception {
28 3 ▶ my $factory = shift;
Subroutine new_exception: called CC=3 SCAR=11.0
Pod undocumented
29 3 ▶ my $ident = (! ref $_[0]) ? shift(@_) : undef;
Branch: 1 branch
BranchTrueFalseTotal
!ref($_[0]) ? : 3 0 3
30 3 ▶ my $arg = shift || {};
Condition100%2/2
shift() || {}
l!l
21
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
31  
32 3 $factory->class_for($ident, $arg)->new($arg);
33 }
34  
35 sub core_roles {
36 60 ▶ return qw(
Subroutine core_roles: called CC=1 SCAR=0.0
Pod documented
37 HTTP::Throwable
38 );
39 }
40  
41 sub extra_roles {
42 33 ▶ return qw(
Subroutine extra_roles: called CC=1 SCAR=0.0
Pod documented
43 HTTP::Throwable::Role::TextBody
44 );
45 }
46  
47 sub roles_for_ident {
48 58 ▶ my ($self, $ident) = @_;
Subroutine roles_for_ident: called CC=3 SCAR=11.4
Pod documented
49  
50 58 ▶ Carp::confess("roles_for_ident called with undefined ident")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ident 0 58 58
51 unless defined $ident;
52  
53 58 ▶ Carp::confess("roles_for_ident called with empty ident string")
Branch: 1 branch
BranchTrueFalseTotal
unless length $ident 0 58 58
54 unless length $ident;
55  
56 58 return "HTTP::Throwable::Role::Status::$ident";
57 }
58  
59 sub roles_for_no_ident {
60 2 ▶ my ($self, $ident) = @_;
Subroutine roles_for_no_ident: called CC=1 SCAR=0.0
Pod documented
61  
62 2 return qw(
63 HTTP::Throwable::Role::Generic
64 HTTP::Throwable::Role::BoringText
65 );
66 }
67  
68 60 ▶sub base_class { () }
Subroutine base_class: called CC=1 SCAR=0.0
Pod documented
69  
70 sub class_for {
71 60 ▶ my ($self, $ident) = @_;
Subroutine class_for: called CC=4 SCAR=13.9
Pod undocumented
72  
73 60 my @roles;
74 60 ▶ if (defined $ident) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ident) { } 58 2 60
75 58 ▶ if ($ident =~ /\A[0-9]{3}\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ident =~ /\A[0-9]{3}\z/) { } 4 54 58
76 4 @roles = $self->roles_for_status_code($ident);
77 } else {
78 54 @roles = $self->roles_for_ident($ident);
79 }
80 } else {
81 2 @roles = $self->roles_for_no_ident;
82 }
83  
84 60 Module::Runtime::use_module($_) for @roles;
85  
86 60 my $class = HTTP::Throwable::Variant->build_variant(
87 superclasses => [ $self->base_class ],
88 roles => [
89 $self->core_roles,
90 $self->extra_roles,
91 @roles
92 ],
93 );
94  
95 60 return $class;
96 }
97  
98 5 my %lookup = (
99 300 => 'MultipleChoices',
100 301 => 'MovedPermanently',
101 302 => 'Found',
102 303 => 'SeeOther',
103 304 => 'NotModified',
104 305 => 'UseProxy',
105 307 => 'TemporaryRedirect',
106  
107 400 => 'BadRequest',
108 401 => 'Unauthorized',
109 403 => 'Forbidden',
110 404 => 'NotFound',
111 405 => 'MethodNotAllowed',
112 406 => 'NotAcceptable',
113 407 => 'ProxyAuthenticationRequired',
114 408 => 'RequestTimeout',
115 409 => 'Conflict',
116 410 => 'Gone',
117 411 => 'LengthRequired',
118 412 => 'PreconditionFailed',
119 413 => 'RequestEntityTooLarge',
120 414 => 'RequestURITooLong',
121 415 => 'UnsupportedMediaType',
122 416 => 'RequestedRangeNotSatisfiable',
123 417 => 'ExpectationFailed',
124 418 => 'ImATeapot',
125  
126 500 => 'InternalServerError',
127 501 => 'NotImplemented',
128 502 => 'BadGateway',
129 503 => 'Status::ServiceUnavailable',
130 504 => 'GatewayTimeout',
131 505 => 'HTTPVersionNotSupported',
132 );
133  
134 sub roles_for_status_code {
135 4 ▶ my ($self, $code) = @_;
Subroutine roles_for_status_code: called CC=1 SCAR=0.0
Pod documented
136  
137 4 my $ident = $lookup{$code};
138 4 return $self->roles_for_ident($ident);
139 }
140  
141 5 1;
142  
143 =pod
144  
145 =encoding UTF-8
146  
147 =head1 NAME
148  
149 HTTP::Throwable::Factory - a factory that throws HTTP::Throwables for you
150  
151 =head1 VERSION
152  
153 version 0.027
154  
155 =head1 OVERVIEW
156  
157 L<HTTP::Throwable> is a role that makes it easy to build exceptions that, once
158 thrown, can be turned into L<PSGI>-style HTTP responses. Because
159 HTTP::Throwable and all its related roles are, well, roles, they can't be
160 instantiated or thrown directly. Instead, they must be built into classes
161 first. HTTP::Throwable::Factory takes care of this job, building classes out
162 of the roles you need for the exception you want to throw.
163  
164 You can use the factory to either I<build> or I<throw> an exception of either a
165 I<generic> or I<specific> type. Building and throwing are very similar -- the
166 only difference is whether or not the newly built object is thrown or returned.
167 To throw an exception, use the C<throw> method on the factory. To return it,
168 use the C<new_exception> method. In the examples below, we'll just use
169 C<throw>.
170  
171 To throw a generic exception -- one where you must specify the status code and
172 reason, and any other headers -- you pass C<throw> a hashref of arguments that
173 will be passed to the exception class's constructor.
174  
175 HTTP::Throwable::Factory->throw({
176 status_code => 301,
177 reason => 'Moved Permanently',
178 additional_headers => [
179 Location => '/new',
180 ],
181 });
182  
183 To throw a specific type of exception, include an exception type identifier,
184 like this:
185  
186 HTTP::Throwable::Factory->throw(MovedPermanently => { location => '/new' });
187  
188 The type identifier is (by default) the end of a role name in the form
189 C<HTTP::Throwable::Role::Status::IDENTIFIER>. The full list of such included
190 roles is given in L<the HTTP::Throwable docs|HTTP::Throwable/WELL-KNOWN TYPES>.
191  
192 =head2 Exports
193  
194 You can import routines called C<http_throw> and C<http_exception> that work
195 like the C<throw> and C<new_exception> methods, respectively, but are not
196 called as methods. For example:
197  
198 use HTTP::Throwable::Factory 'http_exception';
199  
200 builder {
201 mount '/old' => http_exception('Gone'),
202 };
203  
204 =head1 SUBCLASSING
205  
206 One of the big benefits of using HTTP::Throwable::Factory is that you can
207 subclass it to change the kind of exceptions it provides.
208  
209 If you subclass it, you can change its behavior by overriding the following
210 methods -- provided in the order of likelihood that you'd want to override
211 them, most likely first.
212  
213 =head2 extra_roles
214  
215 This method returns a list of role names that will be included in any class
216 built by the factory. By default, it includes only
217 L<HTTP::Throwable::Role::TextBody> to satisfy HTTP::Throwable's requirements
218 for methods needed to build a body.
219  
220 This is the method you're most likely to override in a subclass.
221  
222 =head2 roles_for_ident
223  
224 =head2 roles_for_status_code
225  
226 =head2 roles_for_no_ident
227  
228 This methods convert the exception type identifier to a role to apply. For
229 example, if you call:
230  
231 Factory->throw(NotFound => { ... })
232  
233 ...then C<roles_for_ident> is called with "NotFound" as its argument.
234 C<roles_for_status_code> is used if the string is three ASCII digits.
235  
236 If C<throw> is called I<without> a type identifier, C<roles_for_no_ident> is
237 called.
238  
239 By default, C<roles_for_ident> returns C<HTTP::Throwable::Role::Status::$ident>
240 and C<roles_for_no_ident> returns L<HTTP::Throwable::Role::Generic> and
241 L<HTTP::Throwable::Role::BoringText>.
242  
243 =head2 base_class
244  
245 This is the base class that will be subclassed and into which all the roles
246 will be composed. By default, it is L<Moo::Object>, the universal base Moo
247 class.
248  
249 =head2 core_roles
250  
251 This method returns the roles that are expected to be applied to every
252 HTTP::Throwable exception. This method's results might change over time, and
253 you are encouraged I<B<not>> to alter it.
254  
255 =head1 AUTHORS
256  
257 =over 4
258  
259 =item *
260  
261 Stevan Little <stevan.little@iinteractive.com>
262  
263 =item *
264  
265 Ricardo Signes <rjbs@cpan.org>
266  
267 =back
268  
269 =head1 COPYRIGHT AND LICENSE
270  
271 This software is copyright (c) 2011 by Infinity Interactive, Inc.
272  
273 This is free software; you can redistribute it and/or modify it under
274 the same terms as the Perl 5 programming language system itself.
275  
276 =cut
277  
278 __END__
« Throwable/Role/Status/ImATeapot.pm ↑ Summary Throwable.pm »