File Coverage

blib/lib/Kossy/Exception.pm
Criterion Covered Total %
statement 39 39 100.0
branch 8 8 100.0
condition 7 8 87.5
subroutine 8 8 100.0
pod 0 3 0.0
total 62 66 93.9


line stmt bran cond sub pod time code
1             package Kossy::Exception;
2              
3 10     10   83 use strict;
  10         19  
  10         318  
4 10     10   93 use warnings;
  10         25  
  10         273  
5 10     10   5173 use HTTP::Status;
  10         48261  
  10         2519  
6 10     10   2128 use Text::Xslate qw/html_escape/;
  10         41614  
  10         667  
7 10     10   4531 use Kossy::Response;
  10         31  
  10         3693  
8              
9             our $VERSION = '0.50';
10              
11             sub new {
12 17     17 0 59 my $class = shift;
13 17         27 my $code = shift;
14 17         46 my %args = (
15             code => $code,
16             );
17 17 100       98 if ( @_ == 1 ) {
    100          
18 1         5 $args{message} = shift;
19             }
20             elsif ( @_ % 2 == 0) {
21 15         61 %args = (
22             %args,
23             @_
24             );
25             }
26 17         116 bless \%args, $class;
27             }
28              
29             sub response {
30 17     17 0 6716 my $self = shift;
31 17   100     84 my $code = $self->{code} || 500;
32              
33 17 100       51 if ($self->{response}) {
34 6         36 $self->{response}->code($code);
35 6         78 return $self->{response}->finalize;
36             }
37             else {
38 11         20 my $message = $self->{message};
39 11   66     47 $message ||= HTTP::Status::status_message($code);
40              
41 11         74 my @headers = (
42             'Content-Type' => q!text/html; charset=UTF-8!,
43             );
44              
45 11 100 100     47 if ($code =~ /^3/ && (my $loc = eval { $self->{location} })) {
  2         10  
46 1         3 push(@headers, Location => $loc);
47             }
48              
49 11         37 return Kossy::Response->new($code, \@headers, [$self->html($code,$message)])->finalize;
50             }
51             }
52              
53             sub html {
54 11     11 0 18 my $self = shift;
55 11         34 my ($code,$message) = @_;
56 11         68 $code = html_escape($code);
57 11         37 $message = html_escape($message);
58 11         157 return <
59            
60            
61            
62            
63            
75            
76            
77            

78             $code $message
79            

80            
81            
82            
83             EOF
84             }
85              
86             1;