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

@@ -59,6 +59,7 @@ require SYS_PATH.'autoloader'.EXT;
*/
Config::load('application');
Config::load('session');
Config::load('error');
/**
* Bootstrap the application inversion of control container. The IoC

View File

@@ -1,110 +0,0 @@
<?php namespace Laravel;
/**
* Define a closure that will return the formatted error message when
* when given an exception. This function will be used by all of error
* handlers to create a more readable message.
*/
$message = function($e)
{
$search = array(APP_PATH, SYS_PATH);
$replace = array('APP_PATH/', 'SYS_PATH/');
$file = str_replace($search, $replace, $e->getFile());
return rtrim($e->getMessage(), '.').' in '.$file.' on line '.$e->getLine().'.';
};
/**
* Define a closure that will return a more readable version of the
* severity of an exception. This function will be used by the error
* handler when parsing exceptions.
*/
$severity = function($e)
{
$levels = array(
0 => 'Error',
E_ERROR => 'Error',
E_WARNING => 'Warning',
E_PARSE => 'Parsing Error',
E_NOTICE => 'Notice',
E_CORE_ERROR => 'Core Error',
E_CORE_WARNING => 'Core Warning',
E_COMPILE_ERROR => 'Compile Error',
E_COMPILE_WARNING => 'Compile Warning',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Runtime Notice',
);
$code = $e->getCode();
return (array_key_exists($code, $levels)) ? $levels[$code] : $code;
};
/**
* Create the exception handler function. All of the error handlers
* registered by the framework call this closure to avoid duplicate
* code. Each of the formatting closures defined above will be
* passed into the handler for convenient use.
*/
$handler = function($e) use ($message, $severity)
{
$config = Config::get('error');
if ($config['log'])
{
call_user_func($config['logger'], $e, $severity($e), $message($e), $config);
}
call_user_func($config['handler'], $e, $severity($e), $message($e), $config);
exit(1);
};
/**
* Register the PHP exception handler. The framework throws exceptions
* on every error that cannot be handled. All of those exceptions will
* fall into this closure for processing.
*/
set_exception_handler(function($e) use ($handler)
{
$handler($e);
});
/**
* 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
* occurred, we will convert it to an ErrorException and pass it to
* the common exception handler.
*/
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));
}
});
/**
* Turn off all PHP error reporting and display. Since the framework
* will be displaying the exception messages, we don't want PHP to
* display any ugly error information.
*/
error_reporting(-1);
ini_set('display_errors', 'Off');

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