Updating the various CakeExceptions to take arrays in their constructors. This allows for the existing templates to continue working, as well as generalize the way in which errors are handled. This change also makes the messages coming out of exceptions more readable and removes string hackery.

This commit is contained in:
mark_story 2010-08-29 21:37:25 -04:00
parent 741f2972f9
commit f1164c93d6
22 changed files with 233 additions and 347 deletions

View file

@ -134,7 +134,9 @@ class Dispatcher {
if (!is_object($controller)) { if (!is_object($controller)) {
Router::setRequestInfo($request); Router::setRequestInfo($request);
throw new MissingControllerException(Inflector::camelize($request->params['controller']) . 'Controller'); throw new MissingControllerException(array(
'controller' => Inflector::camelize($request->params['controller']) . 'Controller'
));
} }
$privateAction = $request->params['action'][0] === '_'; $privateAction = $request->params['action'][0] === '_';
$prefixes = Router::prefixes(); $prefixes = Router::prefixes();
@ -151,12 +153,10 @@ class Dispatcher {
Router::setRequestInfo($request); Router::setRequestInfo($request);
if ($privateAction) { if ($privateAction) {
$message = sprintf( throw new PrivateActionException(array(
'%s::%s()', 'controller' => Inflector::camelize($request->params['controller']) . "Controller",
Inflector::camelize($request->params['controller']) . "Controller", 'action' => $request->params['action']
$request->params['action'] ));
);
throw new PrivateActionException($message);
} }
return $this->_invoke($controller, $request); return $this->_invoke($controller, $request);
@ -184,12 +184,10 @@ class Dispatcher {
App::import('Controller', 'Scaffold', false); App::import('Controller', 'Scaffold', false);
return new Scaffold($controller, $request); return new Scaffold($controller, $request);
} }
$message = sprintf( throw new MissingActionException(array(
'%s::%s()', 'controller' => Inflector::camelize($request->params['controller']) . "Controller",
Inflector::camelize($request->params['controller']) . "Controller", 'action' => $request->params['action']
$request->params['action'] ));
);
throw new MissingActionException($message);
} }
$result =& call_user_func_array(array(&$controller, $request->params['action']), $request->params['pass']); $result =& call_user_func_array(array(&$controller, $request->params['action']), $request->params['pass']);
$response = $controller->getResponse(); $response = $controller->getResponse();

View file

@ -54,10 +54,16 @@ class ComponentCollection extends ObjectCollection {
$componentClass = $name . 'Component'; $componentClass = $name . 'Component';
if (!class_exists($componentClass)) { if (!class_exists($componentClass)) {
if (!App::import('Component', $component)) { if (!App::import('Component', $component)) {
throw new MissingComponentFileException(Inflector::underscore($component) . '.php'); throw new MissingComponentFileException(array(
'file' => Inflector::underscore($component) . '.php',
'class' => $componentClass
));
} }
if (!class_exists($componentClass)) { if (!class_exists($componentClass)) {
throw new MissingComponentFileException($component); throw new MissingComponentFileException(array(
'file' => Inflector::underscore($component) . '.php',
'class' => $componentClass
));
} }
} }
$this->_loaded[$name] = new $componentClass($this, $settings); $this->_loaded[$name] = new $componentClass($this, $settings);

View file

@ -414,11 +414,13 @@ class Scaffold {
break; break;
} }
} else { } else {
$message = sprintf('%s::%s()', $this->controller->name . "Controller", $request->action); throw new MissingActionException(array(
throw new MissingActionException($message); 'controller' => $this->controller->name,
'action' => $request->action
));
} }
} else { } else {
throw new MissingDatabaseException($this->ScaffoldModel->useDbConfig); throw new MissingDatabaseException(array('connection' => $this->ScaffoldModel->useDbConfig));
} }
} }

View file

