| 1 | package HTTP::Throwable::Factory 0.028; | ||||||||||||
| 2 | 5 | our $AUTHORITY = 'cpan:STEVAN'; | |||||||||||
| 3 | |||||||||||||
| 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 => { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | exports => [ | ||||||||||||
| 12 | http_throw => Sub::Exporter::Util::curry_method('throw'), | ||||||||||||
| 13 | http_exception => Sub::Exporter::Util::curry_method('new_exception'), | ||||||||||||
| 14 | ], | ||||||||||||
| 15 | 5 | }; | |||||||||||
| 16 | 5 | ▶ | use Module::Runtime; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | |||||||||||||
| 19 | 57 | ▶ | sub throw { | ||||||||||
|
Subroutine
throw: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||
| 20 | 57 | my $factory = shift; | |||||||||||
| 21 | 57 | ▶ | my $ident = (! ref $_[0]) ? shift(@_) : undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | 57 | ▶ | my $arg = shift || {}; | ||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 23 | |||||||||||||
| 24 | 57 | $factory->class_for($ident, $arg)->throw($arg); | |||||||||||
| 25 | } | ||||||||||||
| 26 | |||||||||||||
| 27 | 3 | ▶ | sub new_exception { | ||||||||||
|
Subroutine
new_exception: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 28 | 3 | my $factory = shift; | |||||||||||
| 29 | 3 | ▶ | my $ident = (! ref $_[0]) ? shift(@_) : undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | 3 | ▶ | my $arg = shift || {}; | ||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 31 | |||||||||||||
| 32 | 3 | $factory->class_for($ident, $arg)->new($arg); | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | 60 | ▶ | sub core_roles { | ||||||||||
|
Subroutine
core_roles: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 36 | 60 | return qw( | |||||||||||
| 37 | HTTP::Throwable | ||||||||||||
| 38 | ); | ||||||||||||
| 39 | } | ||||||||||||
| 40 | |||||||||||||
| 41 | 33 | ▶ | sub extra_roles { | ||||||||||
|
Subroutine
extra_roles: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 42 | 33 | return qw( | |||||||||||
| 43 | HTTP::Throwable::Role::TextBody | ||||||||||||
| 44 | ); | ||||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | 58 | ▶ | sub roles_for_ident { | ||||||||||
|
Subroutine
roles_for_ident: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||
| 48 | 58 | my ($self, $ident) = @_; | |||||||||||
| 49 | |||||||||||||
| 50 | 58 | ▶ | Carp::confess("roles_for_ident called with undefined ident") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | unless defined $ident; | ||||||||||||
| 52 | |||||||||||||
| 53 | 58 | ▶ | Carp::confess("roles_for_ident called with empty ident string") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | unless length $ident; | ||||||||||||
| 55 | |||||||||||||
| 56 | 58 | return "HTTP::Throwable::Role::Status::$ident"; | |||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | 2 | ▶ | sub roles_for_no_ident { | ||||||||||
|
Subroutine
roles_for_no_ident: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 60 | 2 | my ($self, $ident) = @_; | |||||||||||
| 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 | 60 | ▶ | sub class_for { | ||||||||||
|
Subroutine
class_for: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||
| 71 | 60 | my ($self, $ident) = @_; | |||||||||||
| 72 | |||||||||||||
| 73 | 60 | my @roles; | |||||||||||
| 74 | 60 | ▶ | if (defined $ident) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 58 | ▶ | if ($ident =~ /\A[0-9]{3}\z/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 => 'ServiceUnavailable', | ||||||||||||
| 130 | 504 => 'GatewayTimeout', | ||||||||||||
| 131 | 505 => 'HTTPVersionNotSupported', | ||||||||||||
| 132 | ); | ||||||||||||
| 133 | |||||||||||||
| 134 | 4 | ▶ | sub roles_for_status_code { | ||||||||||
|
Subroutine
roles_for_status_code: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 135 | 4 | my ($self, $code) = @_; | |||||||||||
| 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.028 | ||||||||||||
| 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 PERL VERSION | ||||||||||||
| 205 | |||||||||||||
| 206 | This library should run on perls released even a long time ago. It should work | ||||||||||||
| 207 | on any version of perl released in the last five years. | ||||||||||||
| 208 | |||||||||||||
| 209 | Although it may work on older versions of perl, no guarantee is made that the | ||||||||||||
| 210 | minimum required version will not be increased. The version may be increased | ||||||||||||
| 211 | for any reason, and there is no promise that patches will be accepted to lower | ||||||||||||
| 212 | the minimum required perl. | ||||||||||||
| 213 | |||||||||||||
| 214 | =head1 SUBCLASSING | ||||||||||||
| 215 | |||||||||||||
| 216 | One of the big benefits of using HTTP::Throwable::Factory is that you can | ||||||||||||
| 217 | subclass it to change the kind of exceptions it provides. | ||||||||||||
| 218 | |||||||||||||
| 219 | If you subclass it, you can change its behavior by overriding the following | ||||||||||||
| 220 | methods -- provided in the order of likelihood that you'd want to override | ||||||||||||
| 221 | them, most likely first. | ||||||||||||
| 222 | |||||||||||||
| 223 | =head2 extra_roles | ||||||||||||
| 224 | |||||||||||||
| 225 | This method returns a list of role names that will be included in any class | ||||||||||||
| 226 | built by the factory. By default, it includes only | ||||||||||||
| 227 | L<HTTP::Throwable::Role::TextBody> to satisfy HTTP::Throwable's requirements | ||||||||||||
| 228 | for methods needed to build a body. | ||||||||||||
| 229 | |||||||||||||
| 230 | This is the method you're most likely to override in a subclass. | ||||||||||||
| 231 | |||||||||||||
| 232 | =head2 roles_for_ident | ||||||||||||
| 233 | |||||||||||||
| 234 | =head2 roles_for_status_code | ||||||||||||
| 235 | |||||||||||||
| 236 | =head2 roles_for_no_ident | ||||||||||||
| 237 | |||||||||||||
| 238 | This methods convert the exception type identifier to a role to apply. For | ||||||||||||
| 239 | example, if you call: | ||||||||||||
| 240 | |||||||||||||
| 241 | Factory->throw(NotFound => { ... }) | ||||||||||||
| 242 | |||||||||||||
| 243 | ...then C<roles_for_ident> is called with "NotFound" as its argument. | ||||||||||||
| 244 | C<roles_for_status_code> is used if the string is three ASCII digits. | ||||||||||||
| 245 | |||||||||||||
| 246 | If C<throw> is called I<without> a type identifier, C<roles_for_no_ident> is | ||||||||||||
| 247 | called. | ||||||||||||
| 248 | |||||||||||||
| 249 | By default, C<roles_for_ident> returns C<HTTP::Throwable::Role::Status::$ident> | ||||||||||||
| 250 | and C<roles_for_no_ident> returns L<HTTP::Throwable::Role::Generic> and | ||||||||||||
| 251 | L<HTTP::Throwable::Role::BoringText>. | ||||||||||||
| 252 | |||||||||||||
| 253 | =head2 base_class | ||||||||||||
| 254 | |||||||||||||
| 255 | This is the base class that will be subclassed and into which all the roles | ||||||||||||
| 256 | will be composed. By default, it is L<Moo::Object>, the universal base Moo | ||||||||||||
| 257 | class. | ||||||||||||
| 258 | |||||||||||||
| 259 | =head2 core_roles | ||||||||||||
| 260 | |||||||||||||
| 261 | This method returns the roles that are expected to be applied to every | ||||||||||||
| 262 | HTTP::Throwable exception. This method's results might change over time, and | ||||||||||||
| 263 | you are encouraged I<B<not>> to alter it. | ||||||||||||
| 264 | |||||||||||||
| 265 | =head1 AUTHORS | ||||||||||||
| 266 | |||||||||||||
| 267 | =over 4 | ||||||||||||
| 268 | |||||||||||||
| 269 | =item * | ||||||||||||
| 270 | |||||||||||||
| 271 | Stevan Little <stevan.little@iinteractive.com> | ||||||||||||
| 272 | |||||||||||||
| 273 | =item * | ||||||||||||
| 274 | |||||||||||||
| 275 | Ricardo Signes <cpan@semiotic.systems> | ||||||||||||
| 276 | |||||||||||||
| 277 | =back | ||||||||||||
| 278 | |||||||||||||
| 279 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 280 | |||||||||||||
| 281 | This software is copyright (c) 2011 by Infinity Interactive, Inc. | ||||||||||||
| 282 | |||||||||||||
| 283 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 284 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 285 | |||||||||||||
| 286 | =cut | ||||||||||||
| 287 | |||||||||||||
| 288 | __END__ | ||||||||||||