File Coverage

blib/lib/Kelp/Exception.pm
Criterion Covered Total %
statement 14 14 100.0
branch 4 4 100.0
condition 2 3 66.6
subroutine 4 4 100.0
pod 1 2 50.0
total 25 27 92.5


line stmt bran cond sub pod time code
1             package Kelp::Exception;
2              
3 2     2   17888 use Kelp::Base;
  2         5  
  2         13  
4              
5 2     2   13 use Carp;
  2         4  
  2         715  
6              
7             attr -code => sub { croak 'code is required' };
8              
9             attr body => undef;
10              
11             sub new
12             {
13 13     13 0 1187 my ($class, $code, %params) = @_;
14              
15 13 100 66     392 croak 'Kelp::Exception can only accept 4XX or 5XX codes'
16             unless defined $code && $code =~ /^[45]\d\d$/;
17              
18 11         20 $params{code} = $code;
19 11         32 return $class->SUPER::new(%params);
20             }
21              
22             sub throw
23             {
24 13     13 1 64 my $self = shift;
25 13 100       19 if (!ref $self) {
26 12         26 $self = $self->new(@_);
27             }
28              
29 11         91 die $self;
30             }
31              
32             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