File Coverage

blib/lib/Catalyst/View/Errors/HTML.pm
Criterion Covered Total %
statement 6 15 40.0
branch n/a
condition n/a
subroutine 2 5 40.0
pod 1 3 33.3
total 9 23 39.1


line stmt bran cond sub pod time code
1              
2             use Moose;
3 1     1   1302 use Text::Template;
  1         475455  
  1         7  
4 1     1   8441  
  1         3577  
  1         382  
5             extends 'Catalyst::View';
6             with 'Catalyst::Component::ApplicationAttribute';
7              
8             has template_engine_args => (
9             is=>'ro',
10             required=>1,
11             lazy=>1,
12             default=> sub {
13             my $self = shift;
14             my $template = $self->_application->config->{root}->file($self->template_name);
15             my $source = -e $template ? $template->slurp : $self->html($self->_application);
16             return +{TYPE => 'STRING', SOURCE => $source};
17             },
18             );
19              
20             has template_name => (is=>'ro', required=>1, default=>'http_errors_html.tmpl');
21              
22             my ($self, $app) = @_;
23             return q[
24 0     0 1   <!DOCTYPE html>
25 0           <html lang="{$lang}">
26             <head>
27             <meta charset="utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" />
28             <meta name="viewport" content="width=device-width, initial-scale=1" />
29             <link rel="icon" href="data:,"> <!-- Stop favicon for now -->
30             <title>{$title}</title>
31             </head>
32             <body>
33             <div class="cover"><h1>{$status_code}: {$title}</h1><p class="lead">{$message}</p></div>
34             </body>
35             </html>
36             ];
37             }
38              
39             has template_engine => (
40             is => 'ro',
41             required => 1,
42             init_arg => undef,
43             lazy => 1,
44             default => sub {
45             my %args = %{shift->template_engine_args};
46             my $engine = Text::Template->new(%args);
47             $engine->compile;
48             return $engine;
49             }
50             );
51              
52             my ($self, $c, $code, %args) = @_;
53             my $html = $self->render_template($c, \%args);
54              
55 0     0 0   $c->response->body($html);
56 0           $c->response->content_type('text/html');
57             $c->response->status($code);
58 0           }
59 0            
60 0           my ($self, $c, $message_info) = @_;
61             return my $html = $self->template_engine->fill_in(HASH => $message_info);
62             }
63              
64 0     0 0   __PACKAGE__->meta->make_immutable;
65 0            
66             =head1 NAME
67              
68             Catalyst::View::Errors::HTML - Standard HTTP Errors Responses in HTML
69              
70             =head1 SYNOPSIS
71              
72             package MyApp::View::HTML;
73              
74             use Moose;
75             extends 'Catalyst::View::Errors::HTML';
76              
77             __PACKAGE__->meta->make_immutable;
78              
79             =head1 DESCRIPTION
80              
81             View class for generating error responses. If you want a lot of customizations you can subclass
82             this in your application, or just use your own view. You can customize this by adding a template
83             at C<$APPHOME/root/http_errors_html.tmpl> (using L<Text::Template> as the templating system)
84              
85             The follow field arguments are passed to this template:
86              
87             =over 4
88              
89             =item lang
90              
91             Defaults to "en_US". This is the language code of the error response.
92              
93             =item message
94              
95             This is a text message of the error condition
96              
97             =item status_code
98              
99             This is the HTTP Status code of the error
100              
101             =item title
102              
103             The official HTTP Status error title (Not Found, Not Authorized, etc.)
104              
105             =item uri
106              
107             The URI that generated the error. Be careful displaying this in your template since if its not
108             properly escaped you can open yourself to HTML injection / Javascript injection attackes.
109              
110             =back
111              
112             In addition any other arguments passed in ->dispatch_error / ->detach_error.
113              
114             =head1 METHODS
115              
116             This view exposes the follow methods for public use or for a programmer to override
117             to change function.
118              
119             =head2 html
120              
121             Should return a string suitable for L<Text::Template> and is used to generate
122             an HTML error response. This is used if there's no file at C<$APPHOME/root/http_errors_html.tmpl>
123              
124             =head1 CONFIGURATION
125              
126             This View exposes the following configuration options
127              
128             =head2 template_engine_args
129              
130             Args that are used to start the L<Text::Template> template engin
131              
132             =head2 template_name
133              
134             Name of the files under $APPHOME/root that is used to render an error view.
135             Default is C<http_errors_html.tmpl>. If this this file doesn't exist we
136             instead use the return of L</html> method for the template string.
137              
138             =head1 SEE ALSO
139            
140             L<CatalystX::Errors>.
141              
142             =head1 AUTHOR
143            
144             L<CatalystX::Errors>.
145            
146             =head1 COPYRIGHT & LICENSE
147            
148             L<CatalystX::Errors>.
149              
150             =cut