diff --git a/core/Debug.php b/core/Debug.php index 9f1258f07..43e9abb4a 100644 --- a/core/Debug.php +++ b/core/Debug.php @@ -6,7 +6,37 @@ */ /** - * Supports debugging and core error handling via static methods. + * Supports debugging and core error handling. + * + * Attaches custom methods to the default + * error handling hooks in PHP. Currently, three levels + * of error are supported: + * + * - Notice + * - Warning + * - Error + * + * Notice level errors are currently unsupported, and will be passed + * directly to the normal PHP error output. + * + * Uncaught exceptions are currently passed to the debug + * reporter as standard PHP errors. + * + * There are four different types of error handler supported by the + * Debug class: + * + * - Friendly + * - Fatal + * - Logger + * - Emailer + * + * Currently, only Friendly, Fatal, and Emailer handlers are implemented. + * + * @todo port header/footer wrapping code to external reporter class + * @todo add support for user defined config: Debug::die_on_notice(true | false) + * @todo add appropriate handling for E_NOTICE and E_USER_NOTICE levels + * @todo better way of figuring out the error context to display in highlighted source + * @todo implement error logger handler * * @package sapphire * @subpackage core @@ -39,7 +69,7 @@ class Debug { if(Director::is_ajax()) echo "Debug ($caller[class]$caller[type]$caller[function]() in line $caller[line] of " . basename($caller['file']) . ")\n"; else - echo "
\n
\n

Debug ($caller[class]$caller[type]$caller[function]() \nin line $caller[line] \nof " . basename($caller['file']) . ")\n

\n"; + echo "
\n
\n

Debug ($caller[class]$caller[type]$caller[function]() \nin line $caller[line] \nof " . basename($caller['file']) . ")\n

\n"; } echo Debug::text($val); @@ -65,6 +95,12 @@ class Debug { die(); } } + + static function dump($val) { + echo '
';
+		print_r($val);
+		echo '
'; + } static function text($val) { if(is_object($val)) { @@ -123,7 +159,8 @@ class Debug { * @todo can this be moved into loadErrorHandlers? */ static function loadFatalErrorHandler() { - set_error_handler('errorHandler', (E_ALL ^ E_NOTICE) ^ E_USER_NOTICE); + //set_error_handler('errorHandler', (E_ALL ^ E_NOTICE) ^ E_USER_NOTICE); + set_error_handler('errorHandler', E_ALL); set_exception_handler('exceptionHandler'); } @@ -164,22 +201,50 @@ class Debug { static function showError($errno, $errstr, $errfile, $errline, $errcontext) { if(!headers_sent()) header("HTTP/1.0 500 Internal server error"); - if(Director::is_ajax()) { echo "ERROR:Error $errno: $errstr\n At l$errline in $errfile\n"; Debug::backtrace(); - } else { - echo "
\n"; - echo "

FATAL ERROR: $errstr
\n At line $errline in $errfile
\n
\n

\n"; - + echo ''. $_SERVER['REQUEST_METHOD'] . ' ' .$_SERVER['REQUEST_URI'] .''; + echo ''; + echo ''; + echo '
'; + echo '
'; + echo "

" . strip_tags($errstr) . "

"; + echo "

{$_SERVER['REQUEST_METHOD']} {$_SERVER['REQUEST_URI']}

"; + echo "

Line $errline in $errfile

"; + echo '
'; + echo '

Source

'; + $lines = file($errfile); + $offset = $errline-10; + $lines = array_slice($lines, $offset, 16); + echo '
';
+			$offset++;
+			foreach($lines as $line) {
+				$line = htmlentities($line);
+				if ($offset == $errline) {
+					echo "$offset $line";
+				} else {
+					echo "$offset $line";
+				}
+				$offset++;
+			}
+			echo '

Trace

'; Debug::backtrace(); - //Debug::show(debug_backtrace()); - - echo "

Context

\n"; - Debug::show($errcontext); - + echo '
'; echo "
\n"; + echo ""; + die(); } } @@ -275,18 +340,24 @@ class Debug { array_shift($bt); } - $result = ""; + $result = ""; if ($returnVal) { return $result; @@ -384,11 +455,31 @@ function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) { Debug::fatalHandler($errno, $errstr, $errfile, $errline, $errcontext); break; + case E_NOTICE: case E_WARNING: case E_CORE_WARNING: case E_USER_WARNING: Debug::warningHandler($errno, $errstr, $errfile, $errline, $errcontext); break; + } } + +/** + * Interface for rendering an error report. + * + * @todo decide whether to subclass this to display email and debug dumps + */ +class DebugReporter { + + function writeHeader() { + + } + + function writeFooter() { + + } + +} + ?>