@ -40,6 +40,27 @@ class ErrorHandler {
*/ */
public $controller = null; 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;
/** /**
* Class constructor. * Class constructor.
* *
@ -54,13 +75,13 @@ class ErrorHandler {
if (method_exists($this->controller, 'apperror')) { if (method_exists($this->controller, 'apperror')) {
return $this->controller->appError($exception); return $this->controller->appError($exception);
} }
$method = Inflector::variable(str_replace('Exception', '', get_class($exception))); $method = $template = Inflector::variable(str_replace('Exception', '', get_class($exception)));
if (!in_array($method, get_class_methods($this))) { if ($exception instanceof CakeException && !in_array($method, get_class_methods($this))) {
$method = 'error'; $method = '_cakeError';
} }
if ($method !== 'error') {
if (Configure::read('debug') == 0) { if ($method !== 'error' && Configure::read('debug') == 0) {
$code = $exception->getCode(); $code = $exception->getCode();
$parentClass = get_parent_class($this); $parentClass = get_parent_class($this);
if ($parentClass != 'ErrorHandler') { if ($parentClass != 'ErrorHandler') {
@ -74,7 +95,7 @@ class ErrorHandler {
$method = 'error500'; $method = 'error500';
} }
} }
} $this->template = $template;
$this->method = $method; $this->method = $method;
$this->error = $exception; $this->error = $exception;
} }
@ -133,6 +154,24 @@ class ErrorHandler {
$this->error404($error); $this->error404($error);
} }
/**
* Generic handler for the internal framework errors CakePHP can generate.
*
* @param CakeExeption $error
* @return void
*/
protected function _cakeError(CakeException $error) {
$url = Router::normalize($this->controller->request->here);
$code = $error->getCode();
$this->controller->response->statusCode($code);
$this->controller->set(array(
'code' => $code,
'url' => h($url),
));
$this->controller->set($error->getAttributes());
$this->_outputMessage($this->template);
}
/** /**
* Convenience method to display a 404 page. * Convenience method to display a 404 page.
* *
@ -167,207 +206,11 @@ class ErrorHandler {
)); ));
$this->_outputMessage('error500'); $this->_outputMessage('error500');
} }
/**
* Renders the Missing Controller web page.
*
* @param array $params Parameters for controller
*/
public function missingController($error) {
$controllerName = str_replace('Controller', '', $error->getMessage());
$this->controller->set(array(
'controller' => $error->getMessage(),
'controllerName' => $controllerName
));
$this->_outputMessage('missingController');
}
/** /**
* Renders the Missing Action web page. * Generate the response using the controller object.
*
* @param array $params Parameters for controller
*/
public function missingAction($error) {
$message = $error->getMessage();
list($controllerName, $action) = explode('::', $message);
$this->controller->set(array(
'controller' => $controllerName,
'action' => $action,
));
$this->_outputMessage('missingAction');
}
/**
* Renders the Private Action web page.
*
* @param array $params Parameters for controller
*/
public function privateAction($error) {
$message = $error->getMessage();
list($controllerName, $action) = explode('::', $message);
$this->controller->set(array(
'controller' => $controllerName,
'action' => $action
));
$this->_outputMessage('privateAction');
}
/**
* Renders the Missing Table web page.
*
* @param array $params Parameters for controller
*/
public function missingTable($error) {
$this->controller->header("HTTP/1.0 500 Internal Server Error");
$this->controller->set(array(
'model' => $error->getModel(),
'table' => $error->getTable(),
));
$this->_outputMessage('missingTable');
}
/**
* Renders the Missing Database web page.
*
* @param array $params Parameters for controller
*/
public function missingDatabase($exception) {
$this->controller->header("HTTP/1.0 500 Internal Server Error");
$this->controller->set(array(
'code' => '500',
'title' => __('Scaffold Missing Database Connection')
));
$this->_outputMessage('missingScaffolddb');
}
/**
* Renders the Missing View web page.
*
* @param array $params Parameters for controller
*/
public function missingView($error) {
$this->controller->set(array(
'file' => $error->getMessage(),
));
$this->_outputMessage('missingView');
}
/**
* Renders the Missing Layout web page.
*
* @param array $params Parameters for controller
*/
public function missingLayout($error) {
$this->controller->layout = 'default';
$this->controller->set(array(
'file' => $error->getMessage(),
));
$this->_outputMessage('missingLayout');
}
/**
* Renders the Database Connection web page.
*
* @param array $params Parameters for controller
*/
public function missingConnection($error) {
$this->controller->header("HTTP/1.0 500 Internal Server Error");
$this->controller->set(array(
'code' => '500',
'model' => $error->getMessage(),
));
$this->_outputMessage('missingConnection');
}
/**
* Renders the Missing Helper file web page.
*
* @param array $params Parameters for controller
*/
public function missingHelperFile($error) {
list($class, $ext) = explode('.', $error->getMessage());
$this->controller->set(array(
'className' => Inflector::camelize($class),
'file' => $error->getMessage()
));
$this->_outputMessage('missingHelperFile');
}
/**
* Renders the Missing Helper class web page.
*
* @param array $params Parameters for controller
*/
public function missingHelperClass($error) {
$class = $error->getMessage();
$file = Inflector::underscore(str_replace('Helper', '', $error->getMessage())) . '.php';
$this->controller->set(array(
'className' => $class,
'file' => $file,
));
$this->_outputMessage('missingHelperClass');
}
/**
* Renders the Missing Behavior file web page.
*
* @param array $params Parameters for controller
*/
public function missingBehaviorFile($error) {
list($class, $ext) = explode('.', $error->getMessage());
$this->controller->set(array(
'className' => Inflector::camelize($class),
'file' => $error->getMessage()
));
$this->_outputMessage('missingBehaviorFile');
}
/**
* Renders the Missing Behavior class web page.
*
* @param array $params Parameters for controller
*/
public function missingBehaviorClass($error) {
$class = $error->getMessage();
$file = Inflector::underscore(str_replace('Behavior', '', $error->getMessage())) . '.php';
$this->controller->set(array(
'className' => $class,
'file' => $file,
));
$this->_outputMessage('missingBehaviorClass');
}
/**
* Renders the Missing Component file web page.
*
* @param array $params Parameters for controller
*/
public function missingComponentFile($error) {
list($class, $ext) = explode('.', $error->getMessage());
$this->controller->set(array(
'className' => Inflector::camelize($class),
'file' => $error->getMessage()
));
$this->_outputMessage('missingComponentFile');
}
/**
* Renders the Missing Component class web page.
*
* @param array $params Parameters for controller
*/
public function missingComponentClass($error) {
$class = $error->getMessage();
$file = Inflector::underscore(str_replace('Component', '', $error->getMessage())) . '.php';
$this->controller->set(array(
'className' => $class,
'file' => $file,
));
$this->_outputMessage('missingComponentClass');
}
/**
* Output message
* *
* @param string $template The template to render.
*/ */
protected function _outputMessage($template) { protected function _outputMessage($template) {
$this->controller->render($template); $this->controller->render($template);

View file

@ -18,6 +18,25 @@
* @since CakePHP(tm) v 2.0 * @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/ */
class Error404Exception extends RuntimeException {
public function __construct($message, $code = 404) {
if (empty($message)) {
$message = __('Not Found');
}
parent::__construct($message, $code);
}
}
class Error500Exception extends CakeException {
public function __construct($message, $code = 500) {
if (empty($message)) {
$message = __('Internal Server Error');
}
parent::__construct($message, $code);
}
}
/** /**
* CakeException is used a base class for CakePHP's internal exceptions. * CakeException is used a base class for CakePHP's internal exceptions.
* In general framework errors are interpreted as 500 code errors. * In general framework errors are interpreted as 500 code errors.
@ -25,31 +44,58 @@
* @package cake.libs * @package cake.libs
*/ */
class CakeException extends RuntimeException { class CakeException extends RuntimeException {
public function __construct($message, $code = 500, Exception $previous = null) {
parent::__construct($message, $code, $previous); protected $_attributes = array();
protected $_messageTemplate = '';
public function __construct($message, $code = 500) {
if (is_array($message)) {
$this->_attributes = $message;
$message = vsprintf(__($this->_messageTemplate), $message);
}
parent::__construct($message, $code);
}
public function getAttributes() {
return $this->_attributes;
} }
} }
class Error404Exception extends RuntimeException {
public function __construct($message, $code = 404, Exception $previous = null) {
parent::__construct($message, $code, $previous);
}
}
class Error500Exception extends CakeException { }
/* /*
* Exceptions used by Dispatcher * Exceptions used by Dispatcher
*/ */
class MissingControllerException extends Error404Exception { } class MissingControllerException extends CakeException {
class MissingActionException extends Error404Exception { } protected $_messageTemplate = 'Controller class %s could not be found.';
class PrivateActionException extends Error404Exception { }
public function __construct($message, $code = 404) {
parent::__construct($message, $code);
}
}
class MissingActionException extends CakeException {
protected $_messageTemplate = 'Action %s::%s() could not be found.';
public function __construct($message, $code = 404) {
parent::__construct($message, $code);
}
}
class PrivateActionException extends CakeException {
protected $_messageTemplate = 'Private Action %s::%s() is not directly accessible.';
public function __construct($message, $code = 404, Exception $previous = null) {
parent::__construct($message, $code, $previous);
}
}
/** /**
* Exceptions used by the ComponentCollection. * Exceptions used by the ComponentCollection.
*/ */
class MissingComponentFileException extends CakeException { } class MissingComponentFileException extends CakeException {
class MissingComponentClassException extends CakeException { } protected $_messageTemplate = 'Component File "%s" is missing.';
}
class MissingComponentClassException extends CakeException {
protected $_messageTemplate = 'Component class "%s" is missing.';
}
/** /**
* Runtime Exceptions for behaviors * Runtime Exceptions for behaviors
@ -60,68 +106,48 @@ class MissingBehaviorClassException extends CakeException { }
/** /**
* Runtime Exceptions for Views * Runtime Exceptions for Views
*/ */
class MissingViewException extends CakeException { } class MissingViewException extends CakeException {
class MissingLayoutException extends CakeException { } protected $_messageTemplate = 'View file "%s" is missing.';
}
class MissingLayoutException extends CakeException {
protected $_messageTemplate = 'Layout file "%s" is missing.';
}
/**
* Exceptions used by the HelperCollection.
*/
class MissingHelperFileException extends CakeException {
protected $_messageTemplate = 'Helper File "%s" is missing.';
}
class MissingHelperClassException extends CakeException {
protected $_messageTemplate = 'Helper class "%s" is missing.';
}
/** /**
* Runtime Exceptions for ConnectionManager * Runtime Exceptions for ConnectionManager
*/ */
class MissingDatabaseException extends CakeException {} class MissingDatabaseException extends CakeException {
class MissingConnectionException extends CakeException {} protected $_messageTemplate = 'Database connection "%s" could not be found.';
}
class MissingConnectionException extends CakeException {
protected $_messageTemplate = 'Database connection "%s" is missing.';
}
/** /**
* Exceptions used by the TaskCollection. * Exceptions used by the TaskCollection.
*/ */
class MissingTaskFileException extends CakeException { } class MissingTaskFileException extends CakeException {
class MissingTaskClassException extends CakeException { } protected $_messageTemplate = 'Task file "%s" is missing.';
}
class MissingTaskClassException extends CakeException {
protected $_messageTemplate = 'Task class "%s" is missing.';
}
/** /**
* Exception class to be thrown when a database table is not found in the datasource * Exception class to be thrown when a database table is not found in the datasource
* *
*/ */
class MissingTableException extends CakeException { class MissingTableException extends CakeException {
/** protected $_messageTemplate = 'Database table %s for model %s was not found.';
* The name of the model wanting to load the database table
*
* @var string
*/
protected $model;
/**
* The name of the missing table
*
* @var string
*/
protected $table;
/**
* Exception costructor
*
* @param string $model The name of the model wanting to load the database table
* @param string $table The name of the missing table
* @return void
*/
public function __construct($model, $table) {
$this->model = $model;
$this->table = $table;
$message = sprintf(__('Database table %s for model %s was not found.'), $table, $model);
parent::__construct($message);
}
/**
* Returns the name of the model wanting to load the database table
*
* @return string
*/
public function getModel() {
return $this->model;
}
/**
* Returns the name of the missing table
*
* @return string
*/
public function getTable() {
return $this->table;
}
} }

View file

@ -94,10 +94,16 @@ class BehaviorCollection extends ObjectCollection {
$class = $name . 'Behavior'; $class = $name . 'Behavior';
if (!App::import('Behavior', $behavior)) { if (!App::import('Behavior', $behavior)) {
throw new MissingBehaviorFileException(Inflector::underscore($behavior) . '.php'); throw new MissingBehaviorFileException(array(
'file' => Inflector::underscore($behavior) . '.php',
'class' => $class
));
} }
if (!class_exists($class)) { if (!class_exists($class)) {
throw new MissingBehaviorClassException(Inflector::underscore($class)); throw new MissingBehaviorClassException(array(
'file' => Inflector::underscore($behavior) . '.php',
'class' => $class
));
} }
if (!isset($this->{$name})) { if (!isset($this->{$name})) {

View file

@ -222,7 +222,7 @@ class ConnectionManager {
$this->_connectionsEnum[$name] = $this->__connectionData($config); $this->_connectionsEnum[$name] = $this->__connectionData($config);
} }
} else { } else {
throw new MissingConnectionException('ConnectionManager'); throw new MissingConnectionException(array('class' => 'ConnectionManager'));
} }
} }

View file

@ -799,7 +799,10 @@ class Model extends Object {
if ($db->isInterfaceSupported('listSources')) { if ($db->isInterfaceSupported('listSources')) {
$sources = $db->listSources(); $sources = $db->listSources();
if (is_array($sources) && !in_array(strtolower($this->tablePrefix . $tableName), array_map('strtolower', $sources))) { if (is_array($sources) && !in_array(strtolower($this->tablePrefix . $tableName), array_map('strtolower', $sources))) {
throw new MissingTableException($this->alias, $this->tablePrefix . $tableName); throw new MissingTableException(array(
'table' => $this->tablePrefix . $tableName,
'class' => $this->alias
));
} }
$this->_schema = null; $this->_schema = null;
} }
@ -2825,7 +2828,7 @@ class Model extends Object {
} }
if (empty($db) || !is_object($db)) { if (empty($db) || !is_object($db)) {
throw new MissingConnectionException($this->useDbConfig); throw new MissingConnectionException(array('class' => $this->name));
} }
} }

