Re: Error recovery - fatal errors
| From: | David Harkness | Date: | Mon, 16 May 2011 18:17:46 +0000 |
| Subject: | Re: Error recovery - fatal errors | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-312812@lists.php.net to get a copy of this message | ||
You can register a shutdown function that gets called even in the case of a
fatal error. We use something like this:
public function init() {
register_shutdown_function(array('Bootstrap', 'fatalErrorCatcher'));
...
}
public function fatalErrorCatcher() {
$error = error_get_last();
if( $error && (
$error['type'] === E_ERROR ||
$error['type'] === E_COMPILE_ERROR ||
$error['type'] === E_CORE_ERROR ||
$error['type'] === E_PARSE
)) {
// kill the buffer content, it's broken anyway
while(ob_get_level()) {
ob_end_clean();
}
// log error to a file
...
// issue 500 and dump out a static "site is broken oh noes!"
page
header('HTTP/1.1 500 Internal Server Error');
echo file_get_contents('fail-whale.html');
}
}
In catchFatalError() we check the last error to see if it's truly fatal. Our
general error-handler has already handled all other error types, and this
since function gets called no matter how the PHP process shutsdown, you
don't want to issue a 500 for a successful request. :)
David