Silki/Plugin/ErrorHandling.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 149 CC 13 SCAR 29.0
CC
13
Cov
0%
finalize
42.0
_finalize_error_with_debug
20.0
_log_error
12.0
CRAP 18.2
1 package Silki::Plugin::ErrorHandling;
2 {
3 $Silki::Plugin::ErrorHandling::VERSION = '0.29';
4 }
5  
6 use strict;
7 use warnings;
8  
9 use Data::Dump qw/dump/;
10 use HTML::Entities qw( encode_entities );
11 use HTTP::Status qw( RC_NOT_FOUND RC_INTERNAL_SERVER_ERROR );
12 use Silki::JSON;
13  
14 # I'd really rather _not_ copy this whole thing in here, but it's the
15 # only way to override how errors are logged. I have to monkey-patch
16 # rather than subclassing or else NEXT::finalize() ends up calling the
17 # finalize in Catalyst itself before calling finalize() for other
18 # plugins (a mess!).
19 {
20  
21 package
22 Catalyst;
23  
24 no warnings 'redefine';
25  
26 sub finalize {
27 my $self = shift;
28  
29 for my $error ( @{ $self->error } ) {
30 $self->_log_error($error);
31 }
32  
33 # Allow engine to handle finalize flow (for POE)
34 if ( $self->engine->can('finalize') ) {
35 $self->engine->finalize($self);
36 }
37 else {
38  
39 $self->finalize_uploads;
40  
41 # Error
42 if ( $#{ $self->error } >= 0 ) {
43 $self->finalize_error;
44 }
45  
46 $self->finalize_headers;
47  
48 # HEAD request
49 if ( $self->request->method eq 'HEAD' ) {
50 $self->response->body('');
51 }
52  
53 $self->finalize_body;
54 }
55  
56 if ( $self->use_stats ) {
57 my $elapsed = sprintf '%f', $self->stats->elapsed;
58 my $av = $elapsed == 0 ? '??' : sprintf '%.3f', 1 / $elapsed;
59 $self->log->info( "Request took ${elapsed}s ($av/s)\n"
60 . $self->stats->report
61 . "\n" );
62 }
63  
64 return $self->response->status;
65 }
66 }
67  
68 sub _log_error {
69 my $self = shift;
70 my $error = shift;
71  
72 # XXX - change this later to log to the apache log?
73 # if ( $error =~ /unknown resource/ )
74  
75 my %error = ( uri => $self->request()->uri() . '' );
76  
77 if ( my $user = $self->user() ) {
78 $error{user} = $user->username();
79 $error{user} .= q{ - } . $user->user_id()
80 if $user->user_id();
81 }
82  
83 $error{error} = $error . '';
84  
85 $self->log()->error( Silki::JSON->Encode( \%error ) );
86 }
87  
88 sub finalize_error {
89 my $self = shift;
90  
91 my @errors = @{ $self->error() || [] };
92  
93 my $status
94 = ( grep {/unknown resource|no default/i} @errors )
95 ? RC_NOT_FOUND
96 : RC_INTERNAL_SERVER_ERROR;
97  
98 $self->response()->status($status);
99  
100 if ( $self->debug() ) {
101 $self->_finalize_error_with_debug( $self, @_ );
102 return;
103 }
104  
105 $self->error( [] );
106  
107 $self->response()->content_type('text/html; charset=utf-8');
108 $self->response()->body( $self->subreq("/error/$status") );
109 }
110  
111 # copied from Catalyst::Engine->finalize_error just so we can set the
112 # fucking status. GRRRR!
113 sub _finalize_error_with_debug {
114 my $self = shift;
115 my $c = shift;
116  
117 $c->res->content_type('text/html; charset=utf-8');
118 my $name = $c->config->{name} || join( ' ', split( '::', ref $c ) );
119  
120 my ( $title, $error, $infos );
121 if ( $c->debug ) {
122  
123 # For pretty dumps
124 $error = join '', map {
125 '<p><code class="error">'
126 . encode_entities($_)
127 . '</code></p>'
128 } @{ $c->error };
129 $error ||= 'No output';
130 $error = qq{<pre wrap="">$error</pre>};
131 $title = $name = "$name on Catalyst $Catalyst::VERSION";
132 $name = "<h1>$name</h1>";
133  
134 # Don't show context in the dump
135 delete $c->req->{_context};
136 delete $c->res->{_context};
137  
138 # Don't show body parser in the dump
139 delete $c->req->{_body};
140  
141 # Don't show response header state in dump
142 delete $c->res->{_finalized_headers};
143  
144 my @infos;
145 my $i = 0;
146 for my $dump ( $c->dump_these ) {
147 my $name = $dump->[0];
148  
149 # stored in there for classes with an anon metaclass.
150 delete $dump->[1]{__MOP__} if ref $dump->[1];
151  
152 my $value = encode_entities( dump( $dump->[1] ) );
153 push @infos, sprintf <<"EOF", $name, $value;
154 <h2><a href="#" onclick="toggleDump('dump_$i'); return false">%s</a></h2>
155 <div id="dump_$i">
156 <pre wrap="">%s</pre>
157 </div>
158 EOF
159 $i++;
160 }
161 $infos = join "\n", @infos;
162 }
163 else {
164 $title = $name;
165 $error = '';
166 $infos = <<"";
167 <pre>
168 (en) Please come back later
169 (fr) SVP veuillez revenir plus tard
170 (de) Bitte versuchen sie es spaeter nocheinmal
171 (at) Konnten's bitt'schoen spaeter nochmal reinschauen
172 (no) Vennligst prov igjen senere
173 (dk) Venligst prov igen senere
174 (pl) Prosze sprobowac pozniej
175 (pt) Por favor volte mais tarde
176 </pre>
177  
178 $name = '';
179 }
180 $c->res->body( <<"" );
181 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
182 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
183 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
184 <head>
185 <meta http-equiv="Content-Language" content="en" />
186 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
187 <title>$title</title>
188 <script type="text/javascript">
189 <!--
190 function toggleDump (dumpElement) {
191 var e = document.getElementById( dumpElement );
192 if (e.style.display == "none") {
193 e.style.display = "";
194 }
195 else {
196 e.style.display = "none";
197 }
198 }
199 -->
200 </script>
201 <style type="text/css">
202 body {
203 font-family: "Bitstream Vera Sans", "Trebuchet MS", Verdana,
204 Tahoma, Arial, helvetica, sans-serif;
205 color: #333;
206 background-color: #eee;
207 margin: 0px;
208 padding: 0px;
209 }
210 :link, :link:hover, :visited, :visited:hover {
211 color: #000;
212 }
213 div.box {
214 position: relative;
215 background-color: #ccc;
216 border: 1px solid #aaa;
217 padding: 4px;
218 margin: 10px;
219 }
220 div.error {
221 background-color: #cce;
222 border: 1px solid #755;
223 padding: 8px;
224 margin: 4px;
225 margin-bottom: 10px;
226 }
227 div.infos {
228 background-color: #eee;
229 border: 1px solid #575;
230 padding: 8px;
231 margin: 4px;
232 margin-bottom: 10px;
233 }
234 div.name {
235 background-color: #cce;
236 border: 1px solid #557;
237 padding: 8px;
238 margin: 4px;
239 }
240 code.error {
241 display: block;
242 margin: 1em 0;
243 overflow: auto;
244 }
245 div.name h1, div.error p {
246 margin: 0;
247 }
248 h2 {
249 margin-top: 0;
250 margin-bottom: 10px;
251 font-size: medium;
252 font-weight: bold;
253 text-decoration: underline;
254 }
255 h1 {
256 font-size: medium;
257 font-weight: normal;
258 }
259 /* from http://users.tkk.fi/~tkarvine/linux/doc/pre-wrap/pre-wrap-css3-mozilla-opera-ie.html */
260 /* Browser specific (not valid) styles to make preformatted text wrap */
261 pre {
262 white-space: pre-wrap; /* css-3 */
263 white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
264 white-space: -pre-wrap; /* Opera 4-6 */
265 white-space: -o-pre-wrap; /* Opera 7 */
266 word-wrap: break-word; /* Internet Explorer 5.5+ */
267 }
268 </style>
269 </head>
270 <body>
271 <div class="box">
272 <div class="error">$error</div>
273 <div class="infos">$infos</div>
274 <div class="name">$name</div>
275 </div>
276 </body>
277 </html>
278  
279  
280 # Trick IE
281 $c->res->{body} .= ( ' ' x 512 );
282  
283 $c->res->status(500)
284 unless $c->res->status;
285 }
286  
287 1;
288  
289 # ABSTRACT: An uber-hack plugin to override Catalyst's error output and add better error logging
290  
291 __END__