View file

@ -24,7 +24,7 @@
</p> </p>
<p class="error"> <p class="error">
<strong><?php echo __('Error'); ?>: </strong> <strong><?php echo __('Error'); ?>: </strong>
<?php printf(__('Create %1$s%2$s in file: %3$s.'), '<em>' . $controller . '::</em>', '<em>' . $action . '</em>', APP_DIR . DS . 'controllers' . DS . Inflector::underscore($controller) . '.php'); ?> <?php printf(__('Create %1$s%2$s in file: %3$s.'), '<em>' . $controller . '::</em>', '<em>' . $action . '()</em>', APP_DIR . DS . 'controllers' . DS . Inflector::underscore($controller) . '.php'); ?>
</p> </p>
<pre> <pre>
&lt;?php &lt;?php

View file

@ -20,7 +20,7 @@
<h2><?php echo __('Missing Behavior Class'); ?></h2> <h2><?php echo __('Missing Behavior Class'); ?></h2>
<p class="error"> <p class="error">
<strong><?php echo __('Error'); ?>: </strong> <strong><?php echo __('Error'); ?>: </strong>
<?php printf(__('The behavior class <em>%s</em> can not be found or does not exist.'), $className); ?> <?php printf(__('The behavior class <em>%s</em> can not be found or does not exist.'), $class); ?>
</p> </p>
<p class="error"> <p class="error">
<strong><?php echo __('Error'); ?>: </strong> <strong><?php echo __('Error'); ?>: </strong>
@ -28,7 +28,7 @@
</p> </p>
<pre> <pre>
&lt;?php &lt;?php
class <?php echo $className;?> extends ModelBehavior { class <?php echo $class;?> extends ModelBehavior {
} }
?&gt; ?&gt;

