Permalink
Browse files

Updated Error folder

  • Loading branch information...
jrbasso committed May 25, 2012
1 parent f5b2e4d commit 89c3e98ac6cae3b9279fe13d796574b8528f4b45
Showing with 88 additions and 89 deletions.
  1. +19 −20 lib/Cake/Error/ErrorHandler.php
  2. +30 −29 lib/Cake/Error/ExceptionRenderer.php
  3. +39 −40 lib/Cake/Error/exceptions.php
@@ -18,11 +18,11 @@
* @since CakePHP(tm) v 0.10.5.1732
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Debugger', 'Utility');
App::uses('CakeLog', 'Log');
App::uses('ExceptionRenderer', 'Error');
App::uses('AppController', 'Controller');
namespace Cake\Error;
use Cake\Core\Configure,
Cake\Core\App,
Cake\Utility\Debugger,
Cake\Log\Log;
/**
*
@@ -31,7 +31,7 @@
*
* ### Uncaught exceptions
*
* When debug < 1 a CakeException will render 404 or 500 errors. If an uncaught exception is thrown
* When debug < 1 a Cake\Error\Exception will render 404 or 500 errors. If an uncaught exception is thrown
* and it is a type that ErrorHandler does not know about it will be treated as a 500 error.
*
* ### Implementing application specific exception handling
@@ -69,13 +69,13 @@
*
* Using the built-in exception handling, you can log all the exceptions
* that are dealt with by ErrorHandler by setting `Exception.log` to true in your core.php.
* Enabling this will log every exception to CakeLog and the configured loggers.
* Enabling this will log every exception to Log and the configured loggers.
*
* ### PHP errors
*
* Error handler also provides the built in features for handling php errors (trigger_error).
* While in debug mode, errors will be output to the screen using debugger. While in production mode,
* errors will be logged to CakeLog. You can control which errors are logged by setting
* errors will be logged to Log. You can control which errors are logged by setting
* `Error.level` in your core.php.
*
* #### Logging errors
@@ -103,29 +103,28 @@ class ErrorHandler {
* This will either use custom exception renderer class if configured,
* or use the default ExceptionRenderer.
*
* @param Exception $exception
* @param \Exception $exception
* @return void
* @see http://php.net/manual/en/function.set-exception-handler.php
*/
public static function handleException(Exception $exception) {
public static function handleException(\Exception $exception) {
$config = Configure::read('Exception');
if (!empty($config['log'])) {
$message = sprintf("[%s] %s\n%s",
get_class($exception),
$exception->getMessage(),
$exception->getTraceAsString()
);
CakeLog::write(LOG_ERR, $message);
}
$renderer = $config['renderer'];
if ($renderer !== 'ExceptionRenderer') {
list($plugin, $renderer) = pluginSplit($renderer, true);
App::uses($renderer, $plugin . 'Error');
Log::write(LOG_ERR, $message);
}
$renderer = App::classname($config['renderer'], 'Error');
try {
if (!$renderer) {
throw new \Exception("{$config['renderer']} is an invalid class.");
}
$error = new $renderer($exception);
$error->render();
} catch (Exception $e) {
} catch (\Exception $e) {
set_error_handler(Configure::read('Error.handler')); // Should be using configured ErrorHandler
Configure::write('Error.trace', false); // trace is useless here since it's internal
$message = sprintf("[%s] %s\n%s", // Keeping same message format
@@ -140,7 +139,7 @@ public static function handleException(Exception $exception) {
/**
* Set as the default error handler by CakePHP. Use Configure::write('Error.handler', $callback), to use your own
* error handling methods. This function will use Debugger to display errors when debug > 0. And
* will log errors to CakeLog, when debug == 0.
* will log errors to Log, when debug == 0.
*
* You can use Configure::write('Error.level', $value); to set what type of errors will be handled here.
* Stack traces for errors can be enabled with Configure::write('Error.trace', true);
@@ -182,7 +181,7 @@ public static function handleError($code, $description, $file = null, $line = nu
$trace = Debugger::trace(array('start' => 1, 'format' => 'log'));
$message .= "\nTrace:\n" . $trace . "\n";
}
return CakeLog::write($log, $message);
return Log::write($log, $message);
}
}
@@ -197,7 +196,7 @@ public static function handleError($code, $description, $file = null, $line = nu
*/
public static function handleFatalError($code, $description, $file, $line) {
$logMessage = 'Fatal Error (' . $code . '): ' . $description . ' in [' . $file . ', line ' . $line . ']';
CakeLog::write(LOG_ERR, $logMessage);
Log::write(LOG_ERR, $logMessage);
$exceptionHandler = Configure::read('Exception.handler');
if (!is_callable($exceptionHandler)) {
@@ -19,16 +19,20 @@
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Sanitize', 'Utility');
App::uses('Router', 'Routing');
App::uses('CakeResponse', 'Network');
namespace Cake\Error;
use Cake\Routing\Router,
Cake\Network\Request,
Cake\Network\Response,
Cake\Core\Configure,
Cake\Utility\Inflector,
Cake\Controller\Controller,
Cake\Controller\ErrorController;
/**
* Exception Renderer.
*
* Captures and handles all unhandled exceptions. Displays helpful framework errors when debug > 1.
* When debug < 1 a CakeException will render 404 or 500 errors. If an uncaught exception is thrown
* When debug < 1 a Cake\Error\Exception will render 404 or 500 errors. If an uncaught exception is thrown
* and it is a type that ExceptionHandler does not know about it will be treated as a 500 error.
*
* ### Implementing application specific exception rendering
@@ -61,7 +65,7 @@ class ExceptionRenderer {
public $controller = null;
/**
* template to render for CakeException
* template to render for Cake\Error\Exception
*
* @var string
*/
@@ -83,31 +87,33 @@ class ExceptionRenderer {
/**
* Creates the controller to perform rendering on the error response.
* If the error is a CakeException it will be converted to either a 400 or a 500
* If the error is a Cake\Error\Exception it will be converted to either a 400 or a 500
* code error depending on the code used to construct the error.
*
* @param Exception $exception Exception
* @param \Exception $exception Exception
*/
public function __construct(Exception $exception) {
public function __construct(\Exception $exception) {
$this->controller = $this->_getController($exception);
if (method_exists($this->controller, 'apperror')) {
return $this->controller->appError($exception);
}
$method = $template = Inflector::variable(str_replace('Exception', '', get_class($exception)));
list(, $baseClass) = namespaceSplit(get_class($exception));
$baseClass = substr($baseClass, 0, -9);
$method = $template = Inflector::variable($baseClass);
$code = $exception->getCode();
$methodExists = method_exists($this, $method);
if ($exception instanceof CakeException && !$methodExists) {
if ($exception instanceof Exception && !$methodExists) {
$method = '_cakeError';
if (empty($template)) {
$template = 'error500';
}
if ($template == 'internalError') {
$template = 'error500';
}
} elseif ($exception instanceof PDOException) {
} elseif ($exception instanceof \PDOException) {
$method = 'pdoError';
$template = 'pdo_error';
$code = 500;
@@ -134,25 +140,20 @@ public function __construct(Exception $exception) {
/**
* Get the controller instance to handle the exception.
* Override this method in subclasses to customize the controller used.
* This method returns the built in `CakeErrorController` normally, or if an error is repeated
* This method returns the built in `ErrorController` normally, or if an error is repeated
* a bare controller will be used.
*
* @param Exception $exception The exception to get a controller for.
* @return Controller
*/
protected function _getController($exception) {
App::uses('CakeErrorController', 'Controller');
if (!$request = Router::getRequest(true)) {
$request = new CakeRequest();
$request = new Request();
}
$response = new CakeResponse(array('charset' => Configure::read('App.encoding')));
$response = new Response(array('charset' => Configure::read('App.encoding')));
try {
if (class_exists('AppController')) {
$controller = new CakeErrorController($request, $response);
}
} catch (Exception $e) {
}
if (empty($controller)) {
$controller = new ErrorController($request, $response);
} catch (\Exception $e) {
$controller = new Controller($request, $response);
$controller->viewPath = 'Errors';
}
@@ -173,10 +174,10 @@ public function render() {
/**
* Generic handler for the internal framework errors CakePHP can generate.
*
* @param CakeException $error
* @param Cake\Error\Exception $error
* @return void
*/
protected function _cakeError(CakeException $error) {
protected function _cakeError(Exception $error) {
$url = $this->controller->request->here();
$code = ($error->getCode() >= 400 && $error->getCode() < 506) ? $error->getCode() : 500;
$this->controller->response->statusCode($code);
@@ -199,7 +200,7 @@ protected function _cakeError(CakeException $error) {
*/
public function error400($error) {
$message = $error->getMessage();
if (Configure::read('debug') == 0 && $error instanceof CakeException) {
if (Configure::read('debug') == 0 && $error instanceof Exception) {
$message = __d('cake', 'Not Found');
}
$url = $this->controller->request->here();
@@ -216,7 +217,7 @@ public function error400($error) {
/**
* Convenience method to display a 500 page.
*
* @param Exception $error
* @param \Exception $error
* @return void
*/
public function error500($error) {
@@ -239,10 +240,10 @@ public function error500($error) {
/**
* Convenience method to display a PDOException.
*
* @param PDOException $error
* @param \PDOException $error
* @return void
*/
public function pdoError(PDOException $error) {
public function pdoError(\PDOException $error) {
$url = $this->controller->request->here();
$code = 500;
$this->controller->response->statusCode($code);
@@ -267,7 +268,7 @@ protected function _outputMessage($template) {
$this->controller->render($template);
$this->controller->afterFilter();
$this->controller->response->send();
} catch (Exception $e) {
} catch (\Exception $e) {
$this->controller->set(array(
'error' => $e,
'name' => $e->getMessage(),
Oops, something went wrong.

0 comments on commit 89c3e98

Please sign in to comment.