| 1 | package Kelp::Exception; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Kelp::Base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 0 | ▶ | attr -code => sub { croak 'code is required' }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | attr body => undef; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub new | ||||||||||||||||||||||||||||
| 12 | 13 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | 13 | my ($class, $code, %params) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 13 | ▶ | croak 'Kelp::Exception can only accept 4XX or 5XX codes' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $code and $code =~ /^[45]\d\d$/
MC/DC50%1/2
defined $code and $code =~ /^[45]\d\d$/
defined $code
$code =~ /^[45]\d\d$/
Truth table66%2/3
A and B
A= defined $codeB= $code =~ /^[45]\d\d$/
| |||||||||||||||||||||||||||||
| 16 | unless defined $code && $code =~ /^[45]\d\d$/; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 11 | $params{code} = $code; | |||||||||||||||||||||||||||
| 19 | 11 | return $class->SUPER::new(%params); | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub throw | ||||||||||||||||||||||||||||
| 23 | 13 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
throw: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 25 | 13 | ▶ | if (!ref $self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 12 | $self = $self->new(@_); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 11 | die $self; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | 1; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | __END__ | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =pod | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 NAME | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Kelp::Exception - Tiny HTTP exceptions | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # will log the body to 'error' level logger | ||||||||||||||||||||||||||||
| 45 | Kelp::Exception->throw(400, body => 'The request was malformed and got aborted'); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # will only show an error page with the code | ||||||||||||||||||||||||||||
| 48 | Kelp::Exception->throw(410); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # code is optional - 500 by default | ||||||||||||||||||||||||||||
| 51 | Kelp::Exception->throw; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | This module offers a fine-grained control of what the user sees when an | ||||||||||||||||||||||||||||
| 56 | exception occurs. Generally, this could also be done by setting the | ||||||||||||||||||||||||||||
| 57 | result code manually, but that requires passing the Kelp instance around and | ||||||||||||||||||||||||||||
| 58 | does not immediately end the handling code. Exceptions are a way to end route | ||||||||||||||||||||||||||||
| 59 | handler execution from deep within the call stack. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | This implementation is very incomplete and can only handle 4XX and 5XX status | ||||||||||||||||||||||||||||
| 62 | codes, meaning that you can't do redirects and normal responses like this. It | ||||||||||||||||||||||||||||
| 63 | also tries to maintain some degree of compatibility with L<HTTP::Exception> | ||||||||||||||||||||||||||||
| 64 | without its complexity. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 code | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | HTTP status code. Only possible are 5XX and 4XX. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Readonly. Required. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head2 body | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Body of the exception - can be anything that can be serialized and if passed | ||||||||||||||||||||||||||||
| 77 | will cause the application to log it on error level. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | Content type and status string for the response will be set accordingly. Will | ||||||||||||||||||||||||||||
| 80 | render HTML in template and plaintext if there is no template (as usual errors do). | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 METHODS | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head2 throw | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # both do exactly the same | ||||||||||||||||||||||||||||
| 87 | Kelp::Exception->throw(...); | ||||||||||||||||||||||||||||
| 88 | die Kelp::Exception->new(...); | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Same as simply constructing and calling die on an object. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =cut | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||