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
This commit is contained in:
Mark Rickerby 2008-02-20 21:37:22 +00:00
parent ecdc4d5cbf
commit 6966328cd5

View File

@ -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:<h1>Error</h1><p>The website server has not been able to respond to your request.</p>\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);
}
@ -272,9 +288,11 @@ class Debug {
}
}
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: