GX/View/Error.pm

filter: Statementstmt 43.6% 48 / 110 Branchbran 20.0% 8 / 40 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 73.3% 11 / 15 Podpod 100% 3 / 3 totaltotal 37.2% 70 / 188 CC 26 SCAR 35.7
CC
26
Cov
34%
_render_html
90.0
render
56.0
_generate_html
30.0
CRAP 35.6
« GX/Database.pm ↑ Summary GX/Engine/FCGI.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/View/Error.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::View::Error;
7  
8 2 ▶use GX::Callback::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use GX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use GX::Exception::Formatter::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use GX::HTML::Util qw( escape_html );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 # ----------------------------------------------------------------------------------------------------------------------
18 # Class setup
19 # ----------------------------------------------------------------------------------------------------------------------
20  
21 2 ▶use GX::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 extends 'GX::View';
24  
25 2 has 'default_encoding' => (
26 isa => 'Scalar',
27 initialize => 1,
28 accessors => {
29 '_get_default_encoding' => { type => 'get' },
30 '_set_default_encoding' => { type => 'set' }
31 }
32 );
33  
34 2 has 'default_format' => (
35 isa => 'Scalar',
36 initialize => 1,
37 accessors => {
38 '_get_default_format' => { type => 'get' },
39 '_set_default_format' => { type => 'set' }
40 }
41 );
42  
43 2 has 'format_handlers' => (
44 isa => 'Hash',
45 initialize => 1,
46 initializer => '_initialize_format_handlers',
47 accessors => {
48 '_get_format_handler' => { type => 'get_value' },
49 '_set_format_handler' => { type => 'set_value' },
50 '_get_format_handlers' => { type => 'get_reference' }
51 }
52 );
53  
54 2 build;
55  
56  
57 # ----------------------------------------------------------------------------------------------------------------------
58 # Export mechanism
59 # ----------------------------------------------------------------------------------------------------------------------
60  
61 2 __PACKAGE__->_install_import_method;
62  
63  
64 # ----------------------------------------------------------------------------------------------------------------------
65 # Public methods
66 # ----------------------------------------------------------------------------------------------------------------------
67  
68 2 ▶sub default_encoding {
Subroutine default_encoding: called CC=1 SCAR=0.0
Pod documented
69  
70 2 return $_[0]->instance->_get_default_encoding;
71  
72 }
73  
74 2 ▶sub default_format {
Subroutine default_format: called CC=1 SCAR=0.0
Pod documented
75  
76 2 return $_[0]->instance->_get_default_format;
77  
78 }
79  
80 0 ▶sub render {
Subroutine render: not called CC=7 SCAR=40.3
Pod documented
81  
82 0 my $self = shift->instance;
83 0 my %args = @_;
84  
85 0 ▶ my $format = $args{'format'} // $self->_get_default_format;
Condition0%0/3
$args{"format"} // $self->_get_default_format
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"format"} // $self->_get_default_format
$args{"format"} $self->_get_default_format
Truth table0%0/3
A // B
A= $args{"format"}B= $self->_get_default_format
ABresult
1X1
011
000
86  
87 0 ▶ if ( ! defined $format ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $format) 0 0 0
88 0 complain "Unspecified output format";
89 }
90  
91 0 my $handler = $self->_get_format_handler( $format );
92  
93 0 ▶ if ( ! $handler ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($handler) 0 0 0
94 0 complain "Unsupported output format \"$format\"";
95 }
96  
97 0 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 0 0 0
98  
99 0 my $output;
100  
101 0 eval { $output = $handler->call( %args ) };
102  
103 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
104 0 complain $@;
105 }
106  
107 0 return $$output;
108  
109 }
110 else {
111  
112 0 eval { $handler->call( %args ) };
113  
114 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
115 0 complain $@;
116 }
117  
118 0 return;
119  
120 }
121  
122 }
123  
124  
125 # ----------------------------------------------------------------------------------------------------------------------
126 # Private methods
127 # ----------------------------------------------------------------------------------------------------------------------
128  
129 0 ▶sub _generate_html {
Subroutine _generate_html: not called CC=5 SCAR=34.0
130  
131 0 my $self = shift;
132 0 my $error = shift;
133 0 my $charset = shift;
134  
135 0 my $html;
136  
137 0 ▶ if ( $self->application->mode eq 'development' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->application->mode eq 'development') { } 0 0 0
138  
139 0 my $title;
140 my $error_html;
141  
142 0 ▶ if ( blessed $error && $error->isa( 'GX::Exception' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($error) and $error->isa('GX::Exception')) { } 0 0 0
Condition0%0/3
builtin::blessed($error) and $error->isa("GX::Exception")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($error) and $error->isa("GX::Exception")
builtin::blessed($error) $error->isa("GX::Exception")
Truth table0%0/3
A and B
A= builtin::blessed($error)B= $error->isa("GX::Exception")
ABresult
0X0
100
111
143 0 $title = ref $error;
144 0 $error_html = GX::Exception::Formatter::HTML->format( $error );
145 }
146 else {
147  
148 0 $title = 'Error';
149  
150 0 ▶ $error //= 'Unknown error';
Condition0%0/2
$error //= "Unknown error"
l!l
00
MC/DC0%0/1
$error //= "Unknown error"
$error
Truth table0%0/2
A
A= $error
Aresult
11
00
151  
152 0 $error_html = join "\n",
153 '<h1>Error</h1>',
154 '<div class="exception">',
155 '<div class="message">',
156 '<code>' . join( '<br />', split( /\n/, escape_html( $error ) ) ) . '</code>',
157 '</div>',
158 '</div>' . "\n";
159  
160 }
161  
162 0 $html = <<HTML;
163 <?xml version="1.0" encoding="$charset"?>
164 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
165 <html xmlns="http://www.w3.org/1999/xhtml">
166 <head>
167 <title>$title</title>
168 <meta http-equiv="Cache-Control" content="no-cache" />
169 <meta http-equiv="Content-Type" content="text/html; charset=$charset" />
170 <meta http-equiv="Expires" content="0" />
171 <meta http-equiv="Pragma" content="no-cache" />
172 <meta name="robots" content="noindex, nofollow" />
173 <style type="text/css">
174 body, code, div, h1, h2, p, table, tbody, tr, td { margin:0; padding:0 }
175 body { padding:50px; font-size:15px; font-family:Arial, sans-serif; color:#333 }
176 h1 { margin:0 0 25px 0; font-size: 32px }
177 h2 { margin:0 0 25px 0; font-size: 24px }
178 li { margin-bottom:25px }
179 p { margin:15px 0 }
180 table { width:100%; empty-cells:show; border-spacing:1px }
181 code { font-family:"DejaVu Sans Mono", Monaco, "Lucida Console", "Andale Mono", monospace }
182 div.message { margin:25px 0; padding:15px; background:#F4CDCD }
183 div.subexceptions { margin:50px 0 }
184 div.stack_trace { margin:50px 0 }
185 div.stack_trace p strong { padding:3px 5px; background: #FEEFB3; font-size:13px; font-family:"DejaVu Sans Mono", Monaco, "Lucida Console", "Andale Mono", monospace }
186 div.viewport { overflow:auto }
187 table.source code { font-size:13px; color:#444 }
188 table.source td { padding:2px 5px; background:#EEE }
189 table.source td.line { text-align:right }
190 table.source td.code { width:100% }
191 table.source tr.highlight td { background:#E3E3E3 }
192 table.source tr.highlight td code { color:#333 }
193 </style>
194 </head>
195 <body>
196 $error_html</body>
197 </html>
198 HTML
199  
200 }
201 else {
202  
203 0 $html = <<HTML;
204 <?xml version="1.0" encoding="$charset"?>
205 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
206 <html xmlns="http://www.w3.org/1999/xhtml">
207 <head>
208 <title>Server error!</title>
209 <meta http-equiv="Content-Type" content="text/html; charset=$charset" />
210 </head>
211 <body>
212 <h1>Server error!</h1>
213 <p>The server encountered an internal error and was unable to complete your request.</p>
214 </body>
215 </html>
216 HTML
217  
218 }
219  
220 0 return $html;
221  
222 }
223  
224 2 ▶sub _initialize_format_handlers {
Subroutine _initialize_format_handlers: called CC=1 SCAR=0.0
225  
226 2 my $self = shift;
227  
228 return {
229 2 'html' => GX::Callback::Method->new( invocant => $self, method => '_render_html' )
230 };
231  
232 }
233  
234 0 ▶sub _register {
Subroutine _register: not called CC=2 SCAR=17.9
235  
236 0 my $self = shift;
237 0 my $application = shift;
238  
239 0 $self->SUPER::_register( $application );
240  
241 0 ▶ if ( ! defined $self->_get_default_encoding ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_get_default_encoding) 0 0 0
242 0 $self->_set_default_encoding( $application->default_encoding );
243 }
244  
245 0 return $self;
246  
247 }
248  
249 0 ▶sub _render_html {
Subroutine _render_html: not called CC=9 SCAR=45.0
250  
251 0 my $self = shift;
252 0 my %args = @_;
253  
254 0 my $context = $args{'context'};
255  
256 my $error = exists $args{'error'}
257 0 ▶ ? $args{'error'}
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'error'} ? : 0 0 0
$context ? : 0 0 0
258 : $context
259 ? $context->error
260 : undef;
261  
262 0 ▶ my $encoding = $args{'encoding'} // $self->_get_default_encoding // 'utf-8';
Condition0%0/5
($args{"encoding"} // $self->_get_default_encoding) // "utf-8"
l!l
00
$args{"encoding"} // $self->_get_default_encoding
l!l&&r!l&&!r
000
MC/DC0%0/2
($args{"encoding"} // $self->_get_default_encoding) // "utf-8"
$args{"encoding"} $self->_get_default_encoding
Truth table0%0/3
A // B
A= $args{"encoding"}B= $self->_get_default_encoding
ABresult
1X1
011
000
263  
264 0 my $encoder = Encode::find_encoding( $encoding );
265  
266 0 ▶ if ( ! $encoder ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($encoder) 0 0 0
267 0 throw "Unsupported output encoding \"$encoding\"";
268 }
269  
270 0 my $charset = $encoder->mime_name;
271  
272 0 my $output = $encoder->encode( $self->_generate_html( $error, $charset ) );
273  
274 0 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 0 0 0
275 0 return \$output;
276 }
277  
278 0 ▶ if ( ! defined $context ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $context) 0 0 0
279 0 throw "Missing argument (\"context\")";
280 }
281  
282 0 my $response = $context->response;
283  
284 0 ▶ if ( ! defined $response->content_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response->content_type) 0 0 0
285 0 $response->content_type( 'text/html; charset=' . $charset );
286 }
287  
288 0 $response->add( \$output );
289  
290 0 return;
291  
292  
293 }
294  
295 2 ▶sub _setup_config {
Subroutine _setup_config: called CC=7 SCAR=20.0
296  
297 2 my $self = shift;
298 2 my $args = shift;
299  
300 2 ▶ if ( exists $args->{'default_encoding'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"default_encoding"}) 1 1 2
301  
302 1 my $encoding = delete $args->{'default_encoding'};
303  
304 1 ▶ if ( defined $encoding ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding) 1 0 1
305  
306 1 my $encoder = Encode::find_encoding( $encoding );
307  
308 1 ▶ if ( ! $encoder ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($encoder) 0 1 1
309 0 throw "Unsupported default encoding \"$encoding\"";
310 }
311  
312 1 $self->_set_default_encoding( $encoder->name );
313  
314 }
315  
316 }
317  
318 2 ▶ if ( exists $args->{'default_format'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{'default_format'}) { } 1 1 2
319  
320 1 my $format = delete $args->{'default_format'};
321  
322 1 ▶ if ( defined $format ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $format) 1 0 1
323  
324 1 ▶ if ( ! $self->_get_format_handler( $format ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_get_format_handler($format)) 0 1 1
325 0 throw "Unsupported default format \"$format\"";
326 }
327  
328 1 $self->_set_default_format( $format );
329  
330 }
331  
332 }
333 else {
334 1 $self->_set_default_format( 'html' );
335 }
336  
337 2 $self->SUPER::_setup_config( $args );
338  
339 2 return;
340  
341 }
342  
343  
344 2 1;
345  
346 __END__
« GX/Database.pm ↑ Summary GX/Engine/FCGI.pm »