File Coverage

blib/lib/Kelp/Exception.pm
Criterion Covered Total %
statement 13 13 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 4 4 100.0
pod 1 2 50.0
total 22 24 91.6


line stmt bran cond sub pod time code
1              
2             use Kelp::Base;
3 1     1   19024  
  1         2  
  1         17  
4             use Carp;
5 1     1   73  
  1         2  
  1         222  
6             attr -code => sub { croak 'code is required' };
7              
8             attr 'body';
9              
10             my ($class, $code, %params) = @_;
11              
12 13     13 0 39 croak 'Kelp::Exception can only accept 4XX or 5XX codes'
13             unless defined $code && $code =~ /^[45]\d\d$/;
14 13 100 66     231  
15             $params{code} = $code;
16             return $class->SUPER::new(%params);
17 12         66 }
18 12         58  
19             my $class = shift;
20             my $ex = $class->new(@_);
21             die $ex;
22 13     13 1 107 }
23 13         40  
24 12         81 1;
25              
26              
27             =pod
28              
29             =head1 NAME
30              
31             Kelp::Exception - Tiny HTTP exceptions
32              
33             =head1 SYNOPSIS
34              
35             Exception->throw(400, body => 'The request was malformed');
36              
37             # code is optional - 500 by default
38             Kelp::Exception->throw;
39              
40             # can control what user sees - even in deployment (unlike string exceptions)
41             Kelp::Exception->throw(501, body => {
42             status => \0,
43             error => 'This method is not yet implemented'
44             });
45              
46             =head1 DESCRIPTION
47              
48             This module offers a fine-grained control of what the user sees when an
49             exception occurs. Generally, this could also be done by setting the
50             result code manually, but that requires passing the Kelp instance around and
51             does not immediately end the handling code. Exceptions are a way to end route
52             handler execution from deep within the call stack.
53              
54             This implementation is very incomplete and can only handle 4XX and 5XX status
55             codes, meaning that you can't do redirects and normal responses like this. It
56             also tries to maintain some degree of compatibility with L<HTTP::Exception>
57             without its complexity.
58              
59             =head1 ATTRIBUTES
60              
61             =head2 code
62              
63             HTTP status code. Only possible are 5XX and 4XX.
64              
65             Readonly.
66              
67             =head2 body
68              
69             Required. Body of the request - can be a string for HTML and a hashref /
70             arrayref for JSON responses.
71              
72             A string will be passed to C<< $response->render_error >> to be rendered inside
73             an error template, if available. A reference will be JSON encoded if JSON is
74             available, otherwise will produce an exception.
75              
76             Content type for the response will be set accordingly.
77              
78             =head1 METHODS
79              
80             =head2 throw
81              
82             # both do exactly the same
83             Kelp::Exception->throw(...);
84             die Kelp::Exception->new(...);
85              
86             Same as simply constructing and calling die on an object.
87              
88             =head1 CAVEATS
89              
90             =over
91              
92             =item
93              
94             Code 500 exceptions will not be logged, as it is considered something that a
95             web developer know about. They are not thrown anywhere in Kelp internal code,
96             so only user code can fall victim to this.
97              
98             =item
99              
100             If there is no content type set, and the exception body is not a reference,
101             then a C<text/html> content type will be guessed and the body text will be
102             passed to an error template, if available.
103              
104             =back
105              
106             =cut
107              
108