File Coverage

blib/lib/Catalyst/View/Errors/HTML.pm
Criterion Covered Total %
statement 12 36 33.3
branch 0 4 0.0
condition 0 3 0.0
subroutine 4 11 36.3
pod 4 7 57.1
total 20 61 32.7


line stmt bran cond sub pod time code
1             package Catalyst::View::Errors::HTML;
2              
3 1     1   1555 use Moose;
  1         545110  
  1         11  
4 1     1   9282 use Text::Template;
  1         4146  
  1         79  
5 1     1   635 use CatalystX::Utils::ContentNegotiation;
  1         3  
  1         81  
6 1     1   536 use CatalystX::Utils::ErrorMessages;
  1         4  
  1         2209  
7              
8             extends 'Catalyst::View';
9             with 'Catalyst::Component::ApplicationAttribute';
10              
11             has template_engine_args => (
12             is=>'ro',
13             required=>1,
14             lazy=>1,
15             default=> sub {
16             my $self = shift;
17             my $template = $self->_application->config->{root}->file($self->template_name);
18             my $source = -e $template ? $template->slurp : $self->html($self->_application);
19             return +{TYPE => 'STRING', SOURCE => $source};
20             },
21             );
22              
23             has template_name => (is=>'ro', required=>1, default=>'http_errors_html.tmpl');
24             has default_language => (is=>'ro', required=>1, default=>'en_US');
25              
26             sub html {
27 0     0 1   my ($self, $app) = @_;
28 0           return q[
29             <!DOCTYPE html>
30             <html lang="{$lang}">
31             <head>
32             <meta charset="utf-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" />
33             <meta name="viewport" content="width=device-width, initial-scale=1" />
34             <link rel="icon" href="data:,"> <!-- Stop favicon for now -->
35             <title>{$title}</title>
36             </head>
37             <body>
38             <div class="cover"><h1>{$code}: {$title}</h1><p class="lead">{$message}</p></div>
39             </body>
40             </html>
41             ];
42             }
43              
44             has template_engine => (
45             is => 'ro',
46             required => 1,
47             init_arg => undef,
48             lazy => 1,
49             default => sub {
50             my %args = %{shift->template_engine_args};
51             my $engine = Text::Template->new(%args);
52             $engine->compile;
53             return $engine;
54             }
55             );
56              
57             has cn => (
58             is => 'ro',
59             init_arg => undef,
60             required => 1,
61             default => sub { CatalystX::Utils::ContentNegotiation::content_negotiator },
62             );
63              
64             sub http_default {
65 0     0 0   my ($self, $c, $code, %args) = @_;
66 0           my $lang = $self->get_language($c);
67 0           my $message_info = $self->finalize_message_info($c, $code, $lang, %args);
68              
69 0           my $html = $self->render_template($c, $message_info);
70            
71 0           $c->response->body($html);
72 0           $c->response->content_type('text/html');
73 0           $c->response->status($code);
74             }
75              
76             sub get_language {
77 0     0 0   my ($self, $c) = @_;
78 0 0         if(my $lang = $c->request->header('Accept-Language')) {
79 0   0       return $self->cn->choose_language([$self->available_languages($c)], $lang) || $self->default_language;
80             }
81 0           return $self->default_language;
82             }
83              
84             sub available_languages {
85 0     0 1   my ($self, $c) = @_;
86 0           return my @lang_tags = CatalystX::Utils::ErrorMessages::available_languages;
87             }
88              
89             sub finalize_message_info {
90 0     0 1   my ($self, $c, $code, $lang, %args) = @_;
91 0           my $message_info = $self->get_message_info($c, $lang, $code);
92 0 0         $message_info = $self->get_message_info($c, 'en_US', $code) unless $message_info; # Fallback to US English
93 0           $c->log->info("Generated error response: $code $message_info->{title}: $message_info->{message}");
94             return +{
95 0           %$message_info,
96             lang => $lang,
97             %args,
98             };
99             }
100              
101             sub get_message_info {
102 0     0 1   my ($self, $c, $lang, $code) = @_;
103 0           return my $message_info_hash = CatalystX::Utils::ErrorMessages::get_message_info($lang, $code);
104             }
105              
106             sub render_template {
107 0     0 0   my ($self, $c, $message_info) = @_;
108 0           return my $html = $self->template_engine->fill_in(HASH => $message_info);
109             }
110              
111             __PACKAGE__->meta->make_immutable;
112              
113             =head1 NAME
114              
115             Catalyst::View::Errors::HTML - Standard HTTP Errors Responses in HTML
116              
117             =head1 SYNOPSIS
118              
119             package MyApp::View::HTML;
120              
121             use Moose;
122             extends 'Catalyst::View::Errors::HTML';
123              
124             __PACKAGE__->meta->make_immutable;
125              
126             =head1 DESCRIPTION
127              
128             View class for generating error responses. If you want a lot of customizations you can subclass
129             this in your application, or just use your own view.
130              
131             =head1 METHODS
132              
133             This view exposes the follow methods for public use or for a programmer to override
134             to change function.
135              
136             =head2 html
137              
138             Should return a string suitable for L<Text::Template> and is used to generate
139             an HTML error response. This is used if there's no file at C<$APPHOME/root/http_errors_html.tmpl>
140              
141             =head2 available_languages
142              
143             An array of the languages available for serving error responses. By default we use
144             L<CatalystX::Utils::ErrorMessages> but if you have your own list of translations you can override
145             this.
146              
147             =head2 get_message_info
148              
149             Return error message info by code and language. By default we use
150             L<CatalystX::Utils::ErrorMessages> but if you have your own list of translations you can override
151             this.
152              
153             =head2 finalize_message_info
154              
155             Finalizes the hash of data that is sent to the template handler to make the body of the error
156             response. You can override if you want to change or add to this data.
157              
158             =head1 CONFIGURATION
159              
160             This View exposes the following configuration options
161              
162             =head2 template_engine_args
163              
164             Args that are used to start the L<Text::Template> template engin
165              
166             =head2 template_name
167              
168             Name of the files under $APPHOME/root that is used to render an error view.
169             Default is C<http_errors_html.tmpl>. If this this file doesn't exist we
170             instead use the return of L</html> method for the template string.
171              
172             =head2 default_language
173              
174             When doing content negotiation if there's no language preferred by the client
175             use this language. Default is C<en_US>.
176              
177             =head1 SEE ALSO
178            
179             L<CatalystX::Errors>.
180              
181             =head1 AUTHOR
182            
183             L<CatalystX::Errors>.
184            
185             =head1 COPYRIGHT & LICENSE
186            
187             L<CatalystX::Errors>.
188              
189             =cut