diff options
author | Florian Pritz <bluewind@xinu.at> | 2014-09-18 17:07:41 +0200 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2014-09-20 19:31:42 +0200 |
commit | e48be5ef7c6b73e51ab297c55950180261b6c061 (patch) | |
tree | a2dd6987158b833c900332f32d4e92d9023444d6 /index.php | |
parent | d2c309aee8189a5d6c2a3fcb0a05ea694d7b646e (diff) |
Improve handling of PHP errors
This converts any error (including INFO, NOTICE and those hidden with @)
to an exception and displays a nice error page for uncaught exceptions.
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'index.php')
-rw-r--r-- | index.php | 55 |
1 files changed, 55 insertions, 0 deletions
@@ -196,6 +196,61 @@ if (false && defined('ENVIRONMENT')) } /* + * Custom error handling + */ +/* CI uses that name for it's error handling function. It misleading, but + * whatever. If I don't use it the framework will override my handler later. + */ +function _exception_handler($errno, $errstr, $errfile, $errline) +{ + if (!(error_reporting() & $errno)) { + // This error code is not included in error_reporting + return; + } + throw new ErrorException($errstr, 0, $errno, $errfile, $errline); +} +set_error_handler("_exception_handler"); + +// The actual exception handler +function _actual_exception_handler($e) +{ + $display_errors = in_array(strtolower(ini_get('display_errors')), array('1', 'on', 'true', 'stdout')); + + $GLOBALS["is_error_page"] = true; + $heading = "Internal Server Error"; + $message = "<p>An unhandled error occured.</p>\n"; + if ($display_errors) { + $message .= '<div>'; + $message .= '<b>Fatal error</b>: Uncaught exception '.get_class($e).'<br>'; + $message .= '<b>Message</b>: '.$e->getMessage().'<br>'; + $message .= '<pre>'.(str_replace(FCPATH, "./", $e->getTraceAsString())).'</pre>'; + $message .= 'thrown in <b>'.$e->getFile().'</b> on line <b>'.$e->getLine().'</b><br>'; + $message .= '</div>'; + } else { + $message .="<p>More information can be found in syslog or by enabling display_errors.</p>"; + } + + error_log($e); + + $message = "$message"; + include APPPATH."/errors/error_general.php"; +} +set_exception_handler('_actual_exception_handler'); + +/** + * Checks for a fatal error, work around for set_error_handler not working on fatal errors. + */ +function check_for_fatal() +{ + $error = error_get_last(); + if ($error["type"] == E_ERROR) { + _actual_exception_handler(new ErrorException( + $error["message"], 0, $error["type"], $error["file"], $error["line"])); + } +} +register_shutdown_function("check_for_fatal"); + +/* * -------------------------------------------------------------------- * LOAD THE BOOTSTRAP FILE * -------------------------------------------------------------------- |