View file

@ -28,7 +28,7 @@
</p> </p>
<pre> <pre>
&lt;?php &lt;?php
class <?php echo $className;?> extends ModelBehavior { class <?php echo $class;?> extends ModelBehavior {
} }
?&gt; ?&gt;

View file

@ -20,15 +20,15 @@
<h2><?php echo __('Missing Component Class'); ?></h2> <h2><?php echo __('Missing Component Class'); ?></h2>
<p class="error"> <p class="error">
<strong><?php echo __('Error'); ?>: </strong> <strong><?php echo __('Error'); ?>: </strong>
<?php printf(__('Component class %1$s was not found.'), '<em>' . $className . '</em>'); ?> <?php printf(__('Component class %1$s was not found.'), '<em>' . $class . '</em>'); ?>
</p> </p>
<p class="error"> <p class="error">
<strong><?php echo __('Error'); ?>: </strong> <strong><?php echo __('Error'); ?>: </strong>
<?php printf(__('Create the class %s in file: %s'), '<em>' . $className . '</em>', APP_DIR . DS . 'controllers' . DS . 'components' . DS . $file); ?> <?php printf(__('Create the class %s in file: %s'), '<em>' . $class . '</em>', APP_DIR . DS . 'controllers' . DS . 'components' . DS . $file); ?>
</p> </p>
<pre> <pre>
&lt;?php &lt;?php
class <?php echo $className;?> extends Component {<br /> class <?php echo $class;?> extends Component {<br />
} }
?&gt; ?&gt;

