2010-11-15 03:20:29 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Exception Renderer
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* Provides Exception rendering features. Which allow exceptions to be rendered
|
2010-11-15 03:20:29 +00:00
|
|
|
* as HTML pages.
|
|
|
|
*
|
|
|
|
* PHP 5
|
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-11-15 03:20:29 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-11-15 03:20:29 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-11-15 03:20:29 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Error
|
2010-11-15 03:20:29 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2010-11-15 03:20:29 +00:00
|
|
|
*/
|
2010-12-09 03:45:18 +00:00
|
|
|
|
2010-12-09 05:13:11 +00:00
|
|
|
App::uses('Sanitize', 'Utility');
|
2011-04-12 02:53:03 +00:00
|
|
|
App::uses('Router', 'Routing');
|
2011-07-03 19:33:27 +00:00
|
|
|
App::uses('CakeResponse', 'Network');
|
2012-07-21 03:35:31 +00:00
|
|
|
App::uses('Controller', 'Controller');
|
2010-12-09 03:45:18 +00:00
|
|
|
|
2010-11-15 03:20:29 +00:00
|
|
|
/**
|
|
|
|
* Exception Renderer.
|
|
|
|
*
|
|
|
|
* Captures and handles all unhandled exceptions. Displays helpful framework errors when debug > 1.
|
2013-07-05 12:36:40 +00:00
|
|
|
* When debug < 1 a CakeException will render 404 or 500 errors. If an uncaught exception is thrown
|
2010-11-15 03:20:29 +00:00
|
|
|
* and it is a type that ExceptionHandler does not know about it will be treated as a 500 error.
|
|
|
|
*
|
2010-11-25 12:23:37 +00:00
|
|
|
* ### Implementing application specific exception rendering
|
2010-11-15 03:20:29 +00:00
|
|
|
*
|
|
|
|
* You can implement application specific exception handling in one of a few ways:
|
|
|
|
*
|
|
|
|
* - Create a AppController::appError();
|
2010-11-25 12:23:37 +00:00
|
|
|
* - Create a subclass of ExceptionRenderer and configure it to be the `Exception.renderer`
|
2010-11-15 03:20:29 +00:00
|
|
|
*
|
|
|
|
* #### Using AppController::appError();
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* This controller method is called instead of the default exception handling. It receives the
|
|
|
|
* thrown exception as its only argument. You should implement your error handling in that method.
|
2010-11-15 03:20:29 +00:00
|
|
|
*
|
2010-11-25 12:23:37 +00:00
|
|
|
* #### Using a subclass of ExceptionRenderer
|
2010-11-15 03:20:29 +00:00
|
|
|
*
|
2011-05-16 22:08:51 +00:00
|
|
|
* Using a subclass of ExceptionRenderer gives you full control over how Exceptions are rendered, you
|
2010-11-25 12:23:37 +00:00
|
|
|
* can configure your class in your core.php, with `Configure::write('Exception.renderer', 'MyClass');`
|
2011-09-18 14:28:47 +00:00
|
|
|
* You should place any custom exception renderers in `app/Lib/Error`.
|
2010-11-15 03:20:29 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Error
|
2010-11-15 03:20:29 +00:00
|
|
|
*/
|
|
|
|
class ExceptionRenderer {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller instance.
|
|
|
|
*
|
|
|
|
* @var Controller
|
|
|
|
*/
|
|
|
|
public $controller = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* template to render for CakeException
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $template = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The method corresponding to the Exception this object is for.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $method = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The exception being handled.
|
|
|
|
*
|
|
|
|
* @var Exception
|
|
|
|
*/
|
|
|
|
public $error = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* code error depending on the code used to construct the error.
|
|
|
|
*
|
2011-07-30 20:48:37 +00:00
|
|
|
* @param Exception $exception Exception
|
2012-11-28 22:30:47 +00:00
|
|
|
* @return mixed Return void or value returned by controller's `appError()` function
|
2010-11-15 03:20:29 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function __construct(Exception $exception) {
|
2010-11-15 03:20:29 +00:00
|
|
|
$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)));
|
|
|
|
$code = $exception->getCode();
|
|
|
|
|
|
|
|
$methodExists = method_exists($this, $method);
|
|
|
|
|
|
|
|
if ($exception instanceof CakeException && !$methodExists) {
|
|
|
|
$method = '_cakeError';
|
2013-02-12 02:38:08 +00:00
|
|
|
if (empty($template) || $template === 'internalError') {
|
2010-11-15 03:20:29 +00:00
|
|
|
$template = 'error500';
|
|
|
|
}
|
2011-09-04 09:20:19 +00:00
|
|
|
} elseif ($exception instanceof PDOException) {
|
|
|
|
$method = 'pdoError';
|
|
|
|
$template = 'pdo_error';
|
|
|
|
$code = 500;
|
2010-11-15 03:20:29 +00:00
|
|
|
} elseif (!$methodExists) {
|
|
|
|
$method = 'error500';
|
2010-12-11 18:30:29 +00:00
|
|
|
if ($code >= 400 && $code < 500) {
|
2010-11-15 03:20:29 +00:00
|
|
|
$method = 'error400';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-26 22:26:54 +00:00
|
|
|
$isNotDebug = !Configure::read('debug');
|
2013-02-12 02:38:08 +00:00
|
|
|
if ($isNotDebug && $method === '_cakeError') {
|
2012-10-04 02:59:35 +00:00
|
|
|
$method = 'error400';
|
|
|
|
}
|
2012-10-04 15:27:24 +00:00
|
|
|
if ($isNotDebug && $code == 500) {
|
2012-10-04 02:59:35 +00:00
|
|
|
$method = 'error500';
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
$this->template = $template;
|
|
|
|
$this->method = $method;
|
|
|
|
$this->error = $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the controller instance to handle the exception.
|
2011-05-16 22:08:51 +00:00
|
|
|
* Override this method in subclasses to customize the controller used.
|
2010-11-15 03:20:29 +00:00
|
|
|
* This method returns the built in `CakeErrorController` 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) {
|
2012-11-01 20:25:37 +00:00
|
|
|
App::uses('AppController', 'Controller');
|
2010-12-04 19:41:15 +00:00
|
|
|
App::uses('CakeErrorController', 'Controller');
|
2012-04-03 17:24:36 +00:00
|
|
|
if (!$request = Router::getRequest(true)) {
|
2011-05-17 01:11:33 +00:00
|
|
|
$request = new CakeRequest();
|
|
|
|
}
|
2012-09-27 18:28:19 +00:00
|
|
|
$response = new CakeResponse();
|
2012-08-08 11:13:28 +00:00
|
|
|
|
|
|
|
if (method_exists($exception, 'responseHeader')) {
|
|
|
|
$response->header($exception->responseHeader());
|
|
|
|
}
|
|
|
|
|
2013-05-27 02:11:13 +00:00
|
|
|
if (class_exists('AppController')) {
|
|
|
|
try {
|
|
|
|
$controller = new CakeErrorController($request, $response);
|
|
|
|
$controller->startupProcess();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
if (!empty($controller) && $controller->Components->enabled('RequestHandler')) {
|
|
|
|
$controller->RequestHandler->startup($controller);
|
|
|
|
}
|
2012-11-24 20:38:42 +00:00
|
|
|
}
|
2012-05-05 16:54:41 +00:00
|
|
|
}
|
|
|
|
if (empty($controller)) {
|
2011-07-03 19:33:27 +00:00
|
|
|
$controller = new Controller($request, $response);
|
2011-05-16 22:08:51 +00:00
|
|
|
$controller->viewPath = 'Errors';
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
return $controller;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the response for the exception.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function render() {
|
2010-11-27 19:35:36 +00:00
|
|
|
if ($this->method) {
|
|
|
|
call_user_func_array(array($this, $this->method), array($this->error));
|
|
|
|
}
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic handler for the internal framework errors CakePHP can generate.
|
|
|
|
*
|
2011-07-31 22:03:28 +00:00
|
|
|
* @param CakeException $error
|
2010-11-15 03:20:29 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _cakeError(CakeException $error) {
|
2011-03-03 03:03:21 +00:00
|
|
|
$url = $this->controller->request->here();
|
2011-09-27 02:00:58 +00:00
|
|
|
$code = ($error->getCode() >= 400 && $error->getCode() < 506) ? $error->getCode() : 500;
|
2010-11-15 03:20:29 +00:00
|
|
|
$this->controller->response->statusCode($code);
|
|
|
|
$this->controller->set(array(
|
|
|
|
'code' => $code,
|
|
|
|
'url' => h($url),
|
2012-08-04 20:46:09 +00:00
|
|
|
'name' => h($error->getMessage()),
|
2010-11-15 03:20:29 +00:00
|
|
|
'error' => $error,
|
2012-02-04 16:12:45 +00:00
|
|
|
'_serialize' => array('code', 'url', 'name')
|
2010-11-15 03:20:29 +00:00
|
|
|
));
|
2012-02-03 02:24:36 +00:00
|
|
|
$this->controller->set($error->getAttributes());
|
|
|
|
$this->_outputMessage($this->template);
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience method to display a 400 series page.
|
|
|
|
*
|
2011-07-30 20:48:37 +00:00
|
|
|
* @param Exception $error
|
|
|
|
* @return void
|
2010-11-15 03:20:29 +00:00
|
|
|
*/
|
|
|
|
public function error400($error) {
|
|
|
|
$message = $error->getMessage();
|
2012-09-14 17:42:25 +00:00
|
|
|
if (!Configure::read('debug') && $error instanceof CakeException) {
|
2011-10-23 18:19:14 +00:00
|
|
|
$message = __d('cake', 'Not Found');
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
2011-03-03 03:03:21 +00:00
|
|
|
$url = $this->controller->request->here();
|
2010-11-15 03:20:29 +00:00
|
|
|
$this->controller->response->statusCode($error->getCode());
|
|
|
|
$this->controller->set(array(
|
2012-08-04 20:46:09 +00:00
|
|
|
'name' => h($message),
|
2010-11-15 03:20:29 +00:00
|
|
|
'url' => h($url),
|
2012-03-04 00:46:15 +00:00
|
|
|
'error' => $error,
|
2012-02-04 16:12:45 +00:00
|
|
|
'_serialize' => array('name', 'url')
|
2010-11-15 03:20:29 +00:00
|
|
|
));
|
|
|
|
$this->_outputMessage('error400');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience method to display a 500 page.
|
|
|
|
*
|
2011-07-30 20:48:37 +00:00
|
|
|
* @param Exception $error
|
|
|
|
* @return void
|
2010-11-15 03:20:29 +00:00
|
|
|
*/
|
|
|
|
public function error500($error) {
|
2011-11-03 01:21:48 +00:00
|
|
|
$message = $error->getMessage();
|
2012-09-14 17:42:25 +00:00
|
|
|
if (!Configure::read('debug')) {
|
2011-12-06 02:27:27 +00:00
|
|
|
$message = __d('cake', 'An Internal Error Has Occurred.');
|
2011-11-03 01:21:48 +00:00
|
|
|
}
|
2011-03-03 03:03:21 +00:00
|
|
|
$url = $this->controller->request->here();
|
2011-09-04 09:20:19 +00:00
|
|
|
$code = ($error->getCode() > 500 && $error->getCode() < 506) ? $error->getCode() : 500;
|
2010-11-15 03:20:29 +00:00
|
|
|
$this->controller->response->statusCode($code);
|
|
|
|
$this->controller->set(array(
|
2012-08-04 20:46:09 +00:00
|
|
|
'name' => h($message),
|
2010-11-15 03:20:29 +00:00
|
|
|
'message' => h($url),
|
|
|
|
'error' => $error,
|
2012-02-04 16:12:45 +00:00
|
|
|
'_serialize' => array('name', 'message')
|
2010-11-15 03:20:29 +00:00
|
|
|
));
|
|
|
|
$this->_outputMessage('error500');
|
|
|
|
}
|
|
|
|
|
2011-09-04 09:20:19 +00:00
|
|
|
/**
|
|
|
|
* Convenience method to display a PDOException.
|
|
|
|
*
|
2011-09-18 14:28:47 +00:00
|
|
|
* @param PDOException $error
|
2011-09-04 09:20:19 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function pdoError(PDOException $error) {
|
|
|
|
$url = $this->controller->request->here();
|
|
|
|
$code = 500;
|
|
|
|
$this->controller->response->statusCode($code);
|
|
|
|
$this->controller->set(array(
|
|
|
|
'code' => $code,
|
|
|
|
'url' => h($url),
|
2012-08-04 20:46:09 +00:00
|
|
|
'name' => h($error->getMessage()),
|
2011-09-04 09:20:19 +00:00
|
|
|
'error' => $error,
|
2012-02-04 16:12:45 +00:00
|
|
|
'_serialize' => array('code', 'url', 'name', 'error')
|
2011-09-04 09:20:19 +00:00
|
|
|
));
|
2012-02-03 02:24:36 +00:00
|
|
|
$this->_outputMessage($this->template);
|
2011-09-04 09:20:19 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 03:20:29 +00:00
|
|
|
/**
|
|
|
|
* Generate the response using the controller object.
|
|
|
|
*
|
|
|
|
* @param string $template The template to render.
|
2011-07-30 20:48:37 +00:00
|
|
|
* @return void
|
2010-11-15 03:20:29 +00:00
|
|
|
*/
|
|
|
|
protected function _outputMessage($template) {
|
2012-02-03 02:24:36 +00:00
|
|
|
try {
|
|
|
|
$this->controller->render($template);
|
|
|
|
$this->controller->afterFilter();
|
|
|
|
$this->controller->response->send();
|
2012-06-01 04:42:55 +00:00
|
|
|
} catch (MissingViewException $e) {
|
2012-12-28 01:58:27 +00:00
|
|
|
$attributes = $e->getAttributes();
|
|
|
|
if (isset($attributes['file']) && strpos($attributes['file'], 'error500') !== false) {
|
2012-06-01 04:42:55 +00:00
|
|
|
$this->_outputMessageSafe('error500');
|
2012-12-28 01:58:27 +00:00
|
|
|
} else {
|
|
|
|
$this->_outputMessage('error500');
|
2012-06-01 04:42:55 +00:00
|
|
|
}
|
2012-02-03 02:24:36 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->_outputMessageSafe('error500');
|
|
|
|
}
|
2010-11-15 03:20:29 +00:00
|
|
|
}
|
2011-04-26 01:04:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A safer way to render error messages, replaces all helpers, with basics
|
|
|
|
* and doesn't call component methods.
|
|
|
|
*
|
|
|
|
* @param string $template The template to render
|
2011-07-30 20:48:37 +00:00
|
|
|
* @return void
|
2011-04-26 01:04:50 +00:00
|
|
|
*/
|
|
|
|
protected function _outputMessageSafe($template) {
|
2012-06-10 16:49:39 +00:00
|
|
|
$this->controller->layoutPath = null;
|
|
|
|
$this->controller->subDir = null;
|
2012-02-04 02:29:37 +00:00
|
|
|
$this->controller->viewPath = 'Errors/';
|
2012-03-11 17:01:53 +00:00
|
|
|
$this->controller->layout = 'error';
|
2012-02-04 02:29:37 +00:00
|
|
|
$this->controller->helpers = array('Form', 'Html', 'Session');
|
|
|
|
|
2012-09-28 01:38:20 +00:00
|
|
|
$view = new View($this->controller);
|
|
|
|
$this->controller->response->body($view->render($template, 'error'));
|
2012-02-03 02:24:36 +00:00
|
|
|
$this->controller->response->type('html');
|
2011-04-26 01:04:50 +00:00
|
|
|
$this->controller->response->send();
|
|
|
|
}
|
2012-03-04 00:46:15 +00:00
|
|
|
|
2011-04-16 21:47:39 +00:00
|
|
|
}
|