| 1 | |||
| 2 | package Apache2::ASP::ErrorHandler; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings 'all'; | ||
| 6 | use base 'Apache2::ASP::HTTPHandler'; | ||
| 7 | use vars __PACKAGE__->VARS; | ||
| 8 | use MIME::Base64; | ||
| 9 | |||
| 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 id="code"><pre>@{[ $error->{stacktrace} ]}</pre></div> | ||
| 76 | <div style="display: none;"> | ||
| 77 | </body> | ||
| 78 | </html> | ||
| 79 | ERROR | ||
| 80 | |||
| 81 | $Response->Write( $msg ); | ||
| 82 | $Server->Mail( | ||
| 83 | To => $Config->errors->mail_errors_to, | ||
| 84 | From => $Config->errors->mail_errors_from, | ||
| 85 | Subject => "Apache2::ASP: Error in @{[ $ENV{HTTP_HOST} ]}@{[ $context->r->uri ]}", | ||
| 86 | 'content-type' => 'text/html', | ||
| 87 | 'content-transfer-encoding' => 'base64', | ||
| 88 | Message => encode_base64( $msg ), | ||
| 89 | smtp => $Config->errors->smtp_server, | ||
| 90 | ); | ||
| 91 | |||
| 92 | }# end run() | ||
| 93 | |||
| 94 | 1;# return true: | ||
| 95 | |||
| 96 | =pod | ||
| 97 | |||
| 98 | =head1 NAME | ||
| 99 | |||
| 100 | Apache2::ASP::ErrorHandler - Default fatal error handler | ||
| 101 | |||
| 102 | =head1 SYNOPSIS | ||
| 103 | |||
| 104 | In your C<apache2-asp-config.xml>: | ||
| 105 | |||
| 106 | <?xml version="1.0" ?> | ||
| 107 | <config> | ||
| 108 | ... | ||
| 109 | <errors> | ||
| 110 | <error_handler>Apache2::ASP::ErrorHandler</error_handler> | ||
| 111 | <mail_errors_to>you@your-site.com</mail_errors_to> | ||
| 112 | <mail_errors_from>root@localhost</mail_errors_from> | ||
| 113 | <smtp_server>localhost</smtp_server> | ||
| 114 | </errors> | ||
| 115 | ... | ||
| 116 | </config> | ||
| 117 | |||
| 118 | =head1 DESCRIPTION | ||
| 119 | |||
| 120 | This class provides a default error handler which does the following: | ||
| 121 | |||
| 122 | 1) Makes a simple HTML page and prints it to the browser, telling the user | ||
| 123 | that an error has just occurred. | ||
| 124 | |||
| 125 | 2) Sends that same HTML to the email address specified in the config, using the | ||
| 126 | SMTP server also specified in the config. The email subject will look something like: | ||
| 127 | |||
| 128 | Apache2::ASP: Error in your-site.com/index.asp | ||
| 129 | |||
| 130 | =head1 SUBCLASSING | ||
| 131 | |||
| 132 | To subclass C<Apache2::ASP::ErrorHandler> you must do the following: | ||
| 133 | |||
| 134 | package My::ErrorHandler; | ||
| 135 | |||
| 136 | use strict; | ||
| 137 | use warnings 'all'; | ||
| 138 | use base 'Apache2::ASP::ErrorHandler'; | ||
| 139 | use vars __PACKAGE__->VARS; | ||
| 140 | |||
| 141 | sub run { | ||
| 142 | my ($s, $context) = @_; | ||
| 143 | |||
| 144 | my $error = $Stash->{error}; | ||
| 145 | |||
| 146 | # $error looks like this: | ||
| 147 | $VAR1 = { | ||
| 148 | title => 'Cannot call execute with a reference', | ||
| 149 | file => '/tmp/PAGE_CACHE/mysite/index_asp.pm', | ||
| 150 | line => 45, | ||
| 151 | stacktrace => # Output from Carp::confess, | ||
| 152 | }; | ||
| 153 | |||
| 154 | # Do something here about the error. | ||
| 155 | } | ||
| 156 | |||
| 157 | 1;# return true: | ||
| 158 | |||
| 159 | =head1 BUGS | ||
| 160 | |||
| 161 | It's possible that some bugs have found their way into this release. | ||
| 162 | |||
| 163 | Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Apache2-ASP> to submit bug reports. | ||
| 164 | |||
| 165 | =head1 HOMEPAGE | ||
| 166 | |||
| 167 | Please visit the Apache2::ASP homepage at L<http://www.devstack.com/> to see examples | ||
| 168 | of Apache2::ASP in action. | ||
| 169 | |||
| 170 | =head1 AUTHOR | ||
| 171 | |||
| 172 | John Drago <jdrago_999@yahoo.com> | ||
| 173 | |||
| 174 | =head1 COPYRIGHT | ||
| 175 | |||
| 176 | Copyright 2008 John Drago. All rights reserved. | ||
| 177 | |||
| 178 | =head1 LICENSE | ||
| 179 | |||
| 180 | This software is Free software and is licensed under the same terms as perl itself. | ||
| 181 | |||
| 182 | =cut | ||
| 183 |