View file

@ -24,11 +24,11 @@
</p> </p>
<p class="error"> <p class="error">
<strong><?php echo __('Error'); ?>: </strong> <strong><?php echo __('Error'); ?>: </strong>
<?php printf(__('Create the class %s in file: %s'), '<em>' . $className . 'Component</em>', APP_DIR . DS . 'controllers' . DS . 'components' . DS . $file); ?> <?php printf(__('Create the class %s in file: %s'), '<em>' . $class . '</em>', APP_DIR . DS . 'controllers' . DS . 'components' . DS . $file); ?>
</p> </p>
<pre> <pre>
&lt;?php &lt;?php
class <?php echo $className;?>Component extends Component {<br /> class <?php echo $class;?> extends Component {<br />
} }
?&gt; ?&gt;

View file

@ -20,7 +20,7 @@
<h2><?php echo __('Missing Database Connection'); ?></h2> <h2><?php echo __('Missing Database Connection'); ?></h2>
<p class="error"> <p class="error">
<strong><?php echo __('Error'); ?>: </strong> <strong><?php echo __('Error'); ?>: </strong>
<?php printf(__('%s requires a database connection'), $model); ?> <?php printf(__('%s requires a database connection'), $class); ?>
</p> </p>
<p class="error"> <p class="error">
<strong><?php echo __('Error'); ?>: </strong> <strong><?php echo __('Error'); ?>: </strong>

