summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2014-09-18 17:07:41 +0200
committerFlorian Pritz <bluewind@xinu.at>2014-09-20 19:31:42 +0200
commite48be5ef7c6b73e51ab297c55950180261b6c061 (patch)
treea2dd6987158b833c900332f32d4e92d9023444d6
parentd2c309aee8189a5d6c2a3fcb0a05ea694d7b646e (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>
-rw-r--r--application/errors/error_general.php5
-rw-r--r--index.php55
2 files changed, 59 insertions, 1 deletions
diff --git a/application/errors/error_general.php b/application/errors/error_general.php
index be495e4f6..844dfb74d 100644
--- a/application/errors/error_general.php
+++ b/application/errors/error_general.php
@@ -43,6 +43,9 @@ if (class_exists("CI_Controller") && !isset($GLOBALS["is_error_page"])) {
<?php
include 'application/views/footer.php';
+} elseif (php_sapi_name() === 'cli' OR defined('STDIN')) {
+ echo "$heading\n";
+ echo str_replace("<br>", "\n", $message);
} else {
// default CI error page
?>
@@ -96,7 +99,7 @@ code {
-webkit-box-shadow: 0 0 8px #D0D0D0;
}
-p {
+p, div {
margin: 12px 15px 12px 15px;
}
</style>
diff --git a/index.php b/index.php
index 2bf26537b..00ab370c3 100644
--- a/index.php
+++ b/index.php
@@ -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
* --------------------------------------------------------------------