File Coverage

blib/lib/Catalyst/View/Errors/Text.pm
Criterion Covered Total %
statement 12 34 35.2
branch 0 2 0.0
condition 0 3 0.0
subroutine 4 11 36.3
pod 4 7 57.1
total 20 57 35.0


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