| 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 | |||
| 18 | my $msg = <<"ERROR"; | ||
| 19 | <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> | ||
| 20 | <html> | ||
| 21 | <head><title>500 Server Error</title> | ||
| 22 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | ||
| 23 | <style type="text/css"> | ||
| 24 | HTML,BODY { | ||
| 25 | background-color: #FFFFFF; | ||
| 26 | } | ||
| 27 | HTML,BODY,P,DIV { | ||
| 28 | font-family: Arial, Helvetica, Sans-Serif; | ||
| 29 | } | ||
| 30 | HTML,BODY,P,PRE,DIV { | ||
| 31 | font-size: 12px; | ||
| 32 | } | ||
| 33 | H1 { | ||
| 34 | font-size: 50px; | ||
| 35 | font-weight: bold; | ||
| 36 | } | ||
| 37 | PRE { | ||
| 38 | padding-right: 10px; | ||
| 39 | line-height: 16px; | ||
| 40 | } | ||
| 41 | .code { | ||
| 42 | margin-top: 20px; | ||
| 43 | margin-left: 15px; | ||
| 44 | width: 95%; | ||
| 45 | padding: 10px; | ||
| 46 | overflow: auto; | ||
| 47 | border: solid 1px #808080; | ||
| 48 | background-color: #FFFFCC; | ||
| 49 | } | ||
| 50 | .clear { | ||
| 51 | clear: both; | ||
| 52 | } | ||
| 53 | .label { | ||
| 54 | text-align: right; | ||
| 55 | padding-right: 5px; | ||
| 56 | float: left; | ||
| 57 | width: 80px; | ||
| 58 | font-weight: bold; | ||
| 59 | } | ||
| 60 | .info { | ||
| 61 | float: left; | ||
| 62 | color: #CC0000; | ||
| 63 | } | ||
| 64 | </style> | ||
| 65 | <body> | ||
| 66 | <h1>500 Server Error</h1> | ||
| 67 | <h2>@{[ $error->{title} ]}</h2> | ||
| 68 | <div><div class="label">File:</div> <div class="info"><code>@{[ $error->{file} ]}</code></div></div> | ||
| 69 | <div class="clear"></div> | ||
| 70 | <div><div class="label">Line:</div> <div class="info">@{[ $error->{line} ]}</div></div> | ||
| 71 | <div class="clear"></div> | ||
| 72 | <div><div class="label">Time:</div> <div class="info">@{[ HTTP::Date::time2iso() ]}</div></div> | ||
| 73 | <div class="clear"></div> | ||
| 74 | <h2>Stacktrace follows below:</h2> | ||
| 75 | <div class="code"><pre>@{[ $error->{stacktrace} ]}</pre></div> | ||
| 76 | <div class="clear"></div> | ||
| 77 | <h3>\%ENV</h3> | ||
| 78 | <div class="code"><pre> | ||
| 79 | HTTP_REFERER: '@{[ $Server->HTMLEncode($ENV{HTTP_REFERER}) ]}' | ||
| 80 | HTTP_COOKIE: '@{[ $Server->HTMLEncode($ENV{HTTP_COOKIE}) ]}' | ||
| 81 | HTTP_USER_AGENT: '@{[ $Server->HTMLEncode($ENV{HTTP_USER_AGENT}) ]}' | ||
| 82 | REMOTE_ADDR: '@{[ $Server->HTMLEncode($ENV{REMOTE_ADDR}) ]}' | ||
| 83 | </pre></div> | ||
| 84 | <h3>\$Form</h3> | ||
| 85 | <div class="code"><pre>@{[ Dumper($Form) ]}</pre></div> | ||
| 86 | <div class="clear"></div> | ||
| 87 | <div style="display: none;"> | ||
| 88 | </body> | ||
| 89 | </html> | ||
| 90 | ERROR | ||
| 91 | |||
| 92 | $Response->Write( $msg ); | ||
| 93 | $Server->Mail( | ||
| 94 | To => $Config->errors->mail_errors_to, | ||
| 95 | From => $Config->errors->mail_errors_from, | ||
| 96 | Subject => "ASP4: Error in @{[ $ENV{HTTP_HOST} ]}@{[ $context->r->uri ]}", | ||
| 97 | 'content-type' => 'text/html', | ||
| 98 | 'content-transfer-encoding' => 'base64', | ||
| 99 | Message => encode_base64( $msg ), | ||
| 100 | smtp => $Config->errors->smtp_server, | ||
| 101 | ); | ||
| 102 | |||
| 103 | }# end run() | ||
| 104 | |||
| 105 | 1;# return true: | ||
| 106 | |||
| 107 | =pod | ||
| 108 | |||
| 109 | =head1 NAME | ||
| 110 | |||
| 111 | ASP4::ErrorHandler - Default fatal error handler | ||
| 112 | |||
| 113 | =head1 SYNOPSIS | ||
| 114 | |||
| 115 | In your C<asp4-config.json>: | ||
| 116 | |||
| 117 | ... | ||
| 118 | "errors": { | ||
| 119 | "error_handler": "ASP4::ErrorHandler", | ||
| 120 | "mail_errors_to": "you@server.com", | ||
| 121 | "mail_errors_from": "root@localhost", | ||
| 122 | "smtp_server": "localhost" | ||
| 123 | }, | ||
| 124 | ... | ||
| 125 | |||
| 126 | =head1 DESCRIPTION | ||
| 127 | |||
| 128 | This class provides a default error handler which does the following: | ||
| 129 | |||
| 130 | 1) Makes a simple HTML page and prints it to the browser, telling the user | ||
| 131 | that an error has just occurred. | ||
| 132 | |||
| 133 | 2) Sends that same HTML to the email address specified in the config, using the | ||
| 134 | SMTP server also specified in the config. The email subject will look something like: | ||
| 135 | |||
| 136 | ASP4: Error in your-site.com/index.asp | ||
| 137 | |||
| 138 | =head1 SUBCLASSING | ||
| 139 | |||
| 140 | To subclass C<ASP4::ErrorHandler> you must do the following: | ||
| 141 | |||
| 142 | package My::ErrorHandler; | ||
| 143 | |||
| 144 | use strict; | ||
| 145 | use warnings 'all'; | ||
| 146 | use base 'ASP4::ErrorHandler'; | ||
| 147 | use vars __PACKAGE__->VARS; | ||
| 148 | |||
| 149 | sub run { | ||
| 150 | my ($s, $context) = @_; | ||
| 151 | |||
| 152 | my $error = $Stash->{error}; | ||
| 153 | |||
| 154 | # $error looks like this: | ||
| 155 | $VAR1 = { | ||
| 156 | title => 'Cannot call execute with a reference', | ||
| 157 | file => '/tmp/PAGE_CACHE/mysite/index_asp.pm', | ||
| 158 | line => 45, | ||
| 159 | stacktrace => # Output from Carp::confess, | ||
| 160 | }; | ||
| 161 | |||
| 162 | # Do something here about the error. | ||
| 163 | } | ||
| 164 | |||
| 165 | 1;# return true: | ||
| 166 | |||
| 167 | =head1 BUGS | ||
| 168 | |||
| 169 | It's possible that some bugs have found their way into this release. | ||
| 170 | |||
| 171 | Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports. | ||
| 172 | |||
| 173 | =head1 HOMEPAGE | ||
| 174 | |||
| 175 | Please visit the ASP4 homepage at L<http://www.devstack.com/> to see examples | ||
| 176 | of ASP4 in action. | ||
| 177 | |||
| 178 | =head1 AUTHOR | ||
| 179 | |||
| 180 | John Drago <jdrago_999@yahoo.com> | ||
| 181 | |||
| 182 | =head1 COPYRIGHT | ||
| 183 | |||
| 184 | Copyright 2008 John Drago. All rights reserved. | ||
| 185 | |||
| 186 | =head1 LICENSE | ||
| 187 | |||
| 188 | This software is Free software and is licensed under the same terms as perl itself. | ||
| 189 | |||
| 190 | =cut | ||
| 191 |