View file

@ -20,7 +20,7 @@
<h2><?php echo __('Missing Helper Class'); ?></h2> <h2><?php echo __('Missing Helper Class'); ?></h2>
<p class="error"> <p class="error">
<strong><?php echo __('Error'); ?>: </strong> <strong><?php echo __('Error'); ?>: </strong>
<?php printf(__('The helper class <em>%s</em> can not be found or does not exist.'), $className); ?> <?php printf(__('The helper class <em>%s</em> can not be found or does not exist.'), $class); ?>
</p> </p>
<p class="error"> <p class="error">
<strong><?php echo __('Error'); ?>: </strong> <strong><?php echo __('Error'); ?>: </strong>
@ -28,7 +28,7 @@
</p> </p>
<pre> <pre>
&lt;?php &lt;?php
class <?php echo $className;?> extends AppHelper { class <?php echo $class;?> extends AppHelper {
} }
?&gt; ?&gt;

View file

@ -28,7 +28,7 @@
</p> </p>
<pre> <pre>
&lt;?php &lt;?php
class <?php echo $className;?> extends AppHelper { class <?php echo $class;?> extends AppHelper {
} }
?&gt; ?&gt;

View file

@ -20,7 +20,7 @@
<h2><?php echo __('Missing Database Table'); ?></h2> <h2><?php echo __('Missing Database Table'); ?></h2>
<p class="error"> <p class="error">
<strong><?php echo __('Error'); ?>: </strong> <strong><?php echo __('Error'); ?>: </strong>
<?php printf(__('Database table %1$s for model %2$s was not found.'), '<em>' . $table . '</em>', '<em>' . $model . '</em>'); ?> <?php printf(__('Database table %1$s for model %2$s was not found.'), '<em>' . $table . '</em>', '<em>' . $class . '</em>'); ?>
</p> </p>
<p class="notice"> <p class="notice">
<strong><?php echo __('Notice'); ?>: </strong> <strong><?php echo __('Notice'); ?>: </strong>

View file

@ -20,7 +20,7 @@
<h2><?php printf(__('Private Method in %s'), $controller); ?></h2> <h2><?php printf(__('Private Method in %s'), $controller); ?></h2>
<p class="error"> <p class="error">
<strong><?php echo __('Error'); ?>: </strong> <strong><?php echo __('Error'); ?>: </strong>
<?php printf(__('%s%s cannot be accessed directly.'), '<em>' . $controller . '::</em>', '<em>' . $action . '</em>'); ?> <?php printf(__('%s%s cannot be accessed directly.'), '<em>' . $controller . '::</em>', '<em>' . $action . '()</em>'); ?>
</p> </p>
<p class="notice"> <p class="notice">
<strong><?php echo __('Notice'); ?>: </strong> <strong><?php echo __('Notice'); ?>: </strong>

View file

@ -54,10 +54,16 @@ class HelperCollection extends ObjectCollection {
$helperClass = $name . 'Helper'; $helperClass = $name . 'Helper';
if (!class_exists($helperClass)) { if (!class_exists($helperClass)) {
if (!App::import('Helper', $helper)) { if (!App::import('Helper', $helper)) {
throw new MissingHelperFileException(Inflector::underscore($name) . '.php'); throw new MissingHelperFileException(array(
'class' => $helperClass,
'file' => Inflector::underscore($name) . '.php'
));
} }
if (!class_exists($helperClass)) { if (!class_exists($helperClass)) {
throw new MissingHelperClassException($helperClass); throw new MissingHelperClassException(array(
'class' => $helperClass,
'file' => Inflector::underscore($name) . '.php'
));
} }
} }
$this->_loaded[$name] = new $helperClass($this->_View, $settings); $this->_loaded[$name] = new $helperClass($this->_View, $settings);
@ -74,9 +80,3 @@ class HelperCollection extends ObjectCollection {
} }
} }
/**
* Exceptions used by the HelperCollection.
*/
class MissingHelperFileException extends RuntimeException { }
class MissingHelperClassException extends RuntimeException { }

View file

