blib/lib/ASP4/ErrorHandler.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 49 CC 2 SCAR 10.0
CC
2
Cov
0%
print_error
6.0
run
2.0
send_error
2.0
CRAP 2.7
1  
2 package ASP4::ErrorHandler;
3  
4 use strict;
5 use warnings 'all';
6 use base 'ASP4::HTTPHandler';
7 use vars __PACKAGE__->VARS;
8 use MIME::Base64;
9 use Data::Dumper;
10  
11  
12 sub run
13 {
14 my ($s, $context) = @_;
15
16 my $error = $Stash->{error};
17 $s->print_error( $error );
18 $s->send_error( $error );
19 }# end run()
20  
21  
22 sub print_error
23 {
24 my ($s, $error) = @_;
25
26 $Response->ContentType('text/html');
27  
28 if( $ENV{HTTP_HOST} eq 'localhost' )
29 {
30 $Response->Write( Dumper(\%$error) );
31 }
32 else
33 {
34 $Response->Write( $s->error_html( $error ) );
35 }# end if()
36
37 $Response->Flush;
38 }# end print_error()
39  
40  
41 sub send_error
42 {
43 my ($s, $error) = @_;
44
45 $Server->Mail(
46 To => $Config->errors->mail_errors_to,
47 From => $Config->errors->mail_errors_from,
48 Subject => "ASP4: Error in @{[ $ENV{HTTP_HOST} ]}@{[ $ENV{REQUEST_URI} ]}",
49 'content-type' => 'text/html',
50 'content-transfer-encoding' => 'base64',
51 Message => encode_base64( $s->error_html($error) ),
52 smtp => $Config->errors->smtp_server,
53 );
54 }# end send_error()
55  
56  
57 sub error_html
58 {
59 my ($s, $error) = @_;
60
61 my $msg = <<"ERROR";
62 <!DOCTYPE html>
63 <html>
64 <head><title>500 Server Error</title>
65 <meta charset="utf-8" />
66 <style type="text/css">
67 HTML,BODY {
68 background-color: #FFFFFF;
69 }
70 HTML,BODY,P,DIV {
71 font-family: Arial, Helvetica, Sans-Serif;
72 }
73 HTML,BODY,P,PRE,DIV {
74 font-size: 12px;
75 }
76 H1 {
77 font-size: 50px;
78 font-weight: bold;
79 }
80 PRE {
81 padding-right: 10px;
82 line-height: 16px;
83 }
84 .code {
85 margin-top: 20px;
86 margin-left: 15px;
87 width: 95%;
88 padding: 10px;
89 overflow: auto;
90 border: solid 1px #808080;
91 background-color: #FFFFCC;
92 }
93 .clear {
94 clear: both;
95 }
96 .label {
97 text-align: right;
98 padding-right: 5px;
99 float: left;
100 width: 80px;
101 font-weight: bold;
102 }
103 .info {
104 float: left;
105 color: #CC0000;
106 }
107 </style>
108 <body>
109 <h1>500 Server Error</h1>
110 <h2>@{[ $error->message ]}</h2>
111 <div><div class="label">URL:</div> <div class="info"><code>@{[ $ENV{HTTP_HOST} ]}@{[ $ENV{REQUEST_URI} ]}</code></div></div>
112 <div class="clear"></div>
113 <div><div class="label">File:</div> <div class="info"><code>@{[ $error->file ]}</code></div></div>
114 <div class="clear"></div>
115 <div><div class="label">Line:</div> <div class="info">@{[ $error->line ]}</div></div>
116 <div class="clear"></div>
117 <div><div class="label">Time:</div> <div class="info">@{[ HTTP::Date::time2iso() ]}</div></div>
118 <div class="clear"></div>
119 <h2>Stacktrace follows below:</h2>
120 <div class="code"><pre>@{[ $error->stacktrace ]}</pre></div>
121 <div class="clear"></div>
122 <h3>\%ENV</h3>
123 <div class="code"><pre>
124 HTTP_REFERER: '@{[ $Server->HTMLEncode($ENV{HTTP_REFERER}||'NONE') ]}'
125 HTTP_COOKIE: '@{[ $Server->HTMLEncode($ENV{HTTP_COOKIE}||'NONE') ]}'
126 HTTP_USER_AGENT: '@{[ $Server->HTMLEncode($ENV{HTTP_USER_AGENT}||'NONE') ]}'
127 REMOTE_ADDR: '@{[ $Server->HTMLEncode($ENV{REMOTE_ADDR}||'NONE') ]}'
128 </pre></div>
129 <h3>\$Form</h3>
130 <div class="code"><pre>@{[ Dumper($Form) ]}</pre></div>
131 <div class="clear"></div>
132 <div style="display: none;">
133 </body>
134 </html>
135 ERROR
136
137 return $msg;
138 }# end error_html()
139  
140  
141 1;# return true:
142  
143 =pod
144  
145 =head1 NAME
146  
147 ASP4::ErrorHandler - Default fatal error handler
148  
149 =head1 SYNOPSIS
150  
151 In your C<asp4-config.json>:
152  
153 ...
154 "errors": {
155 "error_handler": "ASP4::ErrorHandler",
156 "mail_errors_to": "you@server.com",
157 "mail_errors_from": "root@localhost",
158 "smtp_server": "localhost"
159 },
160 ...
161  
162 =head1 DESCRIPTION
163  
164 This class provides a default error handler which does the following:
165  
166 1) Makes a simple HTML page and prints it to the browser, telling the user
167 that an error has just occurred.
168  
169 2) Sends that same HTML to the email address specified in the config, using the
170 SMTP server also specified in the config. The email subject will look something like:
171  
172 ASP4: Error in your-site.com/index.asp
173  
174 =head1 SUBCLASSING
175  
176 To subclass C<ASP4::ErrorHandler> you must do the following:
177  
178 package My::ErrorHandler;
179
180 use strict;
181 use warnings 'all';
182 use base 'ASP4::ErrorHandler';
183 use vars __PACKAGE__->VARS;
184
185 sub run {
186 my ($s, $context) = @_;
187
188 my $error = $Stash->{error};
189
190 # $error is an ASP4::Error object.
191
192 # Do something here about the error.
193 $s->print_error( $error );
194 $s->send_error( $error );
195 }
196
197 1;# return true:
198  
199 =head1 METHODS
200  
201 =head2 error_html( $error )
202  
203 Returns a string of html suitable for printing to the browser or emailing.
204  
205 =head2 print_error( $error )
206  
207 Prints the error html to the browser.
208  
209 =head2 send_error( $error )
210  
211 Sends the error html to the email address specified in the config, using C<<$Server->Mail(...)>>
212 and the smtp server specified in the config.
213  
214 =head1 BUGS
215  
216 It's possible that some bugs have found their way into this release.
217  
218 Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports.
219  
220 =head1 HOMEPAGE
221  
222 Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples
223 of ASP4 in action.
224  
225 =head1 AUTHOR
226  
227 John Drago <jdrago_999@yahoo.com>
228  
229 =head1 COPYRIGHT
230  
231 Copyright 2008 John Drago. All rights reserved.
232  
233 =head1 LICENSE
234  
235 This software is Free software and is licensed under the same terms as perl itself.
236  
237 =cut
238