refactoring error handling.

This commit is contained in:
Taylor Otwell
2011-11-10 22:46:01 -06:00
parent 0dc43490a6
commit 0a2c61ec29
4 changed files with 99 additions and 136 deletions

View File

@@ -8,14 +8,90 @@
require 'bootstrap/core.php';
/**
* Register the framework error handling methods and set the error
* reporting levels. This file will register handlers for exceptions,
* errors, and the shutdown event.
* Register the default timezone for the application. This will be
* the default timezone used by all date / timezone functions in
* the entire application.
*/
require SYS_PATH.'bootstrap/errors'.EXT;
date_default_timezone_set(Config::$items['application']['timezone']);
/**
* Create the exception handler function. All of the error handlers
* registered by the framework call this closure to avoid duplicate
* code. This Closure will determine if the logging Closure should
* be called, and will pass the exception to the developer defined
* handler in the configuration file.
*/
$handler = function($exception)
{
$config = Config::get('error');
if ($config['log'])
{
call_user_func($config['logger'], $exception, $config);
}
call_user_func($config['handler'], $exception, $config);
if ( ! $config['detail'])
{
exit(1);
}
};
/**
* Register the PHP exception handler. The framework throws exceptions
* on every error that cannot be handled. All of those exceptions will
* be sent through this closure for processing.
*/
set_exception_handler(function($exception) use ($handler)
{
$handler($exception);
});
/**
* Register the PHP error handler. All PHP errors will fall into this
* handler, which will convert the error into an ErrorException object
* and pass the exception into the common exception handler.
*/
set_error_handler(function($number, $error, $file, $line) use ($handler)
{
$handler(new \ErrorException($error, $number, 0, $file, $line));
});
/**
* Register the PHP shutdown handler. This function will be called
* at the end of the PHP script or on a fatal PHP error. If an error
* has occured, we will convert it to an ErrorException and pass it
* to the common exception handler for the framework.
*/
register_shutdown_function(function() use ($handler)
{
if ( ! is_null($error = error_get_last()))
{
extract($error, EXTR_SKIP);
$handler(new \ErrorException($message, $type, 0, $file, $line));
}
});
/**
* Setting the PHP error reporting level to -1 essentially forces
* PHP to report every error, and is guranteed to show every error
* on future versions of PHP.
*/
error_reporting(-1);
/**
* If error detail is turned off, we will turn off all PHP error
* reporting and display since the framework will be displaying a
* generic message and we don't want any sensitive details about
* the exception leaking into the views.
*/
if ( ! Config::$items['error']['detail'])
{
//ini_set('display_errors', 'Off');
}
/**
* Load the session and session manager instance. The session
* payload will be registered in the IoC container as an instance