@ -791,7 +791,7 @@ class View extends Object {
} }
} }
} }
throw new MissingViewException($defaultPath . $name . $this->ext); throw new MissingViewException(array('file' => $defaultPath . $name . $this->ext));
} }
/** /**
@ -824,7 +824,7 @@ class View extends Object {
} }
} }
} }
throw new MissingLayoutException($paths[0] . $file . $this->ext); throw new MissingLayoutException(array('file' => $paths[0] . $file . $this->ext));
} }
/** /**

View file

@ -426,7 +426,7 @@ class ErrorHandlerTest extends CakeTestCase {
function testMissingController() { function testMissingController() {
$this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController'); $this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController');
$exception = new MissingControllerException('PostsController'); $exception = new MissingControllerException(array('controller' => 'PostsController'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
ob_start(); ob_start();
@ -445,7 +445,7 @@ class ErrorHandlerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMissingAction() { function testMissingAction() {
$exception = new MissingActionException('PostsController::index()'); $exception = new MissingActionException(array('controller' => 'PostsController', 'action' => 'index'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
ob_start(); ob_start();
@ -473,7 +473,7 @@ class ErrorHandlerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testPrivateAction() { function testPrivateAction() {
$exception = new PrivateActionException('PostsController::_secretSauce()'); $exception = new PrivateActionException(array('controller' => 'PostsController' , 'action' => '_secretSauce'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
ob_start(); ob_start();
@ -491,14 +491,15 @@ class ErrorHandlerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMissingTable() { function testMissingTable() {
$exception = new MissingTableException('Article', 'articles'); $exception = new MissingTableException(array('table' => 'articles', 'class' => 'Article'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
$ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode'));
$ErrorHandler->controller->response->expects($this->once())->method('statusCode')->with(500);
ob_start(); ob_start();
$ErrorHandler->render(); $ErrorHandler->render();
$result = ob_get_clean(); $result = ob_get_clean();
$this->assertPattern('/HTTP\/1\.0 500 Internal Server Error/', $result);
$this->assertPattern('/<h2>Missing Database Table<\/h2>/', $result); $this->assertPattern('/<h2>Missing Database Table<\/h2>/', $result);
$this->assertPattern('/table <em>articles<\/em> for model <em>Article<\/em>/', $result); $this->assertPattern('/table <em>articles<\/em> for model <em>Article<\/em>/', $result);
} }
@ -510,14 +511,15 @@ class ErrorHandlerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMissingDatabase() { function testMissingDatabase() {
$exception = new MissingDatabaseException('default'); $exception = new MissingDatabaseException(array('connection' => 'default'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
$ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode'));
$ErrorHandler->controller->response->expects($this->once())->method('statusCode')->with(500);
ob_start(); ob_start();
$ErrorHandler->render(); $ErrorHandler->render();
$result = ob_get_clean(); $result = ob_get_clean();
$this->assertPattern('/HTTP\/1\.0 500 Internal Server Error/', $result);
$this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result); $this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result);
$this->assertPattern('/Confirm you have created the file/', $result); $this->assertPattern('/Confirm you have created the file/', $result);
} }
@ -529,7 +531,7 @@ class ErrorHandlerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMissingView() { function testMissingView() {
$exception = new MissingViewException('/posts/about.ctp'); $exception = new MissingViewException(array('file' => '/posts/about.ctp'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
ob_start(); ob_start();
@ -546,7 +548,7 @@ class ErrorHandlerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMissingLayout() { function testMissingLayout() {
$exception = new MissingLayoutException('layouts/my_layout.ctp'); $exception = new MissingLayoutException(array('file' => 'layouts/my_layout.ctp'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
ob_start(); ob_start();
@ -564,7 +566,7 @@ class ErrorHandlerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMissingConnection() { function testMissingConnection() {
$exception = new MissingConnectionException('Article'); $exception = new MissingConnectionException(array('class' => 'Article'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
ob_start(); ob_start();
@ -582,7 +584,7 @@ class ErrorHandlerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMissingHelperFile() { function testMissingHelperFile() {
$exception = new MissingHelperFileException('my_custom.php'); $exception = new MissingHelperFileException(array('file' => 'my_custom.php', 'class' => 'MyCustomHelper'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
ob_start(); ob_start();
@ -601,7 +603,7 @@ class ErrorHandlerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMissingHelperClass() { function testMissingHelperClass() {
$exception = new MissingHelperClassException('MyCustomHelper'); $exception = new MissingHelperClassException(array('file' => 'my_custom.php', 'class' => 'MyCustomHelper'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
ob_start(); ob_start();
@ -620,7 +622,7 @@ class ErrorHandlerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMissingBehaviorFile() { function testMissingBehaviorFile() {
$exception = new MissingBehaviorFileException('my_custom.php'); $exception = new MissingBehaviorFileException(array('file' => 'my_custom.php', 'class' => 'MyCustomBehavior'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
ob_start(); ob_start();
@ -639,7 +641,7 @@ class ErrorHandlerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMissingBehaviorClass() { function testMissingBehaviorClass() {
$exception = new MissingBehaviorClassException('MyCustomBehavior'); $exception = new MissingBehaviorClassException(array('file' => 'my_custom.php', 'class' => 'MyCustomBehavior'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
ob_start(); ob_start();
@ -657,7 +659,7 @@ class ErrorHandlerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMissingComponentFile() { function testMissingComponentFile() {
$exception = new MissingComponentFileException('sidebox.php'); $exception = new MissingComponentFileException(array('file' => 'sidebox.php', 'class' => 'SideboxComponent'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
ob_start(); ob_start();
@ -676,7 +678,7 @@ class ErrorHandlerTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMissingComponentClass() { function testMissingComponentClass() {
$exception = new MissingComponentClassException('SideboxComponent'); $exception = new MissingComponentClassException(array('file' => 'sidebox.php', 'class' => 'SideboxComponent'));
$ErrorHandler = new ErrorHandler($exception); $ErrorHandler = new ErrorHandler($exception);
ob_start(); ob_start();