From 6966328cd5480882d95697d330b3864011257e67 Mon Sep 17 00:00:00 2001 From: Mark Rickerby Date: Wed, 20 Feb 2008 21:37:22 +0000 Subject: [PATCH] Added a default exception handler. Any uncaught exceptions thrown from application code are now scooped up by the Debug::fatalHandler Still some small problems with displaying stack traces of exceptions because the context array from trigger_error looks quite different from that of Exception::getTrace Also fixed a couple of echo/print bugs in Debug::friendlyError. From the looks of the code there may be more bugs to cleanup here. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@49899 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/Debug.php | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/core/Debug.php b/core/Debug.php index 4c7c5a5db..97079969d 100644 --- a/core/Debug.php +++ b/core/Debug.php @@ -6,19 +6,26 @@ */ /** - * Class of static methods to support debugging. + * Supports debugging and core error handling via static methods. + * * @package sapphire * @subpackage core */ class Debug { /** - * @var $mail_server string Custom mailserver for sending debug mails. + * @var $custom_smtp_server string Custom mailserver for sending mails. */ protected static $custom_smtp_server = ''; + /** + * @var $send_errors_to string Email address to send error notifications + */ protected static $send_errors_to; + /** + * @var $send_warnings_to string Email address to send warning notifications + */ protected static $send_warnings_to; /** @@ -41,6 +48,10 @@ class Debug { } } + + /** + * Emails the contents of the output buffer + */ static function mailBuffer( $email, $subject ) { mail( $email, $subject, ob_get_contents() ); ob_end_clean(); @@ -101,13 +112,19 @@ class Debug { /** * Load an error handler + * + * @todo why does this delegate to loadFatalErrorHandler? */ static function loadErrorHandlers() { Debug::loadFatalErrorHandler(); } + /** + * @todo can this be moved into loadErrorHandlers? + */ static function loadFatalErrorHandler() { set_error_handler('errorHandler', (E_ALL ^ E_NOTICE) ^ E_USER_NOTICE); + set_exception_handler('exceptionHandler'); } static function warningHandler($errno, $errstr, $errfile, $errline, $errcontext) { @@ -138,7 +155,6 @@ class Debug { } else { if(file_exists('../assets/error-500.html')) { - echo "ERROR:"; include('../assets/error-500.html'); } else { echo "ERROR:

Error

The website server has not been able to respond to your request.

\n"; @@ -254,7 +270,7 @@ class Debug { $bt = debug_backtrace(); // Ingore functions that are plumbing of the error handler - $ignoredFunctions = array('Debug::emailError','Debug::warningHandler','Debug::fatalHandler','errorHandler','Debug::showError','Debug::backtrace'); + $ignoredFunctions = array('Debug::emailError','Debug::warningHandler','Debug::fatalHandler','errorHandler','Debug::showError','Debug::backtrace', 'exceptionHandler'); while( $bt && in_array(self::full_func_name($bt[0]), $ignoredFunctions) ) { array_shift($bt); } @@ -271,10 +287,12 @@ class Debug { $result .= "

\n"; } } - - echo $result; - if($returnVal) return $result; - else echo $result; + + if ($returnVal) { + return $result; + } else { + echo $result; + } } /** @@ -348,6 +366,16 @@ class Debug { } } +function exceptionHandler($exception) { + $errno = E_USER_ERROR; + $type = get_class($exception); + $message = "Uncaught " . $type . ": " . $exception->getMessage(); + $file = $exception->getFile(); + $line = $exception->getLine(); + $context = $exception->getTrace(); + Debug::fatalHandler($errno, $message, $file, $line, $context); +} + function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) { switch($errno) { case E_ERROR: