I need to handle fatal errors for a production to instead show a static styled 503 Service Unavailable503 Service Unavailable HTML output. This is surely a reasonable approach to "catching fatal errors". This is what I've done:
I have a custom error handling function "error_handler" which will display my "503 service unavailable" HTML page on any E_ERROR, E_USER_ERROR, etc. This will now be called on the shutdown function, catching my fatal error.,
function fatal_error_handler() {
if (@is_array($e = @error_get_last())) {
$code = isset($e['type']) ? $e['type'] : 0;
$msg = isset($e['message']) ? $e['message'] : '';
$file = isset($e['file']) ? $e['file'] : '';
$line = isset($e['line']) ? $e['line'] : '';
if ($code>0)
error_handler($code, $msg, $file, $line);
}
}
set_error_handler("error_handler");
register_shutdown_function('fatal_error_handler');
in my custom error_handler function, if the error is E_ERROR or, E_USER_ERROR, etc. I also call @ob_end_clean();@ob_end_clean();
to empty the buffer, thus removing PHP's "fatal error" message.
Take important note of the strict isset() checking and @@
silencing functions since we dontdon’t want our error_handler scripts to generate any errors.
In still agreeing with keparo, catching fatal errors does defeat the purpose of "FATAL error" so itsit's not really intended for you to do further processing. Do not run any mail() functions in this shutdown process as you will certainly back up the mail server or your inbox. Rather log these occurrences to file and schedule a croncron job to find these error.logerror.log files and mail